> ## 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.

# Customize UI exceptions

> How to display custom error messages in the Atoti Sign-Off UI by implementing a Spring exception handler that returns a structured error response with action and stacktrace fields.

To do this, return a response type in the structure of a [ProblemDetail](https://datatracker.ietf.org/doc/html/rfc7807) object.

## Steps

1. Create a class annotated with `@RestControllerAdvice` or `@ControllerAdvice` and import it into your main Spring configuration class.
2. Add a method in this class annotated with `@ExceptionHandler`. The `@ExceptionHandler` annotation should contain the classes of exceptions you’d like to handle.
3. Make that method return a `ProblemDetail` object.
4. Throw your desired exception from within the application. A `ProblemDetail` response should be returned which our UI will parse.

## How it works

An example of how you can do this might look as follows:

```java theme={"languages":{"custom":["/engine/python-sdk/0.9/languages/pycon.tmLanguage.json"]}}
@RestControllerAdvice
public class RestExceptionHandlerControllerAdvice {

   @ExceptionHandler({ MyCustomException.class })
   ProblemDetail handleRestServiceException(MyCustomException ex) {
      ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(ex.getHttpStatus(), ex.getMessage());
      problemDetail.setProperty("action", ex.getAction());
      problemDetail.setProperty("stacktrace", ExceptionUtils.getStackTrace(ex));
      return problemDetail;
   }
}
```

This simple class is annotated with `@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 exception `MyCustomException` 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, which is displayed by the UI.
* `stacktrace`: a string that contains the stack trace of the exception. This is not displayed by the UI but can be useful for debugging via the network tab in your browser’s developer tools.

<Note>
  The `@ExceptionHandler` annotation accepts multiple exception classes for a single method and will catch any exceptions that inherit from the specified exception.
</Note>

For more server-side information, see the [Spring Framework Documentation](https://docs.spring.io/spring-framework/reference/):

* [Error responses](https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-ann-rest-exceptions.)
* [ProblemDetail](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/ProblemDetail.)
* [ControllerAdvice](https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc#global-exception-handling)
