debug hook logging request & response

This commit is contained in:
Jannis R 2018-09-03 15:31:20 +02:00
parent e98cec1734
commit 3c104e2233
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5

View file

@ -1,11 +1,10 @@
'use strict' 'use strict'
const DEBUG = process.env.NODE_DEBUG === 'hafas-client'
const {join} = require('path') const {join} = require('path')
const createHash = require('create-hash') const createHash = require('create-hash')
let captureStackTrace = () => {} const captureStackTrace = DEBUG ? require('capture-stack-trace') : () => {}
if (process.env.NODE_DEBUG === 'hafas-client') {
captureStackTrace = require('capture-stack-trace')
}
const {stringify} = require('query-string') const {stringify} = require('query-string')
const Promise = require('pinkie-promise') const Promise = require('pinkie-promise')
const {fetch} = require('fetch-ponyfill')({Promise}) const {fetch} = require('fetch-ponyfill')({Promise})
@ -31,6 +30,8 @@ const request = (profile, userAgent, opt, data) => {
lang: opt.language || 'en', // todo: is it `eng` actually? lang: opt.language || 'en', // todo: is it `eng` actually?
svcReqL: [data] svcReqL: [data]
}) })
if (DEBUG) console.error(JSON.stringify(body))
const req = profile.transformReq({ const req = profile.transformReq({
method: 'post', method: 'post',
// todo: CORS? referrer policy? // todo: CORS? referrer policy?
@ -84,6 +85,8 @@ const request = (profile, userAgent, opt, data) => {
return res.json() return res.json()
}) })
.then((b) => { .then((b) => {
if (DEBUG) console.error(JSON.stringify(b))
if (b.err) { if (b.err) {
err.message = b.err err.message = b.err
throw err throw err