ExceptionHandler not mapping the body message of the exception in micronaut

79 Views Asked by At

From the API gateway using the micronaut declarative HTTP client as below

@Client(id="fetebirdProductApi", path = "/tag")
public interface ITagClient extends ITagOperation {
}

In one of the micro-service, there is a validation error on the controller and it is throwing a BAD Request with a message in the body as below

@Override
public Mono<MutableHttpResponse<?>> post(@Valid TagDto model) {
    LOG.info("Creating a new tag");
    return _iServiceBus.<TagCreateCommand, Mono<Result<TagDto>>>send(new TagCreateCommand(model)).flatMap(item -> {
                if (Result.isSuccess.get()) {
                    try {
                        return Mono.just(HttpResponse.created(item.value(), new URI(String.format("/%s", item.value().id()))));
                    } catch (URISyntaxException e) {
                        return Mono.error(new GlobalException(e));
                    }
                } else
                    return Mono.just(HttpResponse.badRequest(item.exception().getMessage()));
            }
    );

enter image description here

In the API Gateway I am handling the HTTP response as below, it does catch the HTTP Bad Request, however the message is not be mapped

@Singleton
public class ClientResponseExceptionHandler implements ExceptionHandler<HttpClientResponseException, MutableHttpResponse<?>>{
    private static final Logger LOG = LoggerFactory.getLogger(ClientResponseExceptionHandler.class);

    @Override
    public MutableHttpResponse<?> handle(HttpRequest request, HttpClientResponseException exception) {
        final HttpResponse<?> response = exception.getResponse();
        if (response.status().getCode() < 500) {
            final JsonError error = new JsonError(exception.getMessage());
            error.link(Link.SELF, request.getPath());
            return HttpResponse.status(
                    response.status(),
                    response.reason()
            ).body(error);
        } else {
            if (LOG.isErrorEnabled()) {
                LOG.error("Service responded with error " + exception.getMessage(), exception);
            }
            final JsonError error = new JsonError("API currently unavailable due to error");
            error.link(Link.SELF, request.getPath());
            return HttpResponse.serverError(error);
        }
    }
}

enter image description here

0

There are 0 best solutions below