Error Class Identity#
When a plugin bundles its own copy of @strapi/utils, its ApplicationError constructor is a different object from the one loaded by @strapi/core. JavaScript's instanceof operator compares constructor identity, not shape ā so instanceof errors.ApplicationError returns false for errors thrown by the plugin, even though both objects are structurally identical. The error middleware treats unrecognized errors as unhandled exceptions and responds with 500 Internal Server Error instead of the intended 4xx status.
This was reported in issue #22488: any error thrown in a plugin's middleware or policy above the global error middleware would surface as a 500 regardless of the error type thrown .
Error class hierarchy#
All Strapi domain errors extend ApplicationError in packages/core/utils/src/errors.ts. The notable subclasses are :
| Class | Default status | Extends |
|---|---|---|
ValidationError | 400 | ApplicationError |
NotFoundError | 404 | ApplicationError |
ForbiddenError | 403 | ApplicationError |
UnauthorizedError | 401 | ApplicationError |
PolicyError | 403 | ForbiddenError |
RateLimitError | 429 | ApplicationError |
PayloadTooLargeError | 413 | ApplicationError |
NotImplementedError | 501 | ApplicationError |
Where instanceof checks happen#
Two places perform the identity-sensitive checks:
-
Global error middleware ā
packages/core/core/src/middlewares/errors.ts: catches all unhandled errors and checkserror instanceof errors.ApplicationError(anderrors.HttpError) to decide on status/body formatting. A failed check falls through to the "log + 500" branch. -
Authorize middleware ā
packages/core/core/src/services/server/compose-endpoint.ts: checksinstanceof errors.UnauthorizedErrorandinstanceof errors.ForbiddenErrorto map auth errors toctx.unauthorized()/ctx.forbidden(). Plugin-sourced errors fail these checks and are re-thrown as unhandled exceptions.
A concrete instance: the users-permissions plugin imports ForbiddenError and UnauthorizedError directly from its own @strapi/utils copy and throws them from its verify() function . When the authorize middleware catches those throws, instanceof errors.ForbiddenError is false, so the error is re-thrown to the global error middleware ā which also fails its instanceof check ā and a 500 is returned.
Fix: symbol branding + prototype-chain fallback (PR #27178)#
PR #27178 addresses issue #22488 . The fix has two layers:
1. Process-wide symbol brand on ApplicationError
A Symbol.for('strapi.ApplicationError') marker is stamped onto every new ApplicationError instance as a non-enumerable property. Symbol.for returns the same symbol regardless of which module copy called it, so the brand survives cross-copy boundaries .
2. Prototype-chain fallback for legacy (unbranded) errors
Older plugin copies compiled before the brand was added won't have the marker. A helper ā isApplicationError(error) ā walks the prototype chain looking for a constructor named "ApplicationError" and verifies the details property exists. This keeps unbranded errors from older plugin versions recognized correctly .
3. instanceof replaced by helper functions throughout
packages/core/core/src/middlewares/errors.ts replaces error instanceof errors.ApplicationError with errors.isApplicationError(error). The status-mapping service (packages/core/core/src/services/errors.ts) similarly switches to errors.isErrorOfType(error, ErrorClass) to preserve correct status codes for subclasses like PolicyError .
Regression tests verify four cases: a cross-copy ApplicationError (expects 400), a legacy unbranded error (expects 400), a custom PolicyError subclass (expects 403), and a third-party lookalike (expects opaque 500 to avoid information leakage) .
Workaround (before fix is available)#
Import error classes from the same resolved path as Strapi core ā i.e., ensure @strapi/utils is declared as a peerDependency in your plugin's package.json, not as a direct dependency. This lets the host application's package manager deduplicate the module so only one constructor exists at runtime. See the Monorepo Module Resolution KB article for the broader context on module deduplication in Strapi's package architecture .