fix: ctx.assert() now throws errors that are instanceof HttpError#1937
Open
mibragimov wants to merge 2 commits intokoajs:masterfrom
Open
fix: ctx.assert() now throws errors that are instanceof HttpError#1937mibragimov wants to merge 2 commits intokoajs:masterfrom
mibragimov wants to merge 2 commits intokoajs:masterfrom
Conversation
Fixes koajs#1925 ctx.assert() was delegated to http-assert, which bundles its own copy of http-errors ~1.8.0. Because koa depends on http-errors ^2.0.0, npm installs two separate versions, giving them distinct class prototypes. As a result, errors thrown by ctx.assert() failed instanceof HttpError even though they looked identical. Replace the http-assert dependency with an inline assert() helper that creates errors through the same createError (http-errors ^2.0.0) instance used by ctx.throw() and re-exported as koa.HttpError. The assert API is preserved: ctx.assert(value, status[, msg[, opts]]) ctx.assert.equal / notEqual / strictEqual / notStrictEqual ctx.assert.deepEqual / notDeepEqual (now uses util.isDeepStrictEqual) ctx.assert.fail / ok
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1937 +/- ##
=======================================
Coverage 99.90% 99.90%
=======================================
Files 9 9
Lines 2094 2141 +47
=======================================
+ Hits 2092 2139 +47
Misses 2 2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Also fix assert() to handle undefined msg/opts correctly so all named methods (assert.equal, assert.ok, assert.strictEqual, etc.) produce errors with the correct status code.
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.
Problem
ctx.assert()was delegated entirely to the http-assert package, which bundles its own copy ofhttp-errors ~1.8.0. Because koa depends onhttp-errors ^2.0.0, npm installs two separate versions with distinct class prototypes. As a result, errors thrown byctx.assert()fail aninstanceof HttpErrorcheck even though the errors look identical:Fixes #1925
Solution
Replace the
http-assertdependency with an inlineassert()helper insidelib/context.jsthat creates HTTP errors through the samecreateErrorfunction (fromhttp-errors ^2.0.0) used byctx.throw()and re-exported askoa.HttpError.The full
http-assertAPI is preserved:ctx.assert(value, status[, msg[, opts]])ctx.assert.equal/notEqual/strictEqual/notStrictEqualctx.assert.deepEqual/notDeepEqual— now usesutil.isDeepStrictEqual()(available since Node 9, koa requires Node ≥ 18)ctx.assert.fail/okAfter this fix
Tests
Added a new test case in
__tests__/context/assert.test.jsthat asserts the thrown error isinstanceof HttpError. All 430 existing tests continue to pass.