Package studio.mevera.imperat
Interface BaseThrowableHandler<S extends Source>
- Type Parameters:
S- the type of command source that provides context for exception handling, must extendSource
- All Superinterfaces:
ThrowableHandler<S>
- All Known Subinterfaces:
Command<S>,ImperatConfig<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
SelfHandledExceptioninstances for immediate self-resolution - Falls back to registered
ThrowableResolverinstances 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. 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 Summary
Modifier and TypeMethodDescriptiondefault <E extends Throwable>
booleanhandleExecutionThrowable(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.Methods inherited from interface studio.mevera.imperat.ThrowableHandler
getThrowableResolver, setThrowableResolver
-
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:
- Chain Traversal: Iterates through the complete exception
cause chain using
Throwable.getCause() - Self-Handled Exceptions: Immediately delegates to
SelfHandledException.handle(ImperatConfig, Context)when encountered - Resolver Lookup: Searches for registered
ThrowableResolverinstances 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 { // 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:
handleExecutionThrowablein interfaceThrowableHandler<S extends Source>- 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:
- whether the exception got handled or not.
- See Also:
- Chain Traversal: Iterates through the complete exception
cause chain using
-