Nest: [ Question ] Throw an HttpException on service ?

Created on 15 Dec 2017  路  6Comments  路  Source: nestjs/nest

Hi all,

I would like to know what is the best practice to send an HttpException. Can throw it on service or it's better to do this in the controller ?

Thx

question 馃檶

Most helpful comment

TLDR;

You should treat your Controllers as Your Manager to your Server (Application)
It should have only the response to the request , don't do completed work in it , this work should be done behind the scenes in your services another worker ..etc

Here is the short answer:
When Something bad happen, Throw an Error in your Service and Catch that Error in your Controller then make it user-friendly error
TIP : Here's what I'm doing
Assume that we have a candy Shop

User Request an candy , request goes to

Controller -> Service -> Model (Repository) -> Database

And unfortunately we couldn't find the candy 馃崿 that user requested , then there's an error will rise in Database Model then we will pass it to the Service the Service will pass it to Controller , Then The Controller should Throw an HTTP Error in user-friendly way .
Hope that Helped.

All 6 comments

TLDR;

You should treat your Controllers as Your Manager to your Server (Application)
It should have only the response to the request , don't do completed work in it , this work should be done behind the scenes in your services another worker ..etc

Here is the short answer:
When Something bad happen, Throw an Error in your Service and Catch that Error in your Controller then make it user-friendly error
TIP : Here's what I'm doing
Assume that we have a candy Shop

User Request an candy , request goes to

Controller -> Service -> Model (Repository) -> Database

And unfortunately we couldn't find the candy 馃崿 that user requested , then there's an error will rise in Database Model then we will pass it to the Service the Service will pass it to Controller , Then The Controller should Throw an HTTP Error in user-friendly way .
Hope that Helped.

Hi @workfel,
It depends on how much do you want your services layer to be independent of the transport one. Usually, it'd be better to follow what @shekohex has suggested, thus throw a domain exception and handle it within the controller, or if you wanna more generic solution - inside interceptor / exception filter.

Btw, to avoid a lot of boilerplate, we shouldn't overengineer - if your app is a REST API, throwing NotFoundException when there's no record inside the database directly from the service layer doesn't sound unacceptable 馃檪

Okey, thank you for your answers @shekohex @kamilmysliwiec !

Sorry if I reopen this, but throwing for instance a BadRequestException from within a service, will result in an error message to the console: UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) warning.js:18 (node:76283) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code..

And the response to the client will be: {statusCode: 500, message: "Internal server error"}. How come? Doing something wrong?

I also agree with what @shekohex said. But I'm just wondering why this exception thrown inside a service, not catched by the controller results in a 500 error...

Edit: me dumb. Not awaiting... 馃槄

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings