Skip to content
Advertisement

Firebase – How to extend FirebaseError?

I am implementing a cloud function for signing up with unique username and password.

In order to throw exceptions, I was previously doing the following:

signUpValidation.js

JavaScript

signUp.function.js

JavaScript

But, I don’t really like this way of throwing the exceptions in the signUpValidation module… it makes more sense to me to throw “AuthErrors” instead of “HttpsErrors”.

So, as it seems not possible to extend the default Firebase Errors, I have decided to create my own util/authErrors module:

JavaScript

as you can see, I have created my custom Error and some factory functions for every error type. Then, in my signUpValidation.js, I just do:

JavaScript

Is it possible to extend a FirebaseError? If not, why?

Is it considered a bad practice to work this way in order to throw custom exceptions in Cloud Functions? I mean, should I just throw HttpsErrors?

Advertisement

Answer

Having Custom Error Type is useful if you are going to treat it differently. For example if you have a try/catch block and want to have a different logic for your custom error. but here you are passing error to client which has no idea of either Firebase HttpsError or your custom AuthError. because at the end your object will be serialized to JSON and at the other end there is no class to convert it back to HttpsError or AuthError.

Also at HTTP protocol level, authentication errors are defined by HTTP status codes (e.g. 401 ,403) so they are not inherently different object types. What I’m saying is that I don’t see any advantage in having a custom AuthError class on your server side when it can not be transformed as it is to your client to be treated differently. For client the HTTP status code is the key to differentiate an Auth error from other type of errors.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement