I need to throw a custom exception and pass only the exception message in the response, but I still need to see the full stack trace in the console. My exception is defined as:
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class CartNotFoundException extends RuntimeException {
public CartNotFoundException(String message) {
super(message);
}
}
And I throw the exceptions like:
final Cart cart = cartsRepository.findByUidAndUsername(cartUid, username);
if(cartModel == null) {
throw new CartNotFoundException("No cart found with uid: " + cartUid);
}
The json response that I get is in the format:
{
"timestamp": "2020-09-23T17:04:59.702+00:00",
"status": 400,
"error": "Bad Request",
"message": "No cart found with uid: 84b5305f-e6e2-4d34-a665-20f37b8edfe",
"path": "/carts/84b5305f-e6e2-4d34-a665-20f37b8edfe"
}
But the issue is I'm not able to see the full stack trace in the console, after adding the @ResponseStatus annotation on the custom exception. This is a sample of the exception logging strategy I'm planning to use, I would need the full stack trace to be logged in console logs, and the json response as shown here is sufficient. I have tried adding server.error.include-stacktrace=always in properties but this gives me the whole stack trace in the message field of the response.
Is there anyway to get just the message in response and the whole trace logged in console? Any way to achieve it using just annotations/property changes? (trying to avoid/reduce writing extra handlers/advices)