Handling Registration Errors

In this lesson, we'll learn how to generate custom errors.

It’s time to handle the errors when the registration fails. According to the documentation for the createUserWithEmailAndPassword method, Firebase will provide error codes as to why the registration failed. We’re going to keep things simple by creating one message to handle all errors from the request.

First, we need to tell Angular that there’s an error in our form. This was something taken care of for us when we used validators. There will be times where we’ll want to throw errors outside of a validator. We can throw errors using the setErrors method on form controls or form groups.

This will add an error object to the errors object on the control or group on which we’re calling it. Let’s try using it in the register method. Update the register.component.ts component class file to the following:

Get hands-on with 1400+ tech skills courses.