简体   繁体   中英

Is it good way to throw error from service in nestjs like it:

const movie = await this.movieService.getOne(movie_id);
if(!movie){
  throw new Error(
    JSON.stringify({
      message:'some message',
      status:'http status'
   })
  );
}
const rating = await this.ratingRepository.find({where:{movie});
return rating;

And after it use try catch in controller and throw HttpExeption.

async getAllByMovie(@Param('movie_id') movie_id:string):Promise<Rating[]>{
  try{
    const ratings = await this.ratingService.getAllRatingsByMovie(Number(movie_id));
    return ratings;
  }catch(err){
    const {message,status} = JSON.parse(err.message);
    throw new HttpExeption(message,status);
  }
}

Is it good or not?

In general it's a good idea to throw business errors from your services and handle theses errors on controller layer. But there is room for improvement looking at your code:

To me it looks a bit odd to stringify the message and status in order to pass it to Error . You could create a custom Error that contains these properties:

class MyBusinessError extends Error {
  status: number;

  constructor(message: string, status: number) {
    super(message);
    this.status = status;
  }
}

But I suggest to decide on controller level which status should be returned from the API because this is http specific and should not be part of your business logic.

Also there are exception filters coming with NestJS that you can use to catch exceptions and transform them into http exceptions. With that you don't need to try-catch in every controller method. You can check for specific Error type using instanceof :

try {
  // ...
}
catch(err) {
  if(err instanceof MyBusinessError) {
    // handle business error
  }
  
  throw err;
}

It is a good question,

I face somewhat similar problem,

I prefer having try/catch statements in each service function, so I can easily send a response to a client.

However, seems like NestJs has those exception filters, but im still not fully sold on it, it seems like its a "cool" thing, but in reality I may not wanna check for what instanceof the error is, or go far beyond and create classes and other things for each module/service.

In fact, i'd prefer to have try/catch in the service function, if somewhere it fails, i just send a response to a user in a catch block saying that something went wrong while trying to execute that very function.


There a few problems that come to my mind when trying to think of the exception filters.

Lets say we have a service function, and it has a decent chunk of logic in it, and at some point it fails.

How can I log a custom error message when it happened? Im not sure i can if i use exception filters, the error may be something random, and it will be caught by a general interceptor with a generic message.

But if i have try/catch in service function, i can have whatever logic i want in the function and if it fails at some point i have an error handler specifically for that function.

The latter seems to me a better option.


Regarding the code you posted, it feels weird when you throw new HttpException right? its like its a catch statement and in the catch we have to throw an exception again?

its just odd, I love nestJs but their attempt to abstract exceptions is just not as clean as it could be.

Im pretty sure there is a way to get it to work with their exception filters and everything, but im just not sold on the idea that their way is better than simple try/catch

In NestJs we have all exception filters so that we don't need to handle errors in all places

you can refer https://docs.nestjs.com/exception-filters

all-exceptions.filter.ts

 import { ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus, } from '@nestjs/common'; import { object } from 'underscore'; @Catch() export class AllExceptionsFilter implements ExceptionFilter { catch(exception: any, host: ArgumentsHost) { const ctx = host.switchToHttp(); const response = ctx.getResponse(); const request = ctx.getRequest<CustomRequest>(); let internalStatus; let status = exception instanceof HttpException? exception.getStatus(): HttpStatus.INTERNAL_SERVER_ERROR; let message = exception.sqlMessage || exception.response || exception; let error = exception.sqlState === 45000? 'Bad Request': 'Bad Request'; request.log.timeTookToServe = Date.now() - request.log.timestamp; request.log.message = message; request.log.status = `${status}`; if (exception instanceof TypeError) { status = 400; error = 'Bad Request'; message = exception.message.substring(exception.message.indexOf('\n\n\n') + 1).trim(); } if (status === 500) { console.log(exception.sqlMessage, exception.sqlState, exception); } else { console.log(exception.sqlMessage, exception.sqlState, exception); } const errMessage = errJson[request.log['module']]; response.status(status).json({ status: exception.status || status, error: error, message: [ status === 403? "Either you don't have the privilege or been logged out.": message, ], }); } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM