fix: ctx.assert() throws HttpError instances (#1925)#1942
Open
guoyangzhen wants to merge 1 commit intokoajs:masterfrom
Open
fix: ctx.assert() throws HttpError instances (#1925)#1942guoyangzhen wants to merge 1 commit intokoajs:masterfrom
guoyangzhen wants to merge 1 commit intokoajs:masterfrom
Conversation
Wrap http-assert to re-throw errors using koa's own http-errors, so instanceof HttpError checks work correctly. Fixes koajs#1925
Author
|
Hi, just checking in on this PR. Happy to make any changes if needed. Thanks! |
Author
|
Hi! Any chance this could get a review? The fix is straightforward and tests pass. Happy to adjust if needed. |
guoyangzhen
commented
Apr 2, 2026
Author
guoyangzhen
left a comment
There was a problem hiding this comment.
The CI failure is because http-errors@2.0.1's createError(status, msg, opts) throws a TypeError when opts is undefined.
The fix for wrappedAssert:
function wrappedAssert (value, status, msg, opts) {
try {
httpAssert(value, status, msg, opts)
} catch (err) {
if (err instanceof HttpError) {
throw err
}
const args = [err.status || err.statusCode || 500, err.message || msg]
if (opts) args.push(opts)
throw createError(...args)
}
}The issue: when ctx.assert(false, 404, 'custom message') is called without a 4th arg, opts is undefined, and createError(404, msg, undefined) throws TypeError: argument #3 unsupported type undefined instead of creating the HttpError.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1925
ctx.assert()throws errors fromhttp-assert's ownhttp-errorsdependency, not koa's. This meanserr instanceof HttpErrorreturnsfalsewhen catching errors fromctx.assert(), even though the JSDoc says it throwsHttpError.Root Cause
http-assertimports its own copy ofhttp-errorsin itsnode_modules. Even though both use the same package version, they're separate module instances, soinstanceoffails.Fix
Wrap
http-assertincontext.jsto catch thrown errors and re-throw them using koa's owncreateError. This ensures:err instanceof HttpErrorworks correctlyhttp-assertsub-methods (equal, deepEqual, etc.) are preservedExample