fix hafas errors 🐛, 1.2.6

This commit is contained in:
Jannis R 2017-11-06 21:16:14 +01:00
parent d72fdc8092
commit 237c1428c4
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
2 changed files with 11 additions and 5 deletions

View file

@ -20,6 +20,11 @@ const defaults = {
const hafasError = (err) => {
err.isHafasError = true
return err
}
const createRequest = (opt) => { const createRequest = (opt) => {
opt = Object.assign({}, defaults, opt) opt = Object.assign({}, defaults, opt)
@ -42,15 +47,16 @@ const createRequest = (opt) => {
if (!res.ok) { if (!res.ok) {
const err = new Error(res.statusText) const err = new Error(res.statusText)
err.statusCode = res.status err.statusCode = res.status
err.isHafasError = true throw hafasError(err)
throw err
} }
return res.json() return res.json()
}) })
.then((b) => { .then((b) => {
if (b.err) throw hafasError(b.err) if (b.err) throw hafasError(new Error(b.err))
if (!b.svcResL || !b.svcResL[0]) throw new Error('invalid response') if (!b.svcResL || !b.svcResL[0]) throw new Error('invalid response')
if (b.svcResL[0].err !== 'OK') throw hafasError(b.svcResL[0].errTxt) if (b.svcResL[0].err !== 'OK') {
throw hafasError(new Error(b.svcResL[0].errTxt))
}
const d = b.svcResL[0].res const d = b.svcResL[0].res
const c = d.common || {} const c = d.common || {}

View file

@ -1,7 +1,7 @@
{ {
"name": "hafas-client", "name": "hafas-client",
"description": "JavaScript client for HAFAS mobile APIs.", "description": "JavaScript client for HAFAS mobile APIs.",
"version": "1.2.5", "version": "1.2.6",
"main": "index.js", "main": "index.js",
"files": [ "files": [
"index.js", "index.js",