{"id":510,"hash":"dfbb4821a44d3e9177eac51906dd9a07d662bfaf6b69ab959bffb29461157e35","pattern":"how to send error codes in nestjs app from controller?","full_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.\n\nsave(@Body() body: any, @Res() response: Response): string {\n    console.log(\"posting...\")\n    console.log(body)\n    return \"saving \" + JSON.stringify(body)\n}\n\nthe above code send body with 20X status i want to send different status code like 400 or 500.","ecosystem":"npm","package_name":"typescript","package_version":null,"solution":"You should use exception filters in your case instead.\n\nIn your case you'll need:\n\nthrow new BadRequestException(error);\n\nOr you can use\n\nthrow new HttpException('Forbidden', HttpStatus.FORBIDDEN);\n\nThat will return\n\n{\n  \"statusCode\": 403,\n  \"message\": \"Forbidden\"\n}\n\nDocs: https://docs.nestjs.com/exception-filters","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/56883056/how-to-send-error-codes-in-nestjs-app-from-controller","votes":36,"created_at":"2026-04-19T04:51:16.277499+00:00","updated_at":"2026-04-19T04:51:16.277499+00:00"}