-
Notifications
You must be signed in to change notification settings - Fork 397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to prevent print HttpError in console with express server #281
Comments
AFAIK error is printed only in development mode. @pleerock I haven't found any |
Express will use the
This has been fixed, do you use the latest version? |
|
it doesn't work
|
What appears in the terminal can you share a screenshot? It is a 404 HttpError from routing controllers or something else? |
|
Thanks! I can confirm your issue. The minimal reproducible use-case: import 'reflect-metadata';
import {
createKoaServer,
createExpressServer,
JsonController,
Get,
ForbiddenError
} from 'routing-controllers';
@JsonController()
export class SomeController {
@Get("/")
public test() {
throw new ForbiddenError();
}
}
createExpressServer().listen(3001); The problem is here: Their function logerror(err) {
/* istanbul ignore next */
if (this.get('env') !== 'test') console.error(err.stack || err.toString());
} So Express will always log the error when not in test mode or no handler is provided. @xujif to overcome this you need to add your custom error handler. Something like: import { Request, Response, NextFunction } from 'express';
import { Middleware, ExpressErrorMiddlewareInterface } from 'routing-controllers';
export class FinalMiddleware implements ExpressErrorMiddlewareInterface {
public error(error: any, req: Request, res: Response, next?: NextFunction): void {
if (!res.headersSent) {
res.status(404);
res.send(error);
}
res.end();
}
} @19majkel94 should we care about this? Probably we can simply call Right? |
@19majkel94 Koa has an options for this: If set true we set /**
* Registers all loaded actions in your koa application.
*/
export function useKoaServer<T>(koaApp: T, options?: RoutingControllersOptions): T {
const driver = new KoaDriver(koaApp);
if(options.supressErrors) {
driver.app.silent = true;
}
return createServer(driver, options);
}
/**
* Registers all loaded actions in your koa application.
*/
export function createKoaServer(options?: RoutingControllersOptions): any {
const driver = new KoaDriver();
if(options.supressErrors) {
driver.app.silent = true;
}
return createServer(driver, options);
} And for Express we would simply not call next. Thoughts? |
No, we can't do that as you might want to use default error handler for sending error response and then custom middleware witch will log the error to the db. |
When can i upgrade to next version? |
@xujif You do not have to update to a new version, just create an after middleware like my example above. |
@NoNameProvided I mean the fix of #282 which is closed. |
It's not released yet, so for now you can only install if from the master branch via
Do this only for testing. |
Is there a release planing? |
Stale issue message |
how to prevent print HttpError in console with express server?
koa server not exists this problem.
koa server still return 404 when with a "@Authorized()"
The text was updated successfully, but these errors were encountered: