Interface ThrowableHandler<S extends Source>
- Type Parameters:
S- The valueType extendingSourcethat acts as the source of a command.
- All Known Subinterfaces:
BaseThrowableHandler<S>,Command<S>,ImperatConfig<S>
ThrowableHandler interface defines a mechanism for managing and resolving
throwable instances within a given context. It provides methods to retrieve specific
resolvers for throwable types and handle execution throwable with detailed context
information.-
Method Summary
Modifier and TypeMethodDescription<T extends Throwable>
@Nullable ThrowableResolver<T,S> getThrowableResolver(Class<T> exception) Retrieves theThrowableResolverresponsible for handling the specified valueType of throwable.<E extends Throwable>
booleanhandleExecutionThrowable(E throwable, Context<S> context, Class<?> owning, String methodName) Handles a given throwable by finding the appropriate exception handler or using a default handling strategy if no specific handler is found.<T extends Throwable>
voidsetThrowableResolver(Class<T> exception, ThrowableResolver<T, S> resolver) Registers aThrowableResolverfor a specific exception type and returns the registered resolver instance.
-
Method Details
-
setThrowableResolver
<T extends Throwable> void setThrowableResolver(Class<T> exception, ThrowableResolver<T, S> resolver) Registers aThrowableResolverfor a specific exception type and returns the registered resolver instance. This method allows for fluent configuration of exception handling strategies while providing access to the registered resolver.The registered resolver will be invoked when exceptions of a specified type (or its subtypes, depending on implementation) occur during command execution. This method enables type-safe registration where the exception class and resolver generic types must match.
Example usage:
ThrowableResolver<CommandPermissionException, CommandSource> permissionResolver = setThrowableResolver(CommandPermissionException.class, (exception, context) -> { context.source().sendMessage("§cYou don't have permission: " + exception.getPermission()); // Additional logging or cleanup }); // Can be used for method chaining or further configuration permissionResolver.andThen(someOtherAction);Type Safety: The generic constraint ensures that only resolvers capable of handling the specified exception type can be registered, preventing runtime
ClassCastExceptions during exception resolution.Resolver Precedence: If a resolver already exists for the given exception type, this method will typically replace it. Refer to the implementation documentation for specific behavior regarding resolver conflicts.
Inheritance Behavior: Depending on the implementation, the resolver may handle subclasses of the specified exception type. Check the concrete implementation's documentation for exact inheritance semantics.
- Type Parameters:
T- the specific exception type that the resolver will handle, must extendThrowable- Parameters:
exception- theClassobject representing the exception type to register a resolver for, must not benullresolver- theThrowableResolverimplementation that will handle exceptions to the specified type, must not benull- Throws:
IllegalArgumentException- if eitherexceptionorresolverisnullUnsupportedOperationException- if resolver registration is not supported for the given exception type- Since:
- 1.0
- See Also:
-
getThrowableResolver
@Nullable <T extends Throwable> @Nullable ThrowableResolver<T,S> getThrowableResolver(Class<T> exception) Retrieves theThrowableResolverresponsible for handling the specified valueType of throwable. If no specific resolver is found, it may return null or a default resolver.- Type Parameters:
T- The valueType of the throwable.- Parameters:
exception- The class of the throwable to get the resolver for.- Returns:
- The
ThrowableResolvercapable of handling the throwable of the specified valueType, or null if no specific resolver is registered.
-
handleExecutionThrowable
<E extends Throwable> boolean handleExecutionThrowable(E throwable, Context<S> context, Class<?> owning, String methodName) Handles a given throwable by finding the appropriate exception handler or using a default handling strategy if no specific handler is found.- Parameters:
throwable- The throwable to be handled, which may be an exception or error.context- The context in which the throwable occurred, providing necessary information about the state and source where the exception happened.owning- The class where the throwable originated, used for logging and debugging purposes.methodName- The name of the method where the throwable was thrown, used for logging and debugging.- Returns:
- Whether the error got handled or not
-