ControllerExceptionHandler class
A centralized exception handler for JetLeaf web controllers, responsible for transforming framework-level and domain-specific exceptions into structured, framework-consistent View responses.
The ControllerExceptionHandler provides a declarative mechanism for
handling HTTP and server-side exceptions via annotated methods using
the `@ExceptionHandler` meta-annotation.
Each handler method corresponds to a specific subclass of HttpException
or JetLeafException, ensuring graceful degradation and consistent
error presentation at the controller layer.
Purpose
This class decouples exception handling logic from application controllers
by providing prebuilt responses for the most common HTTP error conditions
(e.g. 400 Bad Request, 403 Forbidden, 404 Not Found, 500 Internal Server Error),
as well as JetLeaf-specific runtime exceptions.
It leverages ErrorPageUtils to render standardized error pages or JSON
responses, preserving request metadata such as:
- the failing request path,
- contextual error identifiers (
errorId), - request attributes or diagnostic details, and
- retry and content negotiation hints (for
Retry-After, etc.).
Integration
The handler is automatically discovered and registered by the JetLeaf
web context during the controller dispatch phase. When a controller method
throws a supported exception, the framework routes control to the matching
@ExceptionHandler-annotated method.
Example
@RestController()
class UserController {
final UserService service;
UserController(this.service);
@Get('/users/:id')
Future<User> getUser(@PathVariable('id') String id) async {
final user = await service.findById(id);
if (user == null) {
throw NotFoundException('User not found for ID: $id');
}
return user;
}
}
When the above controller throws a NotFoundException,
ControllerExceptionHandler.handleNotFoundException automatically
generates a 404 response view using ErrorPageUtils.notFound.
Supported Exception Types
- BadRequestException
- UnauthorizedException
- ForbiddenException
- MethodNotAllowedException
- ConflictException
- PayloadTooLargeException
- UnsupportedMediaTypeException
- TooManyRequestsException
- RequestTimeoutException
- ServiceUnavailableException
- GatewayTimeoutException
- BadGatewayException
- NotFoundException
- HttpException (fallback handler)
Each exception handler ensures HTTP semantics compliance while allowing customization of error messages, retry delays, and diagnostic payloads.
- Annotations
-
- @Order.new(Ordered.HIGHEST_PRECEDENCE)
Constructors
- ControllerExceptionHandler()
-
Creates a new immutable instance of ControllerExceptionHandler.
const
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
equalizedProperties(
) → List< Object?> -
Mixin-style contract for value-based equality,
hashCode, andtoString. -
handleBadGatewayException(
ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, BadGatewayException exception) → Future< View> -
Handles
BadGatewayExceptionby rendering an HTTP 502 (Bad Gateway) error view. -
handleBadRequestException(
ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, BadRequestException exception) → Future< View> -
Handles
BadRequestExceptionby rendering an HTTP 400 (Bad Request) error view. -
handleConflictException(
ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, ConflictException exception) → Future< View> -
Handles
ConflictExceptionby rendering an HTTP 409 (Conflict) error view. -
handleForbiddenException(
ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, ForbiddenException exception) → Future< View> -
Handles
ForbiddenExceptionby rendering an HTTP 403 (Forbidden) error view. -
handleGatewayTimeoutException(
ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, GatewayTimeoutException exception) → Future< View> -
Handles
GatewayTimeoutExceptionby rendering an HTTP 504 (Gateway Timeout) error view. -
handleHttpException(
ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, HttpException exception) → Future< View> -
Handles general
HttpExceptioninstances by rendering a generic error page corresponding to the provided HTTP status code. -
handleMethodNotAllowedException(
ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, MethodNotAllowedException exception) → Future< View> -
Handles
MethodNotAllowedExceptionby rendering an HTTP 405 (Method Not Allowed) error view. -
handleNotFoundException(
ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, NotFoundException exception) → Future< View> -
Handles
NotFoundExceptionby rendering an HTTP 404 (Not Found) error view. -
handlePayloadTooLargeException(
ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, PayloadTooLargeException exception) → Future< View> -
Handles
PayloadTooLargeExceptionby rendering an HTTP 413 (Payload Too Large) error view. -
handleRequestTimeoutException(
ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, RequestTimeoutException exception) → Future< View> -
Handles
RequestTimeoutExceptionby rendering an HTTP 408 (Request Timeout) error view. -
Handles
ServiceUnavailableExceptionby rendering an HTTP 503 (Service Unavailable) error view. -
handleTooManyRequestsException(
ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, TooManyRequestsException exception) → Future< View> -
Handles
TooManyRequestsExceptionby rendering an HTTP 429 (Too Many Requests) error view. -
Handles
UnauthorizedExceptionby rendering an HTTP 401 (Unauthorized) error view. -
handleUnsupportedMediaTypeException(
ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, UnsupportedMediaTypeException exception) → Future< View> -
Handles
UnsupportedMediaTypeExceptionby rendering an HTTP 415 (Unsupported Media Type) error view. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
-
CLASS
→ Class<
ControllerExceptionHandler> -
Represents the
Classmetadata for ControllerExceptionHandler.final