Skip to content

Commit d743bd0

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

File tree

3 files changed

+143
-11
lines changed

3 files changed

+143
-11
lines changed

lib/errors.js

Lines changed: 6 additions & 6 deletions
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

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
const serverError = {
14+
isClient: false,
15+
code: SERVER_ERROR,
16+
statusCode: 500
17+
}
18+
19+
// HAFAS ReST API documentation 1.23.30 (06.06.2019)
20+
const byErrorCode = Object.assign(Object.create(null), {
21+
API_AUTH: {
22+
isClient: true,
23+
code: ACCESS_DENIED,
24+
message: 'invalid or missing authentication data',
25+
statusCode: 401
26+
},
27+
API_QUOTA: {
28+
isClient: true,
29+
code: ACCESS_DENIED,
30+
message: 'quota exceeded for API key',
31+
statusCode: 429
32+
},
33+
API_PARAM: {
34+
...invalidReq,
35+
message: 'parameter missing or invalid' // todo: use HAFAS message
36+
},
37+
API_FORMAT: {
38+
...invalidReq,
39+
message: 'requested response format not supported',
40+
statusCode: 406
41+
},
42+
SVC_PARAM: {
43+
...invalidReq,
44+
message: 'parameter missing or invalid' // todo: use HAFAS message
45+
},
46+
SVC_LOC: {
47+
...invalidReq,
48+
message: 'location missing or invalid'
49+
},
50+
SVC_LOC_ARR: {
51+
...invalidReq,
52+
message: 'destination location missing or invalid'
53+
},
54+
SVC_LOC_DEP: {
55+
...invalidReq,
56+
message: 'origin location missing or invalid'
57+
},
58+
SVC_LOC_VIA: {
59+
...invalidReq,
60+
message: 'change/via location missing or invalid'
61+
},
62+
SVC_LOC_EQUAL: {
63+
...invalidReq,
64+
message: 'origin & destination locations are equal'
65+
},
66+
SVC_LOC_NEAR: {
67+
...invalidReq,
68+
message: 'origin & destination are too near'
69+
},
70+
SVC_DATATIME: {
71+
...invalidReq,
72+
message: 'date/time missing or invalid'
73+
},
74+
SVC_DATATIME_PERIOD: {
75+
...invalidReq,
76+
message: 'date/time not in the timetable data or not allowed'
77+
},
78+
SVC_PROD: {
79+
...invalidReq,
80+
message: 'products fields missing or invalid'
81+
},
82+
SVC_CTX: {
83+
...invalidReq,
84+
message: 'context invalid'
85+
},
86+
SVC_FAILED_SEARCH: {
87+
...serverError,
88+
message: 'search unsuccessful'
89+
},
90+
SVC_NO_RESULT: {
91+
...invalidReq,
92+
message: 'no results found',
93+
statusCode: 404
94+
},
95+
SVC_NO_MATCH: {
96+
...invalidReq,
97+
message: 'no match found',
98+
statusCode: 404
99+
},
100+
INT_ERR: {
101+
...serverError,
102+
message: 'internal error'
103+
},
104+
SOT_AT_DEST: {
105+
...invalidReq,
106+
message: 'trip already arrived'
107+
},
108+
SOT_BEFORE_START: {
109+
...invalidReq,
110+
message: 'trip hasn\'t started yet'
111+
},
112+
SOT_CANCELLED: {
113+
...invalidReq,
114+
message: 'trip is cancelled'
115+
}
116+
})
117+
118+
module.exports = {
119+
ACCESS_DENIED,
120+
INVALID_REQUEST,
121+
NOT_FOUND,
122+
SERVER_ERROR,
123+
byErrorCode
124+
}

rest-exe.js

Lines changed: 13 additions & 5 deletions
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)