dimanche 15 septembre 2019

Services should throw exceptions? (Web API)

I found similar questions but not found any good answer so I am posting it again and with more specific environment.

I am using Node.js + Express to build REST APi for my React Native app.

I am using pattern Router->Controller->Service->Database and I am not sure if I am supposed to throw specific errors from my services.

Example:

  • I am validating registration request.
  • Field validations are hapenning inside Controller (I am using ajv JSON schema validator).
  • Now I need to validate if user/email already exists

Am I supposed to do this from controller by calling for example service.emailExists(data.email) before calling service.createUser(data)??

Or I can let database fall on CREATE duplicate, return false from service.createUser(data) and inform user from controller that Email exists. If I do so, I am not able to inform user if there is Unspecified error inside service, because it will always return as Email exists error, even something else happens.

Aucun commentaire:

Enregistrer un commentaire