Package studio.mevera.imperat
Interface BaseThrowableHandler<S extends CommandSource>
- Type Parameters:
S- the type of command source that provides context for exception handling, must extendCommandSource
- All Superinterfaces:
ThrowableHandler<S>
- All Known Subinterfaces:
Command<S>,ImperatConfig<S>
public non-sealed interface BaseThrowableHandler<S extends CommandSource>
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:
- Traverses the entire exception cause chain from the root exception to the deepest cause
- Prioritizes
SelfHandlingExceptioninstances for immediate self-resolution - Falls back to registered
CommandExceptionHandlerinstances for standard exceptions - Provides detailed debugging information throughout the resolution process
- 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. SelfHandlingException.handle() - immediate self-resolution
2. Registered CommandExceptionHandler 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 CommandExceptionHandler<?, CommandSource> getThrowableResolver(Class<?> exceptionType) {
return resolvers.get(exceptionType);
}
}
- Since:
- 1.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault <E extends Throwable>
booleanhandleExecutionError(E throwable, CommandContext<S> context, Class<?> owning, String methodName) Handles exceptions that occur during command execution by traversing the exception cause chain and applying appropriate resolution strategies.Methods inherited from interface studio.mevera.imperat.ThrowableHandler
getErrorHandlerFor, setErrorHandler
-
Method Details
-
handleExecutionError
default <E extends Throwable> boolean handleExecutionError(@NotNull E throwable, CommandContext<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:
- Chain Traversal: Iterates through the complete exception
cause chain using
Throwable.getCause() - Self-Handled Exceptions: Immediately delegates to
SelfHandlingException.handle(CommandContext)when encountered - Resolver Lookup: Searches for registered
CommandExceptionHandlerinstances matching the current exception type - Debug Logging: Provides detailed logging of the resolution
process through
ImperatDebugger - Fallback Logging: Records unhandled exceptions for debugging
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 { // RootCommand execution } catch (CommandPermissionException e) { // 1. Check if CommandPermissionException is SelfHandlingException → No // 2. Look for CommandExceptionHandler<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.getErrorHandlerFor(Class)implementation is thread-safe.- Specified by:
handleExecutionErrorin interfaceThrowableHandler<S extends CommandSource>- Parameters:
throwable- the root exception that occurred during command execution, may benull(though this typically indicates a logic error)context- the execution context containing the command source, configuration, and other relevant state information, nevernullowning- the class where the exception originated, used for debugging and error logging purposes, may benullmethodName- the name of the method where the exception occurred, used for debugging and error logging, may benull- Returns:
- the unhandled exception if no handler was found, or null if the exception was handled successfully
- See Also:
- Chain Traversal: Iterates through the complete exception
cause chain using
-