Skip to main content

Error Handling

Prism provides a structured approach to errors: throw typed errors from handlers, and middleware catches them and converts them to consistent JSON responses.

Quick Start

Throw and Catch
The client receives:
with HTTP status 404.

Built-in Error Types

PrismAppError provides factory methods for common HTTP errors:
Error Factories

Custom Error Codes

Custom Codes
Response:

Error Middleware

PrismErrorMiddleware catches all errors and converts them to JSON:
Unknown errors return:

Custom Error Handler

Intercept specific errors before the default handler:
Custom Handler

Custom Error Types

Create domain-specific errors by conforming to PrismHTTPErrorResponse:
Custom Error Type

Problem Details (RFC 7807)

For standards-compliant error responses, use PrismProblemDetails:
RFC 7807
Response with Content-Type: application/problem+json:

Combining with Validation

Validation + Error Handling
Always put PrismErrorMiddleware as one of the first middleware in your stack. If it’s added after other middleware, errors thrown in those middleware won’t be caught.

Validation

Validate request data before processing.

Lifecycle Hooks

Custom error hooks for logging and monitoring.