To do this, return a response type in the structure of a ProblemDetail object.Documentation Index
Fetch the complete documentation index at: https://docs.activeviam.com/llms.txt
Use this file to discover all available pages before exploring further.
Steps
- Add a method annotated with
@ExceptionHandlerin a class annotated with@RestControllerAdviceor@ControllerAdvice. The@ExceptionHandlerannotation should contain the classes of exceptions you’d like to handle. - Make that method return a
ProblemDetailobject - Throw your desired exception from within the application. Note that most common exceptions are already handled in
RestExceptionHandlerControllerAdviceandLimitsProcessExceptionHandler.
How it works
For an example of how we do this on the server side in Atoti Limits seeRestExceptionHandlerControllerAdvice, which looks as follows:
@RestControllerAdvice, meaning it will catch exceptions thrown from REST methods. In reality, it inherits from the global
exception handler @ControllerAdvice, which handles exceptions thrown from any method.
The single method in RestExceptionHandlerControllerAdvice catches the generic service exception LimitsServiceException using the @ExceptionHandler annotation and returns a ProblemDetail object, which our UI will parse appropriately.
The ProblemDetail object accepts a map of variables that may hold custom fields of interest. For example, we supply the following fields:
action: a string you can use to suggest a course of action to the user, displayed by the UI.stacktrace: a string that contains the stack trace of the exception, not displayed by the UI but may be useful for debugging.
The
@ExceptionHandler annotation accepts multiple classes of exception for a single method.