I'm currently working on a Node APP that utilizes the routing-controllers library. In the README file, there is a section titled Throw HTTP errors, which includes a self-explanatory example.
However, I encountered an issue when attempting to replicate the code. Here is the example I have in my own code (for testing purposes, I want to trigger the exception without any further action):
@Post('/')
async post(): Promise<any> {
throw new NotFoundError(`User was not found.`);
}
In this code snippet, the NotFoundError
is imported from the routing-controllers
library. It should work as intended but instead of returning an object like this:
{
"name": "NotFoundError",
"message": "User was not found."
}
The response returns the entire error trace rather than just the specified object. The status code is 404
, but the text returned is lengthy and shows various steps within the error trace.
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)
...
I've been trying to debug this issue, but it seems that line 13:12
points to another route implementation:
@Get('/')
async get(): Promise<any> {
return this.helloWorldService.getHello()
}
For reference, here is the full controller implementation:
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.`);
}
}
It's puzzling to me where exactly this error is originating from...
My goal is to receive the same structured response from the server as depicted in the documentation: {name:'', message:''}
, and not the complete stack trace information.
Despite the functionality appearing to work fine with regards to the HTTP code itself, there seems to be an internal error causing the unexpected behavior.
Thank you in advance for any insights or assistance.