How to parse FirebaseAuthException exception message (Admin SDK Java)? - java

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"
}
]
}
}

Related

Can I extend App Engine Standard error JSON in Java?

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.

App engine endpoint warning

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.

using google api explorer to upload data to google cloud storage

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.

How to change swagger response for exception

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.

Google Drive SDK Update sharing permissions to private

I like to update the permission of a document from anyone with the link to private, to set the permission to (anyone with the link) use this and works:
newPermission.setType("anyone");
newPermission.setWithLink(true);
newPermission.setRole("reader");
but trying to set the permission to private is not working, i'm using this :
newPermission.setType("user");
newPermission.setValue("me");
newPermission.setRole("owner");
the response is this
500 Internal Server Error
- Show headers -
{
"error": {
"errors": [
{
"domain": "global",
"reason": "internalError",
"message": "Internal Error"
}
],
"code": 500,
"message": "Internal Error"
}
}
What are the parameters that i have to pass to set the permissions to private ??
Are you the owner of the document? If not, you can not set yourself as owner. If you are, then the list of permissions for the document will have two entries, one for you as owner and another one to make everyone else a reader. Find the latter and delete it, and you should be all set.

Categories