I am developing a web application using Java and Spring Boot.
Specifically, I am reviewing some code written by other developers. I use Postman to make HTTP calls to my application.
There are cases where my application needs to inform the caller of certain situations. In this example, the developer of the application threw a JwtTokenException
to the caller in case of IOException
.
try {
myToken = methodThatObtainsAToken(tokenInput);
}catch(IOException ex) {
throw new JwtTokenException("Error 401", JwtStatusResponse.JWT_NOT_VALID); // TODO: cambiare
}
When something goes wrong here is what happens on POSTMAN:
I have so many other very similar situations in this code and I have to do the following thing: I have to replace the code throw new JwtTokenException
so that it throws exceptions that make the caller understand (so I can see them with Postman) that an HTTP error has occurred (with some code).
In the example I wrote I wrote "Error 401" only as a string. There are other places in the code where I use "Error 500", "Error 302" and so on.. But these are just information strings, and do not correspond to the real error thrown to the client. How do I throw correct exceptions that raise "HTTP errors"?