Skip to content
This repository has been archived by the owner on Nov 15, 2020. It is now read-only.

Boom 8 #40

Merged
merged 2 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { send } = require('micro')
const Boom = require('boom')
const Boom = require('@hapi/boom')

/**
* Catches error from an async function, wraps them
Expand All @@ -20,30 +20,30 @@ module.exports.handleErrors = function (fn, dump = false) {
}

// Determine status code in determined order
let status = res.statusCode || 500
if (err.isBoom) {
status = err.output.statusCode
let statusCode = res.statusCode || 500
if (Boom.isBoom(err)) {
statusCode = err.output.statusCode
} else if (err.statusCode) {
status = err.statusCode
statusCode = err.statusCode
}

// Since it's an error, it's safe to assume <400
// status codes are a mistake.
if (status < 400) {
status = 500
if (statusCode < 400) {
statusCode = 500
}

// Wrap the error and generate the response
const error = err.isBoom ? Boom.wrap(err) : Boom.wrap(err, status)
const error = Boom.isBoom(err) ? err : Boom.boomify(err, { statusCode })

// Add WWW-Authenticate challenge to headers for 401 responses
if (status === 401 && error.data && error.data.challenge) {
if (statusCode === 401 && error.data && error.data.challenge) {
res.setHeader('WWW-Authenticate', error.data.challenge)
}

send(
res,
status,
statusCode,
Object.assign({},
error.output.payload,
error.data && { data: error.data }
Expand All @@ -56,6 +56,11 @@ module.exports.handleErrors = function (fn, dump = false) {
/**
* Creates a Boom error.
*
* @type {@link module:Boom.create}
* @param {number} statusCode
* @param {string|Error} [message]
* @param {any} [data]
* @return {Boom}
*/
module.exports.createError = Boom.create
module.exports.createError = function (statusCode, message, data) {
return new Boom.Boom(message, { statusCode, data })
}
Loading