Class ExceptionController<C>
- Type Parameters:
C- the command sender type
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidRemoves all registered handlers.<T extends Throwable>
voidhandleException(@NonNull CommandContext<C> commandContext, @NonNull T exception) Attempts to handle the givenexceptiongracefully.<T extends Throwable>
@NonNull ExceptionController<C> register(@NonNull TypeToken<T> exceptionType, @NonNull ExceptionHandlerRegistration.BuilderDecorator<C, T> decorator) Decorates a registration builder and registers the result.<T extends Throwable>
@NonNull ExceptionController<C> register(@NonNull Class<T> exceptionType, @NonNull ExceptionHandlerRegistration.BuilderDecorator<C, T> decorator) Decorates a registration builder and registers the result.<T extends Throwable>
@NonNull ExceptionController<C> register(@NonNull ExceptionHandlerRegistration<C, ? extends T> registration) Registers the givenregistration.<T extends Throwable>
@NonNull ExceptionController<C> registerHandler(@NonNull TypeToken<T> exceptionType, @NonNull ExceptionHandler<C, ? extends T> exceptionHandler) Registers the givenexceptionHandler.<T extends Throwable>
@NonNull ExceptionController<C> registerHandler(@NonNull Class<T> exceptionType, @NonNull ExceptionHandler<C, ? extends T> exceptionHandler) Registers the givenexceptionHandler.unwrapCompletionException(@NonNull Throwable throwable) Unwraps aCompletionExceptionrecursively until a cause is encountered that is not a completion exception.
-
Constructor Details
-
ExceptionController
public ExceptionController()Creates a new exception controller.
-
-
Method Details
-
unwrapCompletionException
Unwraps aCompletionExceptionrecursively until a cause is encountered that is not a completion exception.- Parameters:
throwable- the throwable- Returns:
- the original cause
-
handleException
public <T extends Throwable> void handleException(@NonNull CommandContext<C> commandContext, @NonNull T exception) throws Throwable Attempts to handle the givenexceptiongracefully.The controller will attempt to find exception handlers for the exception type, and any of its supertypes. Only one exception handler will get to fully handle the exception.
If no exception handler was able to handle the exception, the exception will be re-thrown. The thrown exception might be different from the initial exception in the case that any of the exception handlers throws a new exception.
- Type Parameters:
T- the exception type- Parameters:
commandContext- the command contextexception- the exception- Throws:
Throwable- any exception left unhandled is re-thrown and should be handled by the caller
-
register
public <T extends Throwable> @NonNull ExceptionController<C> register(@NonNull ExceptionHandlerRegistration<C, ? extends T> registration) Registers the givenregistration.The ordering matters when multiple handlers are registered for the same exception type. The last registered handler will get priority.
- Type Parameters:
T- the exception type handled by the exception handler- Parameters:
registration- the exception handler registration- Returns:
thisexception controller
-
register
public <T extends Throwable> @NonNull ExceptionController<C> register(@NonNull TypeToken<T> exceptionType, @NonNull ExceptionHandlerRegistration.BuilderDecorator<C, T> decorator) Decorates a registration builder and registers the result.The ordering matters when multiple handlers are registered for the same exception type. The last registered handler will get priority.
- Type Parameters:
T- the exception type handled by the exception handler- Parameters:
exceptionType- the exception type handled by the exception handlerdecorator- the builder decorator- Returns:
thisexception controller
-
register
public <T extends Throwable> @NonNull ExceptionController<C> register(@NonNull Class<T> exceptionType, @NonNull ExceptionHandlerRegistration.BuilderDecorator<C, T> decorator) Decorates a registration builder and registers the result.The ordering matters when multiple handlers are registered for the same exception type. The last registered handler will get priority.
- Type Parameters:
T- the exception type handled by the exception handler- Parameters:
exceptionType- the exception type handled by the exception handlerdecorator- the builder decorator- Returns:
thisexception controller
-
registerHandler
public <T extends Throwable> @NonNull ExceptionController<C> registerHandler(@NonNull TypeToken<T> exceptionType, @NonNull ExceptionHandler<C, ? extends T> exceptionHandler) Registers the givenexceptionHandler.The ordering matters when multiple handlers are registered for the same exception type. The last registered handler will get priority.
- Type Parameters:
T- the exception type handled by the exception handler- Parameters:
exceptionType- the exception type handled by the exception handlerexceptionHandler- the exception handler- Returns:
thisexception controller
-
registerHandler
public <T extends Throwable> @NonNull ExceptionController<C> registerHandler(@NonNull Class<T> exceptionType, @NonNull ExceptionHandler<C, ? extends T> exceptionHandler) Registers the givenexceptionHandler.The ordering matters when multiple handlers are registered for the same exception type. The last registered handler will get priority.
- Type Parameters:
T- the exception type handled by the exception handler- Parameters:
exceptionType- the exception type handled by the exception handlerexceptionHandler- the exception handler- Returns:
thisexception controller
-
clearHandlers
public void clearHandlers()Removes all registered handlers.This can be used to make sure that no default handlers of higher precision are invoked before your handler.
It is recommended that you register a handler for
Throwableif you use this, to make sure that no uncaught errors leak out of the controller.
-