P_equals_NP_2021
P_equals_NP_2021

Reputation: 717

How do I create a custom HTTP error code in Java for an exception?

I'm running into the issue of having to differentiate between two error cases which both throw a Java BadrequestException, which shows up as having a 400 error code. I've extended BadrequestException to create a CustomException, but this Exception also throws a 400. Is there a way to have a custom exception throw an "unassigned" error code, like 469, for example? And how would I go about doing that?

Upvotes: 0

Views: 2681

Answers (1)

Christophe L
Christophe L

Reputation: 14035

Assuming you are using JAX-RS you can create a new exception with a custom HTTP status code like so:

public class CustomException extends WebApplicationException {
    private static final long serialVersionUID = 1L;

    public CustomException() {
        super(469);
    }
}

Upvotes: 1

Related Questions