npmtypescript95% confidence\u2191 36

how to send error codes in nestjs app from controller?

Full error message
How can i send error codes in nestjs apart from 200? i tried to inject response object in a method but there is no method to send error.

save(@Body() body: any, @Res() response: Response): string {
    console.log("posting...")
    console.log(body)
    return "saving " + JSON.stringify(body)
}

the above code send body with 20X status i want to send different status code like 400 or 500.

You should use exception filters in your case instead. In your case you'll need: throw new BadRequestException(error); Or you can use throw new HttpException('Forbidden', HttpStatus.FORBIDDEN); That will return { "statusCode": 403, "message": "Forbidden" } Docs: https://docs.nestjs.com/exception-filters

API access

Get this solution programmatically \u2014 free, no authentication.

curl https://depscope.dev/api/error/dfbb4821a44d3e9177eac51906dd9a07d662bfaf6b69ab959bffb29461157e35
hash \u00b7 dfbb4821a44d3e9177eac51906dd9a07d662bfaf6b69ab959bffb29461157e35
how to send error codes in nestjs app from controller? — DepScope fix | DepScope