Skip to content

Commit

Permalink
Merge pull request #171 from kieler/170-error-handling-in-services
Browse files Browse the repository at this point in the history
170 error handling in services
  • Loading branch information
Kebslock authored Sep 18, 2023
2 parents 9f68ece + 6b39223 commit 2f619ab
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 326 deletions.
57 changes: 17 additions & 40 deletions Server/src/routes/init.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,14 @@ export class InitRoute {

const track: Track = await database.tracks.getById(id)

const lineString: Feature<LineString> | null = TrackService.getTrackAsLineString(track)
if (!lineString) {
logger.error(`Could not convert track to line string`)
res.sendStatus(500)
return
}
const lineString: Feature<LineString> = TrackService.getTrackAsLineString(track)

const path: FeatureCollection<LineString> = {
type: "FeatureCollection",
features: [lineString]
}

const length: number | null = TrackService.getTrackLength(track)
if (length == null) {
logger.error(`Could not determine length of track with id ${id}`)
res.sendStatus(500)
return
}

const length: number = TrackService.getTrackLength(track)
const pois: POI[] = await POIService.getAllPOIsForTrack(track)
const apiPois: z.infer<typeof PointOfInterest>[] = await this.getAppPoisFromDbPoi(pois)

Expand Down Expand Up @@ -126,31 +115,13 @@ export class InitRoute {
geometry: { type: "Point", coordinates: [pos.lng, pos.lat] },
properties: null
}
const currentTrack: Track | null = await TrackService.getClosestTrack(backendPos)

if (!currentTrack) {
logger.error(`Could not find current track with position {lat : ${pos.lat}, lng : ${pos.lng}}`)
res.sendStatus(500)
return
}

const length: number | null = TrackService.getTrackLength(currentTrack)

if (length == null) {
logger.error(`Length of track with id ${currentTrack.uid} could not be determined`)
res.sendStatus(500)
return
}
const currentTrack: Track = await TrackService.getClosestTrack(backendPos)
const length: number = TrackService.getTrackLength(currentTrack)

const pois: POI[] = await POIService.getAllPOIsForTrack(currentTrack)
const apiPois: z.infer<typeof PointOfInterest>[] = await this.getAppPoisFromDbPoi(pois)

const lineString: Feature<LineString> | null = TrackService.getTrackAsLineString(currentTrack)
if (!lineString) {
logger.error(`Could not read track with id ${currentTrack.uid} as line string`)
res.sendStatus(500)
return
}
const lineString: Feature<LineString> = TrackService.getTrackAsLineString(currentTrack)

const path: FeatureCollection<LineString> = {
type: "FeatureCollection",
Expand Down Expand Up @@ -195,18 +166,24 @@ export class InitRoute {
// ensure that the app always gets an enum member.
const appType: z.infer<typeof POITypeIcon> = poiIcon in POITypeIcon.enum ? poiIcon : POITypeIconEnum.Generic

const geoJsonPos: Feature<Point> | null = GeoJSONUtils.parseGeoJSONFeaturePoint(poi.position)
if (!geoJsonPos) {
logger.error(`Could not find position of POI with id ${poi.uid}`)
let geoJsonPos: Feature<Point>
try {
geoJsonPos = GeoJSONUtils.parseGeoJSONFeaturePoint(poi.position)
} catch (error) {
logger.warn(`Could not find position of POI with id ${poi.uid}`)
continue
}

const pos: z.infer<typeof Position> = {
lat: GeoJSONUtils.getLatitude(geoJsonPos),
lng: GeoJSONUtils.getLongitude(geoJsonPos)
}
const percentagePosition: number | null = await POIService.getPOITrackDistancePercentage(poi)
if (percentagePosition == null) {
logger.error(`Could not determine percentage position of poi with id ${poi.uid}`)

let percentagePosition: number
try {
percentagePosition = await POIService.getPOITrackDistancePercentage(poi)
} catch (err) {
logger.warn(`Could not determine percentage position of poi with id ${poi.uid}`)
continue
}

Expand Down
23 changes: 4 additions & 19 deletions Server/src/routes/poi.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,9 @@ export class PoiRoute {
private async getAllPOIs(_req: Request, res: Response): Promise<void> {
const pois: POI[] = await database.pois.getAll()

const typedPOIs: (z.infer<typeof UpdatePointOfInterest> | null)[] = pois.map(
const typedPOIs: z.infer<typeof UpdatePointOfInterest>[] = pois.map(
({ uid, name, trackId, description, isTurningPoint, typeId, position }) => {
const geoJsonPos: Feature<Point> | null = GeoJSONUtils.parseGeoJSONFeaturePoint(position)
if (!geoJsonPos) {
logger.error(`Could not find position of POI with id ${uid}`)
return null
}
const geoJsonPos: Feature<Point> = GeoJSONUtils.parseGeoJSONFeaturePoint(position)
const pos: z.infer<typeof Position> = {
lat: GeoJSONUtils.getLatitude(geoJsonPos),
lng: GeoJSONUtils.getLongitude(geoJsonPos)
Expand Down Expand Up @@ -80,12 +76,7 @@ export class PoiRoute {

const poi: POI = await database.pois.getById(poiId)

const geoPos: Feature<Point> | null = GeoJSONUtils.parseGeoJSONFeaturePoint(poi.position)
if (!geoPos) {
logger.error(`Could not find position of POI with id ${poi.uid}`)
res.sendStatus(500)
return
}
const geoPos: Feature<Point> = GeoJSONUtils.parseGeoJSONFeaturePoint(poi.position)
const pos: z.infer<typeof Position> = {
lat: GeoJSONUtils.getLatitude(geoPos),
lng: GeoJSONUtils.getLongitude(geoPos)
Expand Down Expand Up @@ -126,7 +117,7 @@ export class PoiRoute {

const type: POIType = await database.pois.getTypeById(poiPayload.typeId)

const newPOI: POI | null = await POIService.createPOI(
const newPOI: POI = await POIService.createPOI(
geopos,
poiPayload.name ? poiPayload.name : "",
type,
Expand All @@ -135,12 +126,6 @@ export class PoiRoute {
poiPayload.isTurningPoint
)

if (!newPOI) {
logger.error(`Could not create new POI`)
res.sendStatus(500)
return
}

res.json({ id: newPOI.uid })
return
}
Expand Down
49 changes: 23 additions & 26 deletions Server/src/routes/track.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { POI, Track, Vehicle } from "@prisma/client"
import please_dont_crash from "../utils/please_dont_crash"
import { logger } from "../utils/logger"
import { BareTrack, FullTrack, PointOfInterest, Position, UpdateTrack, Vehicle as APIVehicle } from "../models/api"
import VehicleService from "../services/vehicle.service"
import VehicleService, { VehicleData } from "../services/vehicle.service"
import { Feature, LineString, Point } from "geojson"
import POIService from "../services/poi.service"
import GeoJSONUtils from "../utils/geojsonUtils"
Expand Down Expand Up @@ -97,20 +97,8 @@ export class TrackRoute {
const track: Track = await database.tracks.getById(trackId)

// derive and transform the database data for easier digestion by the clients.
const path: Feature<LineString> | null = TrackService.getTrackAsLineString(track)
const length: number | null = TrackService.getTrackLength(track)

if (!path) {
logger.error(`Could not get track with id ${track.uid} as a line string`)
res.sendStatus(500)
return
}

if (length == null) {
logger.error(`Length of track with id ${track.uid} could not be determined`)
res.sendStatus(500)
return
}
const path: Feature<LineString> = TrackService.getTrackAsLineString(track)
const length: number = TrackService.getTrackLength(track)

// Build the response object
const api_track: z.infer<typeof FullTrack> = {
Expand Down Expand Up @@ -197,9 +185,16 @@ export class TrackRoute {
// obtain vehicles associated with the track from the db.
const vehicles: Vehicle[] = await database.vehicles.getAll(track.uid)
const ret: z.infer<typeof APIVehicle>[] = await Promise.allSettled(
vehicles.map(async (vehicle: Vehicle) => {
vehicles.flatMap(async (vehicle: Vehicle) => {
// get the current data of the vehicle
const vehicleData = await VehicleService.getVehicleData(vehicle)
let vehicleData: VehicleData
try {
vehicleData = await VehicleService.getVehicleData(vehicle)
} catch (err) {
logger.warn(`Could not compute vehicle data for vehicle ${vehicle.uid}.`)
return []
}

// If we know that, convert it in the API format.
const pos: z.infer<typeof Position> | undefined = {
lat: GeoJSONUtils.getLatitude(vehicleData.position),
Expand Down Expand Up @@ -242,22 +237,24 @@ export class TrackRoute {
const pois: POI[] = await database.pois.getAll(trackId)
const ret: z.infer<typeof PointOfInterest>[] = (
await Promise.all(
pois.map(async (poi: POI) => {
const pos: Feature<Point> | null = GeoJSONUtils.parseGeoJSONFeaturePoint(poi.position)
if (!pos) {
logger.error(`Could not find position of POI with id ${poi.uid}`)
// res.sendStatus(500)
pois.flatMap(async (poi: POI) => {
let pos: Feature<Point>
try {
pos = GeoJSONUtils.parseGeoJSONFeaturePoint(poi.position)
} catch (err) {
logger.warn(`Could not find position of POI with id ${poi.uid}`)
return []
}
const actualPos: z.infer<typeof Position> = {
lat: GeoJSONUtils.getLatitude(pos),
lng: GeoJSONUtils.getLongitude(pos)
}
const percentagePosition: number | null = await POIService.getPOITrackDistancePercentage(poi)

if (percentagePosition == null) {
logger.error(`Could not find percentage position of POI with id ${poi.uid}`)
// res.sendStatus(500)
let percentagePosition: number
try {
percentagePosition = await POIService.getPOITrackDistancePercentage(poi)
} catch (err) {
logger.warn(`Could not find percentage position of POI with id ${poi.uid}`)
return []
}

Expand Down
Loading

0 comments on commit 2f619ab

Please sign in to comment.