Skip to content

Commit b44a44b

Browse files
committed
rest.exe client: error codes
1 parent 34ed7d7 commit b44a44b

File tree

3 files changed

+130
-11
lines changed

3 files changed

+130
-11
lines changed

lib/errors.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const byErrorCode = Object.assign(Object.create(null), {
5252
H390: {
5353
isClient: true,
5454
code: INVALID_REQUEST,
55-
message: 'journeys search: departure/arrival station replaced',
55+
message: 'journeys search: origin/destination replaced',
5656
statusCode: 400
5757
},
5858
H410: {
@@ -101,7 +101,7 @@ const byErrorCode = Object.assign(Object.create(null), {
101101
H895: {
102102
isClient: true,
103103
code: INVALID_REQUEST,
104-
message: 'journeys search: departure & arrival are too near',
104+
message: 'journeys search: origin & destination are too near',
105105
statusCode: 400
106106
},
107107
H899: {
@@ -145,19 +145,19 @@ const byErrorCode = Object.assign(Object.create(null), {
145145
H9260: {
146146
isClient: true,
147147
code: INVALID_REQUEST,
148-
message: 'journeys search: unknown departure station',
148+
message: 'journeys search: unknown origin',
149149
statusCode: 400
150150
},
151151
H9280: {
152152
isClient: true,
153153
code: INVALID_REQUEST,
154-
message: 'journeys search: unknown intermediate station',
154+
message: 'journeys search: unknown change/via station',
155155
statusCode: 400
156156
},
157157
H9300: {
158158
isClient: true,
159159
code: INVALID_REQUEST,
160-
message: 'journeys search: unknown arrival station',
160+
message: 'journeys search: unknown destination',
161161
statusCode: 400
162162
},
163163
H9320: {
@@ -175,7 +175,7 @@ const byErrorCode = Object.assign(Object.create(null), {
175175
H9380: {
176176
isClient: true,
177177
code: INVALID_REQUEST,
178-
message: 'journeys search: departure/arrival/intermediate station defined more than once',
178+
message: 'journeys search: origin/destination/via defined more than once',
179179
statusCode: 400
180180
},
181181
SQ001: {

rest-exe-errors.js

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
'use strict'
2+
3+
const ACCESS_DENIED = 'ACCESS_DENIED'
4+
const INVALID_REQUEST = 'INVALID_REQUEST'
5+
const NOT_FOUND = 'NOT_FOUND'
6+
const SERVER_ERROR = 'SERVER_ERROR'
7+
8+
const invalidReq = {
9+
isClient: true,
10+
code: INVALID_REQUEST,
11+
statusCode: 400
12+
}
13+
14+
// HAFAS ReST API documentation 1.23.26 (06.09.2018)
15+
const byErrorCode = Object.assign(Object.create(null), {
16+
API_AUTH: {
17+
isClient: true,
18+
code: ACCESS_DENIED,
19+
message: 'invalid or missing authentication data',
20+
statusCode: 401
21+
},
22+
API_QUOTA: {
23+
isClient: true,
24+
code: ACCESS_DENIED,
25+
message: 'quota exceeded for API key',
26+
statusCode: 429
27+
},
28+
API_PARAM: {
29+
...invalidReq,
30+
message: 'parameter missing or invalid' // todo: use HAFAS message
31+
},
32+
API_FORMAT: {
33+
...invalidReq,
34+
message: 'requested response format not supported',
35+
statusCode: 406
36+
},
37+
SVC_PARAM: {
38+
...invalidReq,
39+
message: 'parameter missing or invalid' // todo: use HAFAS message
40+
},
41+
SVC_LOC: {
42+
...invalidReq,
43+
message: 'location missing or invalid'
44+
},
45+
SVC_LOC_ARR: {
46+
...invalidReq,
47+
message: 'destination location missing or invalid'
48+
},
49+
SVC_LOC_DEP: {
50+
...invalidReq,
51+
message: 'origin location missing or invalid'
52+
},
53+
SVC_LOC_VIA: {
54+
...invalidReq,
55+
message: 'change/via location missing or invalid'
56+
},
57+
SVC_LOC_EQUAL: {
58+
...invalidReq,
59+
message: 'origin & destination locations are equal'
60+
},
61+
SVC_LOC_NEAR: {
62+
...invalidReq,
63+
message: 'origin & destination are too near'
64+
},
65+
SVC_DATATIME: {
66+
...invalidReq,
67+
message: 'date/time missing or invalid'
68+
},
69+
SVC_DATATIME_PERIOD: {
70+
...invalidReq,
71+
message: 'date/time not in the timetable data or not allowed'
72+
},
73+
SVC_PROD: {
74+
...invalidReq,
75+
message: 'products fields missing or invalid'
76+
},
77+
SVC_CTX: {
78+
...invalidReq,
79+
message: 'context invalid'
80+
},
81+
SVC_FAILED_SEARCH: {
82+
isClient: false,
83+
code: SERVER_ERROR,
84+
message: 'search unsuccessful',
85+
statusCode: 500
86+
},
87+
SVC_NO_RESULT: {
88+
...invalidReq,
89+
message: 'no results found',
90+
statusCode: 404
91+
},
92+
SVC_NO_MATCH: {
93+
...invalidReq,
94+
message: 'no match found',
95+
statusCode: 404
96+
},
97+
INT_ERR: {
98+
isClient: false,
99+
code: SERVER_ERROR,
100+
message: 'internal error',
101+
statusCode: 500
102+
}
103+
})
104+
105+
module.exports = {
106+
ACCESS_DENIED,
107+
INVALID_REQUEST,
108+
NOT_FOUND,
109+
SERVER_ERROR,
110+
byErrorCode
111+
}

rest-exe.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const Promise = require('pinkie-promise')
77
const {fetch} = require('fetch-ponyfill')({Promise})
88
const {stringify} = require('qs')
99
const {parse: parseContentType} = require('content-type')
10+
const {byErrorCode} = require('./rest-exe-errors')
1011
const findInTree = require('./lib/find-in-tree')
1112
const parseWhen = require('./parse-rest/when')
1213
const parsePolyline = require('./parse-rest/polyline')
@@ -46,16 +47,25 @@ const request = async (method, query = {}) => {
4647
}
4748
const res = await fetch(url, fetchCfg)
4849

49-
const {type: cType} = parseContentType(res.headers.get('content-type'))
50+
const cTypeHeader = res.headers.get('content-type')
51+
const {type: cType} = cTypeHeader ? parseContentType(cTypeHeader) : {}
5052
const asJSON = cType === 'application/json'
5153
const body = asJSON ? await res.json() : await res.text()
54+
if (DEBUG) console.error(asJSON ? JSON.stringify(body) : body)
5255

5356
if (!res.ok) {
5457
// todo: parse HTML error messages
5558
let err = new Error(res.statusText)
5659
if (asJSON) {
57-
err = new Error(body.errorText)
58-
err.code = body.errorCode
60+
const {errorCode, errorText} = body
61+
if (errorCode && byErrorCode[errorCode]) {
62+
Object.assign(err, byErrorCode[errorCode])
63+
err.hafasErrorCode = errorCode
64+
if (errorText) err.hafasErrorMessage = errorText
65+
} else {
66+
err = new Error(errorText)
67+
err.code = errorCode
68+
}
5969
} else if (body) err = new Error(body)
6070

6171
err.statusCode = res.status
@@ -69,8 +79,6 @@ const request = async (method, query = {}) => {
6979
// todo: sometimes it returns a body without any data
7080
// e.g. `location.nearbystops` with an invalid `type`
7181

72-
if (DEBUG) console.error(asJSON ? JSON.stringify(body) : body)
73-
7482
unwrapNested(body, '**.ServiceDays[0]', 'serviceDays')
7583
unwrapNested(body, '**.LegList.Leg', 'legs')
7684
unwrapNested(body, '**.Notes.Note', 'notes')

0 commit comments

Comments
 (0)