Skip to content
Open
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
26 changes: 25 additions & 1 deletion lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,37 @@

const util = require('util')
const createError = require('http-errors')
const { HttpError } = require('http-errors')
const httpAssert = require('http-assert')
const delegate = require('delegates')
const statuses = require('statuses')
const Cookies = require('cookies')

const COOKIES = Symbol('context#cookies')

// Wrap http-assert to ensure thrown errors are instances of koa's HttpError.
// http-assert uses its own http-errors dependency, so instanceof checks fail.
// See: https://github.com/koajs/koa/issues/1925
function wrappedAssert (value, status, msg, opts) {
try {
httpAssert(value, status, msg, opts)
} catch (err) {
if (err instanceof HttpError) {
throw err
}
// Re-throw using koa's createError so instanceof HttpError works
throw createError(err.status || err.statusCode || 500, err.message || msg, opts)
}
}

wrappedAssert.fail = httpAssert.fail
wrappedAssert.equal = httpAssert.equal
wrappedAssert.notEqual = httpAssert.notEqual
wrappedAssert.ok = httpAssert.ok
wrappedAssert.strictEqual = httpAssert.strictEqual
wrappedAssert.notStrictEqual = httpAssert.notStrictEqual
wrappedAssert.deepEqual = httpAssert.deepEqual

/**
* Context prototype.
*/
Expand Down Expand Up @@ -69,7 +93,7 @@ const proto = module.exports = {
* @api public
*/

assert: httpAssert,
assert: wrappedAssert,

/**
* Throw an error with `status` (default 500) and
Expand Down
Loading