"invalid response" error: add isHafasError flag

So that consuming code can tell that this error is caused by an
invalid/unexpected response from HAFAS.
This commit is contained in:
Jannis R 2020-04-01 19:03:36 +02:00
parent 33d77868a4
commit e032ec1acd
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5

View file

@ -256,7 +256,11 @@ const createClient = (profile, userAgent, opt = {}) => {
return profile.request({profile, opt}, userAgent, req) return profile.request({profile, opt}, userAgent, req)
.then(({res, common}) => { .then(({res, common}) => {
if (!Array.isArray(res.outConL) || !res.outConL[0]) { if (!Array.isArray(res.outConL) || !res.outConL[0]) {
throw new Error('invalid response') const err = new Error('invalid response')
// technically this is not a HAFAS error
// todo: find a different flag with decent DX
err.isHafasError = true
throw err
} }
const ctx = {profile, opt, common, res} const ctx = {profile, opt, common, res}
@ -303,7 +307,11 @@ const createClient = (profile, userAgent, opt = {}) => {
.then(({res, common}) => { .then(({res, common}) => {
if (!res || !Array.isArray(res.locL) || !res.locL[0]) { if (!res || !Array.isArray(res.locL) || !res.locL[0]) {
// todo: proper stack trace? // todo: proper stack trace?
throw new Error('invalid response') const err = new Error('invalid response')
// technically this is not a HAFAS error
// todo: find a different flag with decent DX
err.isHafasError = true
throw err
} }
const ctx = {profile, opt, res, common} const ctx = {profile, opt, res, common}