Strategy interface for resolving exceptions that occur during HTTP request processing.
Overview
The ExceptionResolver defines a contract for components that handle exceptions thrown during the execution of HTTP request handlers. Implementations of this interface provide specialized exception resolution strategies that can transform exceptions into appropriate HTTP responses.
Key Responsibilities
- Exception Handling: Catch and process exceptions from handler method execution
- Response Generation: Create appropriate HTTP responses for different exception types
- Error Transformation: Convert technical exceptions to user-friendly error responses
- Resolution Coordination: Work with other resolvers in a chain of responsibility pattern
Resolution Process
When an exception occurs during request processing:
- Framework Detection: The framework catches exceptions from handler methods
- Resolver Chain: Invokes registered ExceptionResolver instances in order
- Resolution Attempt: Each resolver attempts to handle the exception
- First Match Wins: The first resolver that returns
truestops the chain - Fallback Handling: If no resolver handles the exception, default error handling applies
Implementation Patterns
Common resolver implementations include:
- ControllerAdviceExceptionResolver: Uses
@ControllerAdviceannotated pods - ResponseStatusExceptionResolver: Handles
@ResponseStatusannotated exceptions - DefaultExceptionResolver: Provides fallback exception handling
- Custom Business Exception Resolvers: Application-specific exception handling
Return Value Semantics
true: The exception was successfully resolved and an HTTP response was generatedfalse: The resolver could not handle this exception type, continue to next resolver
Example: Custom Exception Resolver
@Pod
class BusinessExceptionResolver implements ExceptionResolver {
@override
Future<bool> resolve(
ServerHttpRequest request,
ServerHttpResponse response,
HandlerMethod handler,
Object ex,
) async {
if (ex is BusinessException) {
response.setStatus(HttpStatus.BAD_REQUEST);
response.getBody().writeString('Business error: ${ex.message}');
return true;
}
return false;
}
}
Integration with Handler Pipeline
Exception resolvers integrate with the handler execution pipeline:
- Pre-handler: Exceptions from argument resolution
- During-handler: Exceptions from handler method execution
- Post-handler: Exceptions from return value handling
Thread Safety
Implementations should be thread-safe as they may be invoked concurrently by multiple request processing threads.
Error Page Integration
Resolvers can work with the ErrorPages registry to:
- Map exceptions to specific HTTP status codes
- Render appropriate error page templates
- Handle redirects for error scenarios
Best Practices
- Specificity: Handle specific exception types rather than generic exceptions
- Composition: Chain multiple specialized resolvers for comprehensive coverage
- Performance: Cache resolution logic for common exception types when possible
- Logging: Log exceptions appropriately before transforming to user responses
- Security: Avoid exposing sensitive exception details in production responses
Related Components
- HandlerMethod: The handler method that generated the exception
- ServerHttpRequest: The current HTTP request being processed
- ServerHttpResponse: The HTTP response to populate with error details
ControllerAdviceExceptionResolver: Resolver using controller advice pods- ErrorPages: Registry for error page configurations
Summary
The ExceptionResolver interface enables a flexible, extensible approach to exception handling in Jetleaf applications, supporting both framework-provided strategies and custom application-specific error handling logic.
- Implementers
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
-
getSupportedMediaTypes(
) → List< MediaType> - Returns the list of media types this handler supports by default.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
resolve(
ServerHttpRequest request, ServerHttpResponse response, HandlerMethod? handler, Object ex, StackTrace st) → Future< bool> - Attempts to resolve an exception by generating an appropriate HTTP response.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited