I am using spring boot with swagger for REST API services. The swagger returns the response in the following format when exception occurs
{
"timestamp": 1449742584285,
"status": 500,
"error": "Internal Server Error",
"exception": "com.foo.exception.NotFoundException",
"message": "User with id 1 not found",
"path": "/user/1"
}
Instead of returning the detailed response I want to show only the message. Also all the time the server tkhrows an RuntimeException.
How to achieve swagger to retun only the message.
Related
I'm using Firebase Admin SDK Java API v6.12.2.
I call FirebaseAuth.getInstance().generatePasswordResetLink(email, actionCodeSettings) to generate a password reset link for users. If the email isn't registered, I get a big blob of text with embedded JSON from e.getMessage().
The looked at the FirebaseAuthException doc and it only exposes one method e.getErrorCode(), which in this case returns internal-error.
I can certainly parse this text to look for "EMAIL_NOT_FOUND" and translate it into a user-friendly message. But isn't that a very clumsy error message? At least, there should have been methods to return the error code 400, and a simple String message, and the details could go into a JSON object.
What is the recommended approach here by the Firebase team and how are other developers handling it?
Output of e.getMessage():
Unexpected HTTP response with status: 400; body: {
"error": {
"code": 400,
"message": "EMAIL_NOT_FOUND",
"errors": [
{
"message": "EMAIL_NOT_FOUND",
"domain": "global",
"reason": "invalid"
}
]
}
}
App Engine manages what's returned as error JSON on Cloud Endpoints. When I throw ServiceException which Google supplies, I pass it statusCode and statusMessage:
public ServiceException(int statusCode, String statusMessage) {
super(statusMessage);
this.statusCode = statusCode;
this.reason = null;
this.domain = null;
}
App Engine then returns this JSON to the caller in the error response:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "The statusMessage I gave it."
}
],
"code": 401,
"message": "The statusMessage I gave it."
}
}
I would like to extend the response with an extra field, e.g. for an application error code which tells the client what went wrong (message should be human readable).
I'm not so deep into Java servlets and internal workings of this, but I have tried to catch the response and modify it, which worked for a 200 response, but not for an error response. Any ideas?
Based on what you are requesting, I would suggest you take a look at how the ServiceException class works on the Java endpoints-framework.
Here you can check how the exceptions are being handled and, based on this, extend the error JSON that is returned to suit your needs, that is, that anyone can understand why his run failed.
When I check my logs on my GAE app, I can see every so often a warning message like that:
com.google.api.control.Client flushAndScheduleReports: direct send of a report request failed because of endpoints.repackaged.com.google.api.client.http.HttpResponseException: 400 (Client.java:354)
{
"error": {
"code": 400,
"message": "Precondition check failed.",
"errors": [
{
"message": "Precondition check failed.",
"domain": "global",
"reason": "failedPrecondition"
}
],
"status": "FAILED_PRECONDITION"
}
}
However it seems the client app works as expected and I don't understand what it means.
If you have a low traffic API, this typically will happen if too long goes in between requests. The app will work as expected, but metric reporting may be off. This is because metrics are aggregated and reported every so many requests, unless you're running a backend instance.
i am trying to use google api explorer to first try to insert an object to google cloud storage.
the request looks like
POST https://www.googleapis.com/storage/v1/b/visionapibucket/o?key={YOUR_API_KEY}
{
"contentType": "image/jpeg",
"uploadType": "media",
"path": "/upload/storage/v1/b/visionapibucket/o"
}
but i see the error as
400 HTTP/2.0 400
- Show headers -
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Required"
},
{
"domain": "global",
"reason": "wrongUrlForUpload",
"message": "Upload requests must include an uploadType URL parameter and a URL path beginning with /upload/",
"extendedHelp": "https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload"
}
],
"code": 400,
"message": "Required"
}
}
not sure what i am missing. please advise
Looks like a bug on the website. It doesn't seem like the explorer supports media.
The request it generated looks like:
POST https://www.googleapis.com/storage/v1/b/visionapibucket/o?key={YOUR_API_KEY}
But a proper upload request would look like:
POST https://www.googleapis.com/upload/storage/v1/b/visionapibucket/o?key={YOUR_API_KEY}&uploadType=media&name=myfile.jpeg
You'll also want to include a "Content-Type" header specifying that it's a JPEG image.
There's a guide on the various ways to upload objects using the JSON API here. The specific type you're looking for is like a simple upload.
Sifting through forums I can't seem to stumble into an issue solution I've been haveing with Spring Boot + JPA and JSR303 bean validation.
Simply put validation works but but when say
org.springframework.web.bind.MethodArgumentNotValidException
is thrown and then the output a consumer gets (from a rest controller) is something like this:
"timestamp": 1427658793929,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.bind.MethodArgumentNotValidException",
"message": "Validation failed for argument at index 1 in method: public org.springframework.http.HttpEntity<org.springframework.hateoas.Resource<org.home.project.UserInfo>>... {omitted for brevity}
This is overkill verbosity - can I intercept this to make the "message:" part of JSON more formatted and user friendly (e.g.) produce a simple list saying:
> - "messages:" { "firstName can not be Empty or Null or has to be at least 8 characters",
"lastName is a swear word...", ... }