mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 15:19:35 +02:00
nicer async stack traces
This commit is contained in:
parent
96448bf900
commit
a28ead4cc1
2 changed files with 25 additions and 12 deletions
|
@ -1,14 +1,13 @@
|
|||
'use strict'
|
||||
|
||||
let captureStackTrace = () => {}
|
||||
if (process.env.NODE_ENV === 'dev') {
|
||||
captureStackTrace = require('capture-stack-trace')
|
||||
}
|
||||
const Promise = require('pinkie-promise')
|
||||
const {fetch} = require('fetch-ponyfill')({Promise})
|
||||
const {stringify} = require('query-string')
|
||||
|
||||
const hafasError = (err) => {
|
||||
err.isHafasError = true
|
||||
return err
|
||||
}
|
||||
|
||||
const request = (profile, data) => {
|
||||
const body = profile.transformReqBody({lang: 'en', svcReqL: [data]})
|
||||
const req = profile.transformReq({
|
||||
|
@ -24,20 +23,33 @@ const request = (profile, data) => {
|
|||
})
|
||||
const url = profile.endpoint + (req.query ? '?' + stringify(req.query) : '')
|
||||
|
||||
// Async stack traces are not supported everywhere yet, so we create our own.
|
||||
const err = new Error()
|
||||
err.isHafasError = true
|
||||
err.request = body
|
||||
captureStackTrace(err)
|
||||
|
||||
return fetch(url, req)
|
||||
.then((res) => {
|
||||
if (!res.ok) {
|
||||
const err = new Error(res.statusText)
|
||||
err.statusCode = res.status
|
||||
throw hafasError(err)
|
||||
if (!res.ok) {
|
||||
err.message = res.statusText
|
||||
throw err
|
||||
}
|
||||
return res.json()
|
||||
})
|
||||
.then((b) => {
|
||||
if (b.err) throw hafasError(new Error(b.err))
|
||||
if (!b.svcResL || !b.svcResL[0]) throw new Error('invalid response')
|
||||
if (b.err) {
|
||||
err.message = b.err
|
||||
throw err
|
||||
}
|
||||
if (!b.svcResL || !b.svcResL[0]) {
|
||||
err.message = 'invalid response'
|
||||
throw err
|
||||
}
|
||||
if (b.svcResL[0].err !== 'OK') {
|
||||
throw hafasError(new Error(b.svcResL[0].errTxt))
|
||||
err.message = b.svcResL[0].errTxt
|
||||
throw err
|
||||
}
|
||||
const d = b.svcResL[0].res
|
||||
const c = d.common || {}
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
"node": ">=6"
|
||||
},
|
||||
"dependencies": {
|
||||
"capture-stack-trace": "^1.0.0",
|
||||
"fetch-ponyfill": "^4.1.0",
|
||||
"lodash": "^4.17.4",
|
||||
"luxon": "^0.3.1",
|
||||
|
@ -55,7 +56,7 @@
|
|||
"vbb-stations-autocomplete": "^3.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "node test/index.js",
|
||||
"test": "env NODE_ENV=dev node test/index.js",
|
||||
"prepublishOnly": "npm test | tap-spec"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue