JHipster: How to display customized error messages to users

Viewed 795

I am using JHipster v6.6.0 and I want to display customized error messages to users.

This question has been asked before and answered by using the class CustomParameterizedException.

But I can not find this class in the JHipster version that I am using.

Has the class been removed? And if so, what can be used instead?

1 Answers

The class CustomParameterizedException was removed in PR#9624 in favor of the Problem library.

Quoting the documentation on Managing Server Errors:

To handle Spring MVC REST errors, JHipster uses Zalando’s Problem Spring Web library, in order to provide rich, JSON-based error messages.

You can achieve the same custom error messages as before with less effort and more customization options.

import org.zalando.problem.Problem;
import org.zalando.problem.Status;
...
throw Problem.builder()
             .withStatus(Status.BAD_REQUEST)
             .with("param1", "value 1")
             .with("param2", "value 2")
             .build();

If you notice, this is how JHipster handles errors in base components (users, accounts, authorization).

Check the ExceptionTranslator.java class for examples and guidance. A good place to start would be handleMethodArgumentNotValid() I think, but my experience with the Problem library is very limited.

Related