Sunday 20 October 2019

How do you handle errors in your Repositories/Data Access Layer?

I'm working in TypeScript, and I have a Repository that encapsulates my ORM's query building API. To sign up a user, for instance, a Controller Action delegates to a Service, which calls methods of my Repository.I impose particular constraints with my ORM, such as stating that an email field must be unique. That means that my Repository's save and update methods for the user could throw an error by the ORM. If that's the case, and for the purpose of abstraction, I obviously want to encapsulate that error with some custom error type.So, should my Repository functions intentionally throw domain-specific errors to be caught by the Service? I was considering letting errors bubble up all the way to the Controller, and then handling it with Express Middleware to return a proper response to the client. Another option might be to build an Either Monad result wrapper for my Repository functions and have them return that.Do you have any advice?Thank you.

Submitted October 21, 2019 at 12:53AM by JamieCorkhill

No comments:

Post a Comment