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?
Asked
Active
Viewed 2,318 times
0
-
1We have no idea of which framework you're using. There are many, many of them. So, find out what you're using, then read the documentation of the framework. It probably contains the information. – JB Nizet Mar 28 '18 at 18:51
-
Framework as in angularjs? – P_equals_NP_2021 Mar 28 '18 at 18:54
-
No. AngularJS is a JavaScript framework, used at client-side. You're using a Java framework, at server-side. – JB Nizet Mar 28 '18 at 18:55
-
Ohh sorry. We're using Maven. – P_equals_NP_2021 Mar 28 '18 at 18:58
-
Yeah, but that is completely irrelevant. Maven is a build tool. Not a Java web framework. If nobody in your team is able to even name the framework you chose to implement your app, how can you even work? Do you guys never read any documentation? – JB Nizet Mar 28 '18 at 19:11
-
Related: https://stackoverflow.com/questions/25546848/rest-api-is-it-a-really-bad-practice-to-create-custom-http-response-codes. For your purposes specifically, you should be using an error code in the body instead rather than a new status code. – Vasan Mar 28 '18 at 19:48
1 Answers
1
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);
}
}

Christophe L
- 13,725
- 6
- 33
- 33