RestControllerExceptionHandler class

A centralized exception handling component for RESTful controllers in the JetLeaf Web framework.

The RestControllerExceptionHandler serves as the default global exception translator for REST APIs. It intercepts exceptions thrown by controller pods and converts them into standardized JSON or structured response bodies using ResponseBody.

Purpose

JetLeaf’s REST controller layer is designed for explicit exception-based flow control. Rather than returning raw error codes, controllers may throw typed exceptions (such as BadRequestException, NotFoundException, or ServiceUnavailableException).
The RestControllerExceptionHandler automatically maps these to valid HTTP responses, ensuring consistent structure and behavior across the application.

Each handler method is annotated with `@ExceptionHandler`, linking a specific exception type to a corresponding HTTP response. The returned ResponseBody object encapsulates:

  • the HTTP HttpStatus code,
  • an error description or diagnostic message,
  • optional details (metadata, validation errors, etc.),
  • and a timestamp for traceability.

Features

  • Consistent REST error format — all responses are standardized.
  • Automatic mapping of exceptions to HTTP status codes.
  • Extensible design — developers can subclass or replace this handler to customize error payloads or serialization rules.
  • Stateless & reusable — safe to use across multiple web requests.

Supported Exception Types

The following exception classes are supported by default:

Exception Type HTTP Status
BadRequestException 400 BAD REQUEST
UnauthorizedException 401 UNAUTHORIZED
ForbiddenException 403 FORBIDDEN
NotFoundException 404 NOT FOUND
MethodNotAllowedException 405 METHOD NOT ALLOWED
ConflictException 409 CONFLICT
PayloadTooLargeException 413 PAYLOAD TOO LARGE
UnsupportedMediaTypeException 415 UNSUPPORTED MEDIA TYPE
TooManyRequestsException 429 TOO MANY REQUESTS
ServiceUnavailableException 503 SERVICE UNAVAILABLE
BadGatewayException 502 BAD GATEWAY
GatewayTimeoutException 504 GATEWAY TIMEOUT
HttpException Generic HTTP error fallback

Example

@RestController()
class AccountController {
  @Get('/accounts/:id')
  Future<Account> getAccount(@PathVariable('id') String id) async {
    final account = await repository.findById(id);
    if (account == null) throw NotFoundException('Account not found');
    return account;
  }
}

If NotFoundException is thrown, this handler automatically returns:

{
  "error": "Not Found",
  "message": "Account not found",
  "timestamp": "2025-11-07T12:34:56.789Z"
}

Integration

The handler is automatically registered in JetLeaf’s web dispatcher pipeline. It applies only to REST controllers, ensuring that web controllers rendering views use ControllerExceptionHandler instead.

Annotations
  • @Order.new(Ordered.HIGHEST_PRECEDENCE)

Constructors

RestControllerExceptionHandler()
Creates a new instance of RestControllerExceptionHandler.
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, and toString.
handleBadGatewayException(ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, BadGatewayException exception) Future<ResponseBody<Object>>
Handles BadGatewayException by returning an HTTP 502 (Bad Gateway) response.
handleBadRequestException(ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, BadRequestException exception) Future<ResponseBody<Object>>
Handles BadRequestException by returning an HTTP 400 (Bad Request) response.
handleConflictException(ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, ConflictException exception) Future<ResponseBody<Object>>
Handles ConflictException by returning an HTTP 409 (Conflict) response.
handleForbiddenException(ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, ForbiddenException exception) Future<ResponseBody<Object>>
Handles ForbiddenException by returning an HTTP 403 (Forbidden) response.
handleGatewayTimeoutException(ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, GatewayTimeoutException exception) Future<ResponseBody<Object>>
Handles GatewayTimeoutException by returning an HTTP 504 (Gateway Timeout) response.
handleHttpException(ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, HttpException exception) Future<ResponseBody<Object>>
Handles generic HttpException instances by returning an appropriate HTTP response.
handleMethodNotAllowedException(ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, MethodNotAllowedException exception) Future<ResponseBody<Object>>
Handles MethodNotAllowedException by returning an HTTP 405 (Method Not Allowed) response.
handleNotFoundException(ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, NotFoundException exception) Future<ResponseBody<Object>>
Handles NotFoundException by returning an HTTP 404 (Not Found) response.
handlePayloadTooLargeException(ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, PayloadTooLargeException exception) Future<ResponseBody<Object>>
Handles PayloadTooLargeException by returning an HTTP 413 (Payload Too Large) response.
handleServiceUnavailableException(ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, ServiceUnavailableException exception) Future<ResponseBody<Object>>
Handles ServiceUnavailableException by returning an HTTP 503 (Service Unavailable) response.
handleTooManyRequestsException(ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, TooManyRequestsException exception) Future<ResponseBody<Object>>
Handles TooManyRequestsException by returning an HTTP 429 (Too Many Requests) response.
handleUnauthorizedException(ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, UnauthorizedException exception) Future<ResponseBody<Object>>
Handles UnauthorizedException by returning an HTTP 401 (Unauthorized) response.
handleUnsupportedMediaTypeException(ServerHttpRequest request, ServerHttpResponse response, WebRequest webRequest, UnsupportedMediaTypeException exception) Future<ResponseBody<Object>>
Handles UnsupportedMediaTypeException by returning an HTTP 415 (Unsupported Media Type) response.
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<RestControllerExceptionHandler>
Represents the Class metadata for RestControllerExceptionHandler.
final