I'm working on an app with quarkus and I'm trying to create an ExceptionMapper to get rid of HTML errors when invoking rest services. It works ok in dev mode or in fat jars but not in native mode:
This is my exception mapper
@Provider
//@RegisterForReflection -> even with this it doesn't work
public class DefaultErrorMapper implements ExceptionMapper<Exception> {
@Override
public Response toResponse(Exception exception) {
int code = 500;
if (exception instanceof WebApplicationException) {
code = ((WebApplicationException) exception).getResponse().getStatus();
}
exception.printStackTrace();
return Response.status(code)
.entity(Json.createObjectBuilder().add("error", "unknown exception").add("code", code).build())
.build();
}
}
This is my resource service
@POST
@Transactional
public Response addMyEntity(MyEntity myEntity) {
try {
service.addMyEntity(myEntity);
return Response.ok().build();
} catch (FormularioInvalidoException e) {
return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
} catch (Exception e) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Unknown error").build();
}
}
When I call the POST in dev mode or from a fat jar built with "./mvnw package -Dquarkus.package.type=uber-jar" and forcing an error it works ok and it shows the following
{
"error": {
"chars": "Unknown error",
"string": "Unknown error",
"valueType": "STRING"
},
"code": {
"integral": true,
"valueType": "NUMBER"
}
}
But when running in native mode
<html lang="en">
<head>
<title>Internal Server Error - Error id c81bed52-1d75-46e9-917d-310dda5d3212-2</title>
<meta charset="utf-8">
... more html ...
So i need to get the error response instead of the HTML in native mode whenever there is an error. Is there something else i should configure?
thanks!