RestExceptionResolver class
Resolves exceptions using @RestControllerAdvice annotated pods.
Overview
The RestExceptionResolver provides a sophisticated exception handling mechanism that leverages controller advice pods to handle exceptions in a centralized, declarative manner. It scans for advice pods, caches their exception handling methods, and orchestrates the resolution process.
Key Responsibilities
- Advice Discovery: Automatically finds all
@RestControllerAdvicepods - Method Resolution: Identifies appropriate exception handler methods using MethodExceptionAdviser
- Argument Resolution: Uses MethodArgumentResolver to prepare method arguments
- Return Value Handling: Uses ReturnValueHandlerManager to process results
- Performance Optimization: Caches adviser-method mappings for efficient exception resolution
Exception Resolution Flow
When an exception occurs:
- Cache Lookup: Checks cached adviser-method mappings for the exception type
- Adviser Discovery: Finds appropriate controller advice pods if not cached
- Method Selection: Identifies the best matching exception handler method
- Argument Resolution: Prepares method arguments using the argument resolver
- Method Invocation: Invokes the exception handler method with resolved arguments
- Result Processing: Handles the return value using the return value handler
- Context Update: Updates the handler context with resolved arguments
Caching Strategy
The resolver maintains a cache mapping exception types to adviser-method pairs:
- Key: The
Classof the exception being handled - Value: A MapEntry containing the ExceptionAdviser and
Method - Benefits: Avoids repeated reflection and scanning for common exceptions
- Eviction: Cache persists for the lifetime of the resolver
Ordering Strategy
Controller advice pods are ordered using:
AnnotationAwareOrderComparator: Respects@Orderand@PriorityannotationsPackageOrderComparator: Orders by package hierarchy and dependencies
Integration Example
@RestControllerAdvice
@Order(1)
class GlobalExceptionHandler {
@ExceptionHandler([NotFoundException])
ResponseBody<ErrorResponse> handleNotFound(NotFoundException ex) {
return ResponseBody.notFound(ErrorResponse(message: 'Resource not found'));
}
}
Performance Considerations
- Caching eliminates reflection overhead for repeated exceptions
- Advice pods are sorted once during initialization
- Method resolution uses efficient type matching algorithms
- Argument resolution leverages the shared argument resolver infrastructure
Thread Safety
- Cache operations are not synchronized (assumes initialization-time population)
- Advice pod list is immutable after initialization
- Method invocation is thread-safe for stateless advice pods
Related Components
- ExceptionAdviser: Wrapper for controller advice pod metadata
- MethodExceptionAdviser: Resolves exception handler methods
- MethodArgumentResolver: Resolves method arguments
- ReturnValueHandlerManager: Handles return values
- ExceptionResolver: Base exception resolution interface
Summary
The RestExceptionResolver provides a powerful, cache-optimized mechanism for centralized exception handling using controller advice pods, enabling clean separation of error handling logic from business logic.
- Implemented types
- Annotations
-
- @Order.new(Ordered.HIGHEST_PRECEDENCE)
Constructors
- RestExceptionResolver(MethodArgumentResolverManager _argumentResolver, ReturnValueHandlerManager _returnValueHandler)
- Creates a new RestExceptionResolver with required dependencies.
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
-
getPackageName(
) → String - Represents an abstraction for identifying the package that an object, resource, or service belongs to.
-
getSupportedMediaTypes(
) → List< MediaType> -
Returns the list of media types this handler supports by default.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onReady(
) → Future< void> - Interface to be implemented by pods that require initialization logic after their properties have been set by the container.
-
resolve(
ServerHttpRequest request, ServerHttpResponse response, HandlerMethod? handler, Object ex, StackTrace st) → Future< bool> -
Attempts to resolve an exception by generating an appropriate HTTP response.
override
-
setApplicationContext(
ApplicationContext applicationContext) → void -
Sets the
ApplicationContextthat this component runs in. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited