mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 15:19:35 +02:00
refreshJourney: actually throw the error 🐛, add code
This commit is contained in:
parent
87e5649f89
commit
3c888a0ea0
3 changed files with 21 additions and 17 deletions
7
index.js
7
index.js
|
@ -8,6 +8,7 @@ const pRetry = require('p-retry')
|
||||||
|
|
||||||
const defaultProfile = require('./lib/default-profile')
|
const defaultProfile = require('./lib/default-profile')
|
||||||
const validateProfile = require('./lib/validate-profile')
|
const validateProfile = require('./lib/validate-profile')
|
||||||
|
const {INVALID_REQUEST} = require('./lib/errors')
|
||||||
|
|
||||||
const isNonEmptyString = str => 'string' === typeof str && str.length > 0
|
const isNonEmptyString = str => 'string' === typeof str && str.length > 0
|
||||||
|
|
||||||
|
@ -241,7 +242,7 @@ const createClient = (profile, userAgent, opt = {}) => {
|
||||||
|
|
||||||
const refreshJourney = (refreshToken, opt = {}) => {
|
const refreshJourney = (refreshToken, opt = {}) => {
|
||||||
if ('string' !== typeof refreshToken || !refreshToken) {
|
if ('string' !== typeof refreshToken || !refreshToken) {
|
||||||
new TypeError('refreshToken must be a non-empty string.')
|
throw new TypeError('refreshToken must be a non-empty string.')
|
||||||
}
|
}
|
||||||
|
|
||||||
opt = Object.assign({
|
opt = Object.assign({
|
||||||
|
@ -307,10 +308,12 @@ 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?
|
||||||
const err = new Error('invalid response')
|
// todo: DRY with lib/request.js
|
||||||
|
const err = new Error('response has no stop')
|
||||||
// technically this is not a HAFAS error
|
// technically this is not a HAFAS error
|
||||||
// todo: find a different flag with decent DX
|
// todo: find a different flag with decent DX
|
||||||
err.isHafasError = true
|
err.isHafasError = true
|
||||||
|
err.code = INVALID_REQUEST
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -206,10 +206,23 @@ const byErrorCode = Object.assign(Object.create(null), {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const addErrorInfo = (err, errorCode, errorText, responseId) => {
|
||||||
|
if (byErrorCode[errorCode]) {
|
||||||
|
Object.assign(err, byErrorCode[errorCode])
|
||||||
|
if (errorCode) err.hafasErrorCode = errorCode
|
||||||
|
if (errorText) err.hafasErrorMessage = errorText
|
||||||
|
} else {
|
||||||
|
err.code = errorCode || null
|
||||||
|
err.message = errorText || errorCode || null
|
||||||
|
err.responseId = responseId || null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
ACCESS_DENIED,
|
ACCESS_DENIED,
|
||||||
INVALID_REQUEST,
|
INVALID_REQUEST,
|
||||||
NOT_FOUND,
|
NOT_FOUND,
|
||||||
SERVER_ERROR,
|
SERVER_ERROR,
|
||||||
byErrorCode
|
byErrorCode,
|
||||||
|
addErrorInfo,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const DEV = process.env.NODE_ENV === 'dev'
|
const DEV = process.env.NODE_ENV === 'dev'
|
||||||
const DEBUG = process.env.DEBUG === 'hafas-client'
|
const DEBUG = /\bhafas-client\b/.test(process.env.DEBUG || '')
|
||||||
|
|
||||||
const {randomBytes} = require('crypto')
|
const {randomBytes} = require('crypto')
|
||||||
const createHash = require('create-hash')
|
const createHash = require('create-hash')
|
||||||
|
@ -9,7 +9,7 @@ const captureStackTrace = DEV ? require('capture-stack-trace') : () => {}
|
||||||
const {stringify} = require('qs')
|
const {stringify} = require('qs')
|
||||||
const Promise = require('pinkie-promise')
|
const Promise = require('pinkie-promise')
|
||||||
const {fetch} = require('fetch-ponyfill')({Promise})
|
const {fetch} = require('fetch-ponyfill')({Promise})
|
||||||
const {byErrorCode} = require('./errors')
|
const {addErrorInfo} = require('./errors')
|
||||||
|
|
||||||
const id = randomBytes(6).toString('hex')
|
const id = randomBytes(6).toString('hex')
|
||||||
const randomizeUserAgent = (userAgent) => {
|
const randomizeUserAgent = (userAgent) => {
|
||||||
|
@ -19,18 +19,6 @@ const randomizeUserAgent = (userAgent) => {
|
||||||
|
|
||||||
const md5 = input => createHash('md5').update(input).digest()
|
const md5 = input => createHash('md5').update(input).digest()
|
||||||
|
|
||||||
const addErrorInfo = (err, errorCode, errorText, responseId) => {
|
|
||||||
if (byErrorCode[errorCode]) {
|
|
||||||
Object.assign(err, byErrorCode[errorCode])
|
|
||||||
if (errorCode) err.hafasErrorCode = errorCode
|
|
||||||
if (errorText) err.hafasErrorMessage = errorText
|
|
||||||
} else {
|
|
||||||
err.code = errorCode || null
|
|
||||||
err.message = errorText || errorCode || null
|
|
||||||
err.responseId = responseId || null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const request = (ctx, userAgent, reqData) => {
|
const request = (ctx, userAgent, reqData) => {
|
||||||
const {profile, opt} = ctx
|
const {profile, opt} = ctx
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue