@@ -10,6 +10,7 @@ import { Request, Response } from "express";
10
10
import * as OpenApiValidator from "express-openapi-validator" ;
11
11
import { ROUTING_API , RoutingSvc } from "./services/routing" ;
12
12
import { paths } from "./api" ;
13
+ import { ValidationError } from "./error" ;
13
14
14
15
type GeoLocation = {
15
16
latitude : number ;
@@ -49,37 +50,31 @@ app.post("/route", async (req: Request, res: Response) => {
49
50
const r =
50
51
req . body as paths [ "/route" ] [ "post" ] [ "requestBody" ] [ "content" ] [ "application/json" ] ;
51
52
52
- try {
53
- let srcLocation : GeoLocation ;
54
- let destLocation : GeoLocation ;
53
+ let srcLocation : GeoLocation ;
54
+ let destLocation : GeoLocation ;
55
55
56
- if ( r . src . kind === "address" ) {
57
- const postcode = r . src . address ?. postcode ?. trim ( ) ;
58
- if ( ! postcode ) {
59
- throw new Error ( "Source address must include a postcode." ) ;
60
- }
61
- srcLocation = await routing_service . geolookup ( postcode ) ;
62
- } else {
63
- srcLocation = r . src . location ! ;
56
+ if ( r . src . kind === "address" ) {
57
+ const postcode = r . src . address ?. postcode ?. trim ( ) ;
58
+ if ( ! postcode ) {
59
+ throw new ValidationError ( "Source address must include a postcode." ) ;
64
60
}
61
+ srcLocation = await routing_service . geolookup ( postcode ) ;
62
+ } else {
63
+ srcLocation = r . src . location ! ;
64
+ }
65
65
66
- if ( r . dest . kind === "address" ) {
67
- const postcode = r . dest . address ?. postcode ?. trim ( ) ;
68
- if ( ! postcode ) {
69
- throw new Error ( "Destination address must include a postcode." ) ;
70
- }
71
- destLocation = await routing_service . geolookup ( postcode ) ;
72
- } else {
73
- destLocation = r . dest . location ! ;
66
+ if ( r . dest . kind === "address" ) {
67
+ const postcode = r . dest . address ?. postcode ?. trim ( ) ;
68
+ if ( ! postcode ) {
69
+ throw new ValidationError ( "Destination address must include a postcode." ) ;
74
70
}
75
-
76
- const routes = await routing_service . route ( srcLocation , destLocation ) ;
77
- res . json ( { routes } ) ;
78
- } catch ( error ) {
79
- console . error ( error ) ;
80
- const err = error as Error ;
81
- res . status ( 500 ) . json ( { message : err . message } ) ;
71
+ destLocation = await routing_service . geolookup ( postcode ) ;
72
+ } else {
73
+ destLocation = r . dest . location ! ;
82
74
}
75
+
76
+ const routes = await routing_service . route ( srcLocation , destLocation ) ;
77
+ res . json ( { routes } ) ;
83
78
} ) ;
84
79
85
80
app . get ( "/geocode/:postcode" , async ( req : Request , res : Response ) => {
0 commit comments