Interface BaseThrowableHandler<S extends Source>

Type Parameters:
S - the type of command source that provides context for exception handling, must extend Source
All Superinterfaces:
ThrowableHandler<S>
All Known Subinterfaces:
Command<S>, ImperatConfig<S>

public non-sealed interface BaseThrowableHandler<S extends Source> extends ThrowableHandler<S>
A base implementation of ThrowableHandler that provides intelligent exception chain traversal and resolution logic. This interface serves as a foundation for command execution error handling in the Imperat framework.

This handler implements a comprehensive exception resolution strategy that:

  1. Traverses the entire exception cause chain from the root exception to the deepest cause
  2. Prioritizes SelfHandledException instances for immediate self-resolution
  3. Falls back to registered ThrowableResolver instances for standard exceptions
  4. Provides detailed debugging information throughout the resolution process
  5. Logs unhandled exceptions for debugging purposes

The non-sealed modifier allows for further extension while maintaining the base exception handling contract.

Exception Resolution Priority:


 1. SelfHandledException.handle() - immediate self-resolution
 2. Registered ThrowableResolver for the specific exception type
 3. Traverse to exception.getCause() and repeat
 4. Log error if no handler found in the entire chain
 

Example implementation:


 public class CommandExceptionHandler implements BaseThrowableHandler<CommandSource> {

     @Override
     public ThrowableResolver<?, CommandSource> getThrowableResolver(Class<?> exceptionType) {
         return resolvers.get(exceptionType);
     }
 }
 
Since:
1.0
See Also:
  • Method Details

    • handleExecutionThrowable

      default <E extends Throwable> boolean handleExecutionThrowable(@NotNull E throwable, Context<S> context, Class<?> owning, String methodName)
      Handles exceptions that occur during command execution by traversing the exception cause chain and applying appropriate resolution strategies.

      This method implements a sophisticated exception handling workflow:

      The method terminates early upon successful resolution, ensuring that only the most appropriate handler processes each exception. If no suitable handler is found in the entire cause chain, the original exception is logged as an error.

      Resolution Flow Example:

      
       try {
           // Command execution
       } catch (CommandPermissionException e) {
           // 1. Check if CommandPermissionException is SelfHandledException → No
           // 2. Look for ThrowableResolver<CommandPermissionException> → Found
           // 3. Call resolver.resolve(e, context) → Success, return
       } catch (WrappedException e) {
           // 1. Check WrappedException → No resolver
           // 2. Check e.getCause() (CommandSyntaxException) → Found resolver
           // 3. Resolve and return
       }
       

      Thread Safety: This method is thread-safe assuming the underlying ThrowableHandler.getThrowableResolver(Class) implementation is thread-safe.

      Specified by:
      handleExecutionThrowable in interface ThrowableHandler<S extends Source>
      Parameters:
      throwable - the root exception that occurred during command execution, may be null (though this typically indicates a logic error)
      context - the execution context containing the command source, configuration, and other relevant state information, never null
      owning - the class where the exception originated, used for debugging and error logging purposes, may be null
      methodName - the name of the method where the exception occurred, used for debugging and error logging, may be null
      Returns:
      whether the exception got handled or not.
      See Also: