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 @RestControllerAdvice pods
  • 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:

  1. Cache Lookup: Checks cached adviser-method mappings for the exception type
  2. Adviser Discovery: Finds appropriate controller advice pods if not cached
  3. Method Selection: Identifies the best matching exception handler method
  4. Argument Resolution: Prepares method arguments using the argument resolver
  5. Method Invocation: Invokes the exception handler method with resolved arguments
  6. Result Processing: Handles the return value using the return value handler
  7. 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 Class of 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:

  1. AnnotationAwareOrderComparator: Respects @Order and @Priority annotations
  2. PackageOrderComparator: 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

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 ApplicationContext that this component runs in.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited