firebase / firebase-admin-go

Firebase Admin Go SDK
Apache License 2.0
1.13k stars 242 forks source link

Improve error handling #465

Closed andrefedev closed 2 years ago

andrefedev commented 2 years ago

To improve the library, I would like to propose that the errors thrown instead of spitting a flat error could return a more explicit error depending on the function that is being consumed, here is an example:


u, err := firebase.auth.GetUserByPhoneNumber(ctx, phone)
// If there is a user registered with the phone number, this will be the result of the error. How to parse this?
http error status: 400; body: {
  "error": {
    "code": 400,
    "message": "PHONE_NUMBER_EXISTS",
    "errors": [
      {
        "message": "PHONE_NUMBER_EXISTS",
        "domain": "global",
        "reason": "invalid"
      }
    ]
  }
}

// I would like to see better documentation or can we handle errors this way?
u, err := firebase.auth.GetUserByPhoneNumber(ctx, phone)
if err != nil {
    if err == auth.ErrUserNotFound { ... }
    if err == auth.ErrPhoneNumberExists { ... }
}
// Or If we want to validate if the entered number is already registered, we will return an error, but otherwise we want to continue and create the user.
if err == auth.ErrUserNotFound {
    auth.CreateUser(....)
}

I know that you can wrap the output to return something custom but for newcomers and not being familiar with all the errors that you would have to handle with firebase auth and other components, there should be either better documentation or a general way to handle all the errors without letting any escape.

google-oss-bot commented 2 years ago

I found a few problems with this issue:

andrefedev commented 2 years ago

I just found that there is a version of firebase 4 that has an errorutils.go module that does exactly what I wanted to find in this great library, you guys do more than an excellent job, thanks.