I would like to create a REST API with NestJs. But I want to add GraphQL as another top level layer later on. So for the start I have the basic layers controller, service and TypeORM repository. Let's assume you want to update a user's username by id. The controller route could be
PATCH /users/:id/username
Two problems might come up in the service or repository layer:
- The user id might not exist
- The username exists already
The basic flow of this operation would be
- Fetch the user by id
- Handle error if the user does not exist
- Check if the username exists already
- Handle error if the username exists already
- Update the user's username
I'm thinking about how I should handle those errors. I could throw exceptions immediately based on this concept
https://en.wikipedia.org/wiki/Fail-fast
NestJs provides some out of the box exceptions I can use
https://docs.nestjs.com/exception-filters#built-in-http-exceptions
The problem is that I don't think I should throw HTTP exceptions in my service layer. They should be thrown in my controller logic. So what is a common approach for those errors?
- Should I just return
undefined
instead of an updated user? The controller wouldn't know which part failed. - Should I create my own exceptions extending
Error
and throw them? - Due to the fact exceptions come with low performance should the return type of the function be something like
<User | NotFoundError | ConflictError>
?