Skip to content

Error Management Functional

Samuel Villaescusa Vinader edited this page Jul 17, 2020 · 4 revisions

Description

First of all, I am using ArrowKT as functional library to be able to achieve the Functional Approach on the error management.

I am using this approach on the Error management because of two main points:

  1. Error logic should not be managed during the process, but to tell user something unexpected is happening. This means that I want to take care of exceptions in the Presentation layer, and make this process kind of transparent in the rest of the layers.
  2. Atomic logic. This means that I am sure that every code calls will be executed, no matter what (db errors, endpoint exceptions etc) as I will always wrap exceptions using Try objects.

So, the process gets completed by adding Either to the formula. Repository pattern will return this kind of object that contains errors in the Left part of the couple, and results in the Right part so we will be able to make use of fold to check what is inside at the Presentation layer, and then manage errors or success depending on the situation.

Code example

service try

Here you can see that I'm making use of Try to wrap the fake authenticationRepository call, that may throw an exception if email or password are incorrect.

either repository

Then, in the Repository, I am generating Either response, to wrap exceptions to my own domain failures, and also the response.

either-presenter

At last, in the Presenter, I am making use of fold to check what is the result of the request.

Clone this wiki locally