Skip to content

Commit

Permalink
err.request + err.response
Browse files Browse the repository at this point in the history
  • Loading branch information
sjvans committed Sep 13, 2024
1 parent 7063d9b commit 6d05fa3
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions srv/log2restv2.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,16 @@ async function _post(url, data, options) {
const chunks = []
res.on('data', chunk => chunks.push(chunk))
res.on('end', () => {
const { statusCode, statusMessage } = res
let body = Buffer.concat(chunks).toString()
if (res.headers['content-type']?.match(/json/)) body = JSON.parse(body)
if (res.statusCode >= 400) {
const err = new Error(res.statusMessage)
err.response = res
err.body = body
// prettier-ignore
const err = new Error(`Request failed with${statusMessage ? `: ${statusCode} - ${statusMessage}` : ` status ${statusCode}`}`)
err.request = { method: options.method, url, headers: options.headers, body: data }
if (err.request.headers.authorization)
err.request.headers.authorization = err.request.headers.authorization.split(' ')[0] + ' ***'
err.response = { statusCode, statusMessage, headers: res.headers, body }
reject(err)
} else {
resolve(body)
Expand Down

0 comments on commit 6d05fa3

Please sign in to comment.