Title: How to return json message from callback instead of triggering error | NestJs File upload
Tags: node.js;file-upload;error-handling;nestjs;multer
Question: I am working on Nestjs Multer File Upload, I have created a file filter for FileInterceptor, I wanna send a response back instead of sending an error, I have to send a JSON with the message as &quot;file type is not supported&quot;.
export const FileFilter = (req, file, callback) =&gt; {
if (!file.originalname.match(/\.(jpg|jpeg|png)$/)) {
return callback(new Error('Only image files supported!'), false);
callback(null, true);
Instead of sending new Error(), I would like to send
res.send({status:&quot;error&quot;,message:&quot;File types does not supported&quot;});

Here is the accepted answer: This action is performed by a built-in global exception filter, which handles exceptions of type HttpException (and subclasses of it). When an exception is unrecognised (is neither HttpException nor a class that inherits from HttpException), the built-in exception filter generates the following default JSON response:
&quot;statusCode&quot;: 500,
&quot;message&quot;: &quot;Internal server error&quot;
In many cases, you will not need to write custom exceptions, and can use the built-in Nest HTTP exception, as described in the next section. If you do need to create customised exceptions, it's good practice to create your own exceptions hierarchy, where your custom exceptions inherit from the base HttpException class. With this approach, Nest will recognise your exceptions, and automatically take care of the error responses. Let's implement such a custom exception:
import { ExceptionFilter, Catch, HttpException, ArgumentsHost, HttpStatus, BadRequestException } from '@nestjs/common';
@Catch()
export class ErrorFilter implements ExceptionFilter {
catch(error: Error, host: ArgumentsHost) {
let response = host.switchToHttp().getResponse();
let status = (error instanceof HttpException) ? error.message: HttpStatus.INTERNAL_SERVER_ERROR;
if (status.statusCode === HttpStatus.BAD_REQUEST) {
return response.status(HttpStatus.BAD_REQUEST).send(status)
if (status.statusCode === HttpStatus.NOT_FOUND) {
return response.status(HttpStatus.NOT_FOUND).send(status)
if (status.statusCode === HttpStatus.UNAUTHORIZED)
return response.status(status.statusCode).send(status)

if (status.statusCode === HttpStatus.NOT_FOUND)
return response.status(status).send(status)
if (status === HttpStatus.INTERNAL_SERVER_ERROR) {
if (process.env.NODE_ENV === 'production') {
console.error(error.stack);
return response.status(status).render('views/500');
}
else {
let message = error.stack;
return response.status(status).send(message);
}
For more details, you can check the Nest.js documentation custom exception filter.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:11/08/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<node.js><file-upload><error-handling><nestjs><multer>', 'Title': 'How to return json message from callback instead of triggering error | NestJs File upload', 'ViewCount': 503, 'id': 69879022, 'user_AboutMe': None, 'user_AccountId': 21102386.0, 'user_DisplayName': 'Nishad Ceroen', 'user_Reputation': 25.0, 'user_Views': 3.0, 'user_id': 15512036.0}}