Throw error with custom message using routing-controllers with desired format

Issue

I have a Node APP using routing-controllers and into Readme there is a section called Throw HTTP errors where the example is self-explanatory.

But the problem is when I try to reproduce that lines. In my code I have this example (is for testing purpose, I want to throw the exception without do anymore):

@Post('/')
async post(): Promise<any> {
    throw new NotFoundError(`User was not found.`);
}

Where NotFoundError is imported from routing-controllers. And this should works but… it returns the entire error trace instead of an object like this

{
  "name": "NotFoundError",
  "message": "User was not found."
}

The status is 404 but the text returned is:

Error
    at new HttpError (/path_to_the_code/node_modules/src/http-error/HttpError.ts:16:18)
    at new NotFoundError (/path_to_the_code/node_modules/src/http-error/NotFoundError.ts:10:5)
    at HelloWorld.<anonymous> (/path_to_the_code/src/controller/controllers/HelloWorldController.ts:20:15)
    at Generator.next (<anonymous>)
    at /path_to_the_code/src/controller/controllers/HelloWorldController.ts:17:71
    at new Promise (<anonymous>)
    at __awaiter (/path_to_the_code/src/controller/controllers/HelloWorldController.ts:13:12)
    at HelloWorld.post (/path_to_the_code/src/controller/controllers/HelloWorldController.ts:36:16)
    at ActionMetadata.callMethod (/path_to_the_code/node_modules/src/metadata/ActionMetadata.ts:252:44)
    at /path_to_the_code/node_modules/src/RoutingControllers.ts:123:28

I’m trying to debug and line 13:12 is another route:

@Get('/')
async get(): Promise<any> {
    return this.helloWorldService.getHello()
}

By the way, the entire controller is like this:

import { Controller, Get, NotFoundError, Post } from 'routing-controllers';
import { Service } from 'typedi';
import { HelloWorldService } from '../../business/services/HelloWorldService';

@Service()
@Controller('/hello-world')
export class HelloWorld {

    constructor(public helloWorldService: HelloWorldService) { }

    @Get('/')
    async get(): Promise<any> {
        return this.helloWorldService.getHello()
    }

    @Post('/')
    async post(): Promise<any> {
        throw new NotFoundError(`User was not found.`);
    }
}

So I can’t wonder from where came the error…

What I want is receive from the server the same structure as shown into docs: {name:'', message:''} and not the entire stack trace.

By the way it seems to be an internal error even the HTTP code works fine.

Thanks in advance.

Solution

I am pretty new at using Express and routing-controllers so I am not sure if this is the best way or not. I got it to work by adding a custom error handler as middleware:

import { Middleware, ExpressErrorMiddlewareInterface, HttpError } from 'routing-controllers';

@Middleware({ type: 'after' })
export class HttpErrorHandler implements ExpressErrorMiddlewareInterface {
    error(error: any, request: any, response: any, next: (err: any) => any) {
        if (error instanceof HttpError) {
            response.status(error.httpCode).json(error);
        }

        next(error);
    }
}

Note that I set the status using error.httpCode and then convert error into JSON. After this, I added the error handler into the middlewares array and set defaultErrorHandler to false:

const app = createExpressServer({
    defaultErrorHandler: false,
    middlewares: [
        HttpErrorHandler
    ],
    // ...
});

I now get a response like this (along with the appropriate status code on the response):

{
   httpCode: 404,
   name: "NotFoundError",
   message: "User was not found",
}

Answered By – Vivin Paliath

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published