Skip to content

Error Handling with FirebaseError - Inconsistence #347

Closed

Description

@MojoDeMedici

I am using the new sdk version 3.0

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.

What do you think @hiranya911 ??

Thanks for the great work...

Activity

  1. google-oss-bot commented on Sep 13, 2019

    @google-oss-bot

    I found a few problems with this issue:

    • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
    • This issue does not seem to follow the issue template. Make sure you provide all the required information.
  2. hiranya911 commented on Sep 13, 2019

    @hiranya911
    Contributor

    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.

  3. self-assigned this
    on Sep 13, 2019
  4. MojoDeMedici commented on Sep 13, 2019

    @MojoDeMedici
    Author

    @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?

  5. hiranya911 commented on Sep 13, 2019

    @hiranya911
    Contributor

    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.

  6. MojoDeMedici commented on Sep 13, 2019

    @MojoDeMedici
    Author

    @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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions