You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Error Handling with FirebaseError - Inconsistence #347
When I catch the error raised by FirebaseError, I find some inconsistence on how the errors are handled and provided.
For Example using an already registered account ( let's say jendoe@gmail.com) in my project, I find:
Creating a user
I use auth.create_user with the right parameters, and I get an error with:
print(str(error)) -> Error while calling Auth service (EMAIL_EXISTS).
print(error.code) -> INVALID_ARGUMENT
print(error.cause) -> 400 Client Error: Bad Request for url: https://identitytoolkit.googleapis.com/v1/projects/xxxxxx/accounts
print(type(error)) -> <class 'firebase_admin.exceptions.InvalidArgumentError'>
print(error.http_response.json()) -> {'error': {'code': 400, 'message': 'EMAIL_EXISTS', 'errors': [{'message': 'EMAIL_EXISTS', 'domain': 'global', 'reason': 'invalid'}]}}
Getting a user by email
I use auth.get_user_by_email with the right parameter, and I get an error with:
print(str(error)) -> No user record found for the provided email: jendoe@gmail.com.
print(error.code) -> NOT_FOUND
print(error.cause) -> None
print(type(error)) -> <class 'firebase_admin._auth_utils.UserNotFoundError'>
print(error.http_response.json()) -> {'kind': 'identitytoolkit#GetAccountInfoResponse'}
the two error are kind of inconsistent in my opinion. There should be consistence in the fields, in order to be consistent with the overall idea of having a global FirebaseError in the exception.
In particular, the create method has the old format, and I cannot understand where the 'message': 'EMAIL_EXISTS' comes from (I looked the whole source code).
Would be nice if all the errors (Exception) would follow the same structure, this would allow to perform many things in an easy way without polluting the code in an app.
Thanks for reporting this. There are still a few error codes that should be handled properly, and EMAIL_EXISTS is one of them. We need to define a mapping for it so that you get the auth.EmailAlreadyExistsError instead. I can get this change included in the next release.
@hiranya911 Thanks I checked what you did and it's perfect.
However, and that probably is my fault, I am still confuse by the inconsistence between the two error returned specifically the line with error.http_response.json().
Why the two structure are different? I mean why one has error as key with message etc in the response, and the other has kind ?
Would not be better having each response returning the same structure and just change the value of the structure?
Why the two structure are different? I mean why one has error as key with message etc in the response, and the other has kind ?
This is because the Auth backend API doesn't actually consider user-not-found to be an error. When you query the backend API with a non-existing UID, it actually sends back a 200 OK response. SDK intercepts this, and raises the correct auth.UserNotFoundError. As a result cause is set to None (since there was no backend/requests exception to set), and http_response is set to the payload sent by the backend.
On the other hand backend does send back 400 Bad Request error for email-exists. As a result we can populate the cause with the associated requests.RequestException, and the http_response is set to the error message sent by the server.
Does that make sense? In general you should write all your error handing logic around code, message and the exception type. cause and http_response are for more advanced error handling scenarios, and I don't see many developers wanting to use them. And if you do, always check if they are set first. Both properties are allowed to be None.
I do handle my code as you suggested, my gimmick with http_response was just to get out a message. The create method was spitting out a weird one, and I believe you just corrected its behavior with what you fixed for next release.
Maybe adding a message parameter directly inside of error would be useful (you can just call error.message as you do with error.cause etc). I know the message is already there and str(error) gives it, but having in the same format would help in jsonify it or in modifying it (actually it could be really cool if you could customize the message).
Activity
google-oss-bot commentedon Sep 13, 2019
I found a few problems with this issue:
hiranya911 commentedon Sep 13, 2019
Thanks for reporting this. There are still a few error codes that should be handled properly, and EMAIL_EXISTS is one of them. We need to define a mapping for it so that you get the
auth.EmailAlreadyExistsErrorinstead. I can get this change included in the next release.MojoDeMedici commentedon Sep 13, 2019
@hiranya911 Thanks I checked what you did and it's perfect.
However, and that probably is my fault, I am still confuse by the inconsistence between the two error returned specifically the line with error.http_response.json().
Why the two structure are different? I mean why one has error as key with message etc in the response, and the other has kind ?
Would not be better having each response returning the same structure and just change the value of the structure?
hiranya911 commentedon Sep 13, 2019
This is because the Auth backend API doesn't actually consider user-not-found to be an error. When you query the backend API with a non-existing UID, it actually sends back a 200 OK response. SDK intercepts this, and raises the correct
auth.UserNotFoundError. As a resultcauseis set toNone(since there was no backend/requests exception to set), andhttp_responseis set to the payload sent by the backend.On the other hand backend does send back 400 Bad Request error for email-exists. As a result we can populate the
causewith the associatedrequests.RequestException, and thehttp_responseis set to the error message sent by the server.Does that make sense? In general you should write all your error handing logic around
code,messageand the exception type.causeandhttp_responseare for more advanced error handling scenarios, and I don't see many developers wanting to use them. And if you do, always check if they are set first. Both properties are allowed to beNone.MojoDeMedici commentedon Sep 13, 2019
@hiranya911 Thanks! Now it makes sense.
I do handle my code as you suggested, my gimmick with http_response was just to get out a message. The create method was spitting out a weird one, and I believe you just corrected its behavior with what you fixed for next release.
Maybe adding a message parameter directly inside of error would be useful (you can just call error.message as you do with error.cause etc). I know the message is already there and str(error) gives it, but having in the same format would help in jsonify it or in modifying it (actually it could be really cool if you could customize the message).
Thanks!