From 0aaf3be4c6aea889f49e89c9905583ab8bf09d1b Mon Sep 17 00:00:00 2001 From: Kevin Ebsen Date: Fri, 15 Sep 2023 19:22:29 +0200 Subject: [PATCH] #170: Using HTTPError instead of returning null in GeoJSONUtils --- Server/src/utils/geojsonUtils.ts | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/Server/src/utils/geojsonUtils.ts b/Server/src/utils/geojsonUtils.ts index 3de631e5..83affc67 100644 --- a/Server/src/utils/geojsonUtils.ts +++ b/Server/src/utils/geojsonUtils.ts @@ -1,3 +1,5 @@ +import { HTTPError } from "../models/error" + /** * Some utilities for simpler handling of GeoJSON */ @@ -21,11 +23,11 @@ export default class GeoJSONUtils { /** * Get track kilometer for given GeoJSON point (basically a wrapper for accessing this property) * @param point GeoJSON point to get the track kilometer for - * @returns track kilometer if available, `null` otherwise + * @returns track kilometer if available */ - public static getTrackKm(point: GeoJSON.Feature): number | null { + public static getTrackKm(point: GeoJSON.Feature): number { if (point.properties == null || point.properties["trackKm"] == null) { - return null + throw new HTTPError(`Could not get track kilometer of position ${JSON.stringify(point)}.`, 500) } return point.properties["trackKm"] } @@ -87,37 +89,37 @@ export default class GeoJSONUtils { /** * Parses JSON to a GeoJSON feature of a point (if possible) - * @param json JSON to parse - * @returns parsed GeoJSON feature or `null` if an error occured while parsing + * @param object JSON to parse + * @returns parsed GeoJSON feature */ - public static parseGeoJSONFeaturePoint(json: unknown): GeoJSON.Feature | null { - if (this.isGeoJSONFeaturePoint(json)) { - return json as GeoJSON.Feature - } else if (this.isGeoJSONPosition(json)) { + public static parseGeoJSONFeaturePoint(object: unknown): GeoJSON.Feature { + if (this.isGeoJSONFeaturePoint(object)) { + return object as GeoJSON.Feature + } else if (this.isGeoJSONPosition(object)) { // If we just have plain 2D coordinates, construct a point feature. const feature: GeoJSON.Feature = { type: "Feature", properties: {}, geometry: { type: "Point", - coordinates: json + coordinates: object } } return feature } - return null + throw new HTTPError(`Could not parse ${JSON.stringify(object)} as GeoJSON feature of point.`, 500) } /** * Try to parse anything to a GeoJSON feature collection of points (if possible) * @param object object to parse - * @returns parsed GeoJSON feature collection or `null` if an error occured while parsing + * @returns parsed GeoJSON feature collection */ - public static parseGeoJSONFeatureCollectionPoints(object: unknown): GeoJSON.FeatureCollection | null { + public static parseGeoJSONFeatureCollectionPoints(object: unknown): GeoJSON.FeatureCollection { if (this.isGeoJSONFeatureCollectionPoints(object)) { return object as GeoJSON.FeatureCollection } - return null + throw new HTTPError(`Could not parse ${JSON.stringify(object)} as GeoJSON feature collection of points.`, 500) } /**