Auth Error Classification#
Auth error classification describes how Strapi translates thrown auth-related errors (UnauthorizedError, ForbiddenError, PolicyError, etc.) into HTTP responses. When this pipeline works correctly, intentional 401/403 errors surface to API clients; when it breaks — most commonly due to cross-package instanceof failures — those errors silently become 500 Internal Server Errors.
Error Class Hierarchy#
All auth error types live in @strapi/utils → errors.ts. The relevant portion of the class tree:
ApplicationError
├── ForbiddenError → HTTP 403
│ └── PolicyError → HTTP 403
└── UnauthorizedError → HTTP 401
ForbiddenError: thrown when a user is authenticated but lacks permission. Default message:"Forbidden access".UnauthorizedError: thrown when the user is not authenticated. Default message:"Unauthorized".PolicyError: extendsForbiddenError; used in route policies, exposes a custom message through the public API (unlike plainForbiddenError).
Request Pipeline#
Every request passes through a fixed middleware stack composed in compose-endpoint.ts:
authenticate → authorize → policies → plugin middlewares → controller
Authenticate (createAuthenticateMiddleware) delegates to the active auth strategy's authenticate().
Authorize (createAuthorizeMiddleware) wraps authService.verify() and converts errors to Koa responses:
UnauthorizedError→ctx.unauthorized()(HTTP 401)ForbiddenError→ctx.forbidden()(HTTP 403), unless it is aPolicyError, which is re-thrown so its message is visible to the caller- Any other error → re-thrown upstream
Global error middleware (errors.ts middleware) is the last catch-all:
ApplicationError→formatApplicationError(), which maps the class to the correct HTTP status.HttpError→formatHttpError().- Anything else →
formatInternalError(), i.e., HTTP 500.
The status mapping table in services/errors.ts drives formatApplicationError:
| Error class | HTTP status |
|---|---|
UnauthorizedError | 401 |
ForbiddenError | 403 |
NotFoundError | 404 |
PayloadTooLargeError | 413 |
RateLimitError | 429 |
NotImplementedError | 501 |
(anything else ApplicationError) | 400 |
users-permissions Strategy#
The users-permissions auth strategy implements both halves:
authenticate(): resolves the JWT, fetches the user, builds a CASL ability from role permissions. Returns{ authenticated: false }or{ error: 'Invalid credentials' }for invalid/blocked/unconfirmed users — it does not throw. Any unexpected error is swallowed and returns{ authenticated: false }.verify(): throwsUnauthorizedErrorwhen no ability exists or the route has no scope and the user is anonymous ; throwsForbiddenErrorwhen the user's ability doesn't cover the required scope .
The Cross-Package instanceof Problem#
Root cause: Node.js module resolution may give each package (e.g., a plugin bundled separately) its own copy of @strapi/utils. Errors thrown by a plugin's copy of ApplicationError fail instanceof errors.ApplicationError in core's error middleware — core sees them as unrecognized errors and returns HTTP 500.
This is tracked in GitHub issue #22488 and fixed in PR #27178.
Fix (PR #27178): Two mechanisms were introduced :
- Symbol branding —
ApplicationErrorinstances are stamped withSymbol.for('strapi.ApplicationError')at construction time. - Prototype-walking fallback —
isApplicationError(error)checks for the brand first, then walks the prototype chain looking for anApplicationErrorancestor with adetailsproperty (handles errors from older, unbranded plugin copies). isErrorOfType(error, errorType)— generalizes prototype inspection for subclass status mapping, ensuringPolicyError/ForbiddenErrorfrom a foreign module copy still resolve to HTTP 403 rather than 400.
The error middleware's instanceof errors.ApplicationError check was replaced by errors.isApplicationError(error) .
Key Source Files#
| File | Role |
|---|---|
packages/core/utils/src/errors.ts | Error class definitions and (post-fix) isApplicationError / isErrorOfType helpers |
packages/core/core/src/middlewares/errors.ts | Global Koa error middleware — top-level catch handler |
packages/core/core/src/services/errors.ts | formatApplicationError, status mapping table |
packages/core/core/src/services/server/compose-endpoint.ts | Route pipeline — authorize middleware |
packages/plugins/users-permissions/server/src/strategies/users-permissions.js | Auth strategy: authenticate() + verify() |