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

# Adding Custom UI Exceptions

Atoti Market Risk allows you to display custom error messages in the UI.

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

## Steps

1. Add a method annotated with `@ExceptionHandler` in a class annotated with `@RestControllerAdvice` or `@ControllerAdvice`. The `@ExceptionHandler` annotation should contain the classes of exceptions you’d like to handle.
2. Make that method return a `ProblemDetail` object
3. Throw your desired exception from within the application. Note that most common exceptions are already handled in `GlobalExceptionHandlerController`.

## How it works

For an example of how we do this on the server side in Atoti Market Risk see `GlobalExceptionHandlerController`, which looks as follows:

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

    @ExceptionHandler(MRServiceException.class)
    ResponseEntity<ProblemDetail> handleMRServiceException(MRServiceException ex) {
        ProblemDetail problemDetail = buildProblemDetailWithStackTrace(ex, HttpStatus.valueOf(ex.getHttpStatus().value()));
        problemDetail.setProperties(ex.getDetailProperties());
        return buildResponseEntity(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 `GlobalExceptionHandlerController` catches the generic `MRServiceException` using the `@ExceptionHandler` annotation and returns a `ProblemDetail` object, which our UI will parse appropriately.

The `ProblemDetail` object accepts a map of variables which may hold custom fields of interest. Note that the `@ExceptionHandler` annotation accepts multiple
classes of exception for a single method.

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)
