Skip to content

feat: add stable error codes to errors thrown by Express - #6982

Open
gayanMatch wants to merge 4 commits into
expressjs:masterfrom
gayanMatch:feat/explicit-error-code
Open

gayanMatch wants to merge 4 commits into
expressjs:masterfrom
gayanMatch:feat/explicit-error-code

Conversation

@gayanMatch

@gayanMatch gayanMatch commented Jan 10, 2026 •

Copy link
Copy Markdown

Errors thrown by Express now carry a stable code property so error handlers can branch on the code instead of matching message text. Implements the proposal in #6951. Original implementation by @gayanMatch.

What's included

  • lib/errors.js with one constant per error and factory helpers used at every throw site
  • express.errorCodes, a frozen object with all 15 codes
  • express.isExpressError(err), true only for errors carrying one of these codes
  • Tests for every code and for the helper
  • History.md entry listing each code and what throws it

Decisions

  • Codes are prefixed EXPRESS_ERR_ so they do not collide with Node.js's ERR_* namespace
  • Error types and messages are unchanged: instanceof TypeError and RangeError checks keep working, so this is semver-minor
  • No custom error class. isExpressError() covers the "did Express throw this" question without changing instanceof behaviour

Follow-ups

Refs #6951

gayanMatch and others added 4 commits January 9, 2026 15:57
Rename the 15 codes from ERR_* to EXPRESS_ERR_* so they do not collide
with Node.js's own ERR_* namespace, and add a History.md entry listing
every code and what throws it. Messages and error types are unchanged.

Refs expressjs#6951
Returns true only for errors carrying one of the codes in
express.errorCodes, so handlers can tell framework errors apart from
application errors without a custom error class.

Refs expressjs#6951
@UlisesGascon UlisesGascon self-assigned this Oct 23, 2026
@UlisesGascon UlisesGascon added semver-minor This change is a semver minor 5.x labels Oct 23, 2026
@UlisesGascon UlisesGascon changed the title feat: add standardized error codes for Express errors feat: add stable error codes to errors thrown by Express Oct 23, 2026
@UlisesGascon

UlisesGascon commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

If we want to wait for express@6 also is an option

@UlisesGascon

Copy link
Copy Markdown
Member

@gayanMatch awesome work with this PR!

I just pushed a72565c to include a prefix and 685d941 to include the express.isExpressError() helper and solved conflicts as the PR was a bit old.

@krzysdz

krzysdz commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

I have two questions after a quick look at the code:

  1. There are 3 functions that are identical, except for the specific Error subclass. Are there any advantages to this solution over passing the type as an argument1?
    function createError(type, code, message) {
        const error = new type(message);
        // ...
  2. Error.captureStackTrace is not standard and originates from V8, but according to browser-compat-data it should be supported by all platforms that can run Express. Does it make sense to guard it behind if?

By the way #6951 is tagged as 6.x and this PR as 5.x .

Footnotes

  1. in languages with better type systems I'd probably do this as a template parameter, but this is JS... ↩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

5.x discuss semver-minor This change is a semver minor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants