English 中文(简体)
以理想格式使用路由控制器丢弃自定义信件错误
原标题:Throw error with custom message using routing-controllers with desired format
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 { 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. (/path_to_the_code/src/controller/controllers/HelloWorldController.ts:20:15) at Generator.next () at /path_to_the_code/src/controller/controllers/HelloWorldController.ts:17:71 at new Promise () 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 { 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 { return this.helloWorldService.getHello() } @Post( / ) async post(): Promise { 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.
最佳回答
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", }
问题回答
Change the @Controller( /hello-world ) into @JsonController( /hello-world ) Then you can add the middleware as @Vivin Paliath mentioned earlier and improve error handling.




相关问题
store data in memory with nestjs

I am trying to persist some data in my nestjs server so I can then use that data in my client app through http requests. I have created a products.service.ts file with the function getAllData() that ...

React Hook Form Error on custom Input component

I am having a problem when I use react hook form + zod in my application, in short the inputs never change value and I get the following error in the console: Warning: Function components cannot be ...

Updatable promises using proxy in JavaScript

EDIT: I ve updated this question, and the old question is moved here. A POC can be found on this jsfiddle or the snippet below GOAL: The goal here is to make or rather simulate a promise that can be ...

热门标签