Error Handler Either
Sep 25, 2022
You probably already threw an exception and then forgot to handle it at the, crucial point, and all your code went down the drain. In this article, I will teach you how to handle errors in a better way for large-scale applications without worrying about handling them.
Error Handler Either
An Either is basically a container for a value that can be success or error. With an Either, we can apply transformations to the contained value without having to worry if it is an error or not until we reach a point in our code where we want to handle the error if it has occurred.
The Either can receive two values as mentioned above Left
and Right
, and in the case of Left
, they would be errors and the Right
a success
value (Either = Error | Success
). After understanding this, let's move on to the code and it is important to note that I will use TypeScript and that
the folder names are irrelevant to you.
If you are a TypeScript developer for some time, you have noticed the generics. If you don't know what it is, study it first to make it easier to understand. I will assume you already know what they are generics.
In the LeftType
, I will place the type(s) of error that I expect, and in the RightType
, the possible success cases. So let's continue and now create the Left
and Right
classes.
Create the two missing classes and in these, I have already passed the same types that they received in the Either
type, and in the constructor
of each one, they receive a value with the type of the value that the class represents.
Well, until here, this doesn't say anything to us, so let's create two methods in the two classes isLeft
and isRight
. We will use these methods to know if the return of a function
is a case of error or success.
In our Right
class, the isRight
method returns true
because if we are in the Right
class, it means that it is a success case, and the isLeft
method returns false
because if there is a success case, it means that there was no error. I did the opposite in the Left
class.
Probably the typing has left you confused, but this is to ensure that TypeScript understands the type of the value that the classes receive, "it is as if we were recovering the lost values".
Now, to be able to use this in our application, let's create two functions left
and right
. Note that the names are lowercase because we already have classes with these same names.
And these, as soon as I call the left function, I pass the error and in the same function it returns the methods of the Left class, and if you remember, I returned true in the isLeft method, which means that when verifying isLeft, I will have the boolean value true and in isRight the opposite value in this case, false. The same goes for the right function. now let's test it.
Development of a simple application using Either.
To do this, create a very simple example of an API without a framework or library. Start by creating a usecases folder and in it a user creation file:
In the return typing of the execute
method, I pass it as a Promise
and use the Either
by passing the global Error
interface in the LeftType
part and the User type I created in the RightType
part. We could make custom errors, which would be even better, but this is enough to test it.
Note that where I should throw an exception, I return the left
function that creates an instance of our Left
class (returns isLeft
as true), and in the end, I return the right
function that creates an instance of our Right
class.
In the controller, usually express typing is used, but I used the any
type to make it easier, but after instantiating the CreateUserUseCase
class, I execute the execute
method passing the destructed parameters in the request.
To check for errors, remember that we return left
or right
, because in these same returns, we have the instances of the Left
or Right
classes with the isLeft
and isRight
methods that return a boolean. With that in mind, we can check if there was an error using the isLeft
method. If it is true
, it means that it entered the Left
class. If it is false
, we already know that it is a success case, and we will have the value, which is a variable depending on the value
of isLeft
and isRight
, it can be error
or user
. Let's go to the service code...
Notice that we did not handle the errors with try catch
. We just execute the Promise, and for that, I used an IIFE (Immediately Invoked Function Expression), which is a function in JavaScript that is executed as soon as it is defined.
And in this case, I expect an error because in the application, the email is mandatory. To start, let's install ts-node
to run in a TypeScript
file.
Now we can test and see the result:
Now let's remove the comment on the email and test to see if it returns the user with id 0
:
Let's go to the terminal and run it again: