I have using oracle db. New data inserted in oracle using Java API and try to get the response from read end point using java API call. in DB one field name TRAN_OUTCOME="Success" will get the response. If TRAN_OUTCOME="Failure" will not get the response. failure case have throwing Exception. what was the Http Status error code want to return this scenario?
Throwing message format:
[
{
"Message": "Process associated with the Process ID has ended in failure. Kindly re-initiate the process.";
}
]
In case of Http Status codes, the general practice is
4XX: In case the processing failed as a result of some validation/condition is not satisfied by the client (calling service)
5XX: when some error occurred on server side
If there is any database exception which is not due to parameters sent by Client then you can return 500. If the data sent by client is causing some database constraint violation like Unique constraint etc. then you can return appropriate 4XX status code
There is also unconventional way where people send 2XX response but in the response data, they mention ERROR:TRUE etc. to indicate failure. But this way is not recommended.
The DefaultOAuth2ExceptionRenderer is throwing an InvalidMediaType exception when the request accept header is invalid. I need to send an appropriate response in json when this happens, however the content negotiation is falling back to sending out the stack trace even though an appropriate error message had been set on the response.
I just ended up adding a Filter earlier in the chain that attempts to parse the media type and if the invalid media type exception is thrown it responds with Http Status code 415.
could anybody help, Retrofit onFailure gets getKind error like HTTP,NETWORK and CONVERSION what these all mean..
Take a look here: http://static.javadoc.io/com.squareup.retrofit/retrofit/1.9.0/retrofit/RetrofitError.Kind.html
CONVERSION:
An exception was thrown while (de)serializing a body.
HTTP: A non-200 HTTP status code was received from the server.
NETWORK: An IOException occurred while communicating to the server.
UNEXPECTED: An internal error occurred while attempting to execute a request.
I have developed a REST service using Apache CXF and notice that if I send invalid characters in the URL, the CXF servlet throws back the following exception before it gets to my code:
Servlet failed with Exception
java.lang.IllegalArgumentException
at java.net.URI.create(URI.java:841)
at org.apache.cxf.transport.servlet.BaseUrlHelper.getBaseURL(BaseUrlHelper.java:49)
at org.apache.cxf.transport.servlet.ServletController.getBaseURL(ServletController.java:73)
at org.apache.cxf.transport.servlet.ServletController.updateDestination(ServletController.java:82)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:162)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:137)
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:158)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:239)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:164)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:215)
This results in a 500 status code being returned to the client.
I would ideally like to intercept this exception and return a 400 Bad Request status code to the client but I am unable to work out how to do this.
Any help much appreciated!
Many thanks
The way I did this is by means of an interceptor.
If an error has occurred during initial processing of a request, a framework response (and of course status code) will be put on the message pipe.
So you could for example write a interceptor on "marshall" phase, check if there already is a response and rewrite to your own response.
#Override
public void handleMessage(Message message) throws Fault {
if (message.getExchange().get(Response.class) != null) {
//build your response and put it on the exchange
message.getExchange().put(Response.class, yourResponse);
}
I am using Apache HttpClient and would like to communicate HTTP errors (400 Bad Request, 404 Not Found, 500 Server Error, etc.) via the Java exception mechanism to the calling code. Is there an exception in the Java standard library or in a widely used library that would be appropriate to use or to subclass for this purpose?
The alternative is to check status return codes. This appears to be the HttpClient design philosophy, but since these errors are truly exceptional in my app, I would like to have the stack trace and other nice exception things set up for me when they happen.
If it's not an Exception in HttpClient design philosophy, but an Exception in your code, then create your own Exception classes. ( As a subclass of org.apache.commons.httpclient.HttpException )
Quick answer
In Spring you have exactly what you want:
HttpClientErrorException - Exception thrown when an HTTP 4xx is received.
HttpServerErrorException - Exception thrown when an HTTP 5xx is received.
And a recommended practice
Minimally, you should differentiate exceptions related to business logic (e.g., insufficient balance, email address is not valid) from other exceptions (e.g., server not available, unsupported media type, SQLException).
In our REST API, we have a library for Java clients that parses responses and throws only three different exceptions:
400, 401, 403, 404, 409, 422: throw MyBusinessException, which contains a message that can be shown to the end user. The message comes in the response body (exception handling on the service side), but if not present we have a default message specific to each status code.
405, 412, 415: throw HttpClientErrorException with a message that is specific to each status code.
other 4xx codes: throw HttpClientErrorException with a generic message.
5xx codes: throw HttpServerErrorException with a generic message.
All these exceptions are unchecked.
Check out the page on Exception Handling for HttpClient
To answer your question though there appears to be an org.apache.commons.httpclient.HttpException class that is probably a good fit.
If you do need a custom exception class for this I would think java.io.IOException would be the correct super class to use.
I'd say this depends on what you are using the HTTPClient for. For example, the PayPal SDK uses HttpClient to transmit API calls to the PayPal server, but fails to check the HTTP response code when it's done. I patched my copy so that if the response code isn't 200 it throws a PayPal FatalException, with an appropriate message. That's because the caller isn't interested in the HTML or any of the details of this HTTP post, and isn't even interested in the fact that we're using HTTP as a transport. If the call is successful then the body of the response contains transaction details, which are extracted and placed into a response object; otherwise it contains HTML which is useless. HTTP is just the transport in this case, so certain response codes indicate errors which can be reported using exceptions. Because this is part of the PayPal SDK, I used a PayPal Exception class. In some other system or library, I'd use a subtype of whatever exceptions that library already uses. For example, if I were writing a GMail library, which accesses GMail accounts, I'd probably create a GMailException class, and subclass that for the different kinds of exceptions the library runs into. Alternatively, you can use something like IOException.
The reason HttpClient makes you check response codes is because the response may be useful even if the response code is not 200. Some websites put useful text on a 404 page, either providing something useful for the user to do, or a search form, or just a helpful error message. Depending on your use case you may want to just show the response content rather than throw an exception.
I found this exception on Apache HTTP Client 4.5.7:
...
import org.apache.http.client.HttpResponseException;
...
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() != 200) {
throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
}
...
The sample of result is:
Exception in thread "main" org.apache.http.client.HttpResponseException: status code: 400, reason phrase: Bad Request
There is org.apache.commons.httpclient.HttpException if you want a library exception. We have also sometimes created our own for specific purposes, both creating an exception for specific HTTP status codes and a generic one for and unexpected status code.