Skip to content
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

Open
xujif opened this issue Sep 8, 2017 · 15 comments
Open

how to prevent print HttpError in console with express server #281

xujif opened this issue Sep 8, 2017 · 15 comments
Labels
status: awaiting answer Awaiting answer from the author of issue or PR. type: question Questions about the usage of the library.

Comments

@xujif
Copy link

xujif commented Sep 8, 2017

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()"

@MichalLytek
Copy link
Contributor

AFAIK error is printed only in development mode. @pleerock I haven't found any console call in src, do we have a logger support?

@NoNameProvided
Copy link
Member

how to prevent print HttpError in console with express server?

Express will use the debug package by default. So you have to set the DEBUG env variable to "" (empty string)

koa server still return 404 when with a "@Authorized()"

This has been fixed, do you use the latest version?

@NoNameProvided NoNameProvided added the type: question Questions about the usage of the library. label Sep 8, 2017
@xujif
Copy link
Author

xujif commented Sep 8, 2017

#282

koa server still return 404 when with a "@Authorized()"

@xujif
Copy link
Author

xujif commented Sep 9, 2017

it doesn't work

Express will use the debug package by default. So you have to set the DEBUG env variable to "" (empty string)

@NoNameProvided
Copy link
Member

What appears in the terminal can you share a screenshot? It is a 404 HttpError from routing controllers or something else?

@xujif
Copy link
Author

xujif commented Sep 9, 2017

>rm -rf dist\ && tsc && node dist\t.js
listen at 8088
Error
    at ForbiddenError.HttpError [as constructor] (D:\code\t\node_modules\routing-controllers\http-error\HttpError.js:27:23)
    at new ForbiddenError (D:\code\t\node_modules\routing-controllers\http-error\ForbiddenError.js:20:28)
    at TestClass.err (D:\code\t\dist\controllers\test.js:40:15)
    at ActionMetadata.callMethod (D:\code\t\node_modules\routing-controllers\metadata\ActionMetadata.js:105:48)
    at D:\code\t\node_modules\routing-controllers\RoutingControllers.js:92:140
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

@NoNameProvided
Copy link
Member

NoNameProvided commented Sep 9, 2017

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:

https://github.com/expressjs/express/blob/a4bd4373b2c3b2521ee4c499cb8e90e98f78bfa5/lib/application.js#L149-L175

Their logerror method looks like this:

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 res.end() after sending the error and not calling next() here:

https://github.com/pleerock/routing-controllers/blob/master/src/driver/express/ExpressDriver.ts#L343

Right?

@NoNameProvided
Copy link
Member

@19majkel94 Koa has an options for this: app.silent: boolean, maybe we can add an option to RoutingControllersOptions something like supressErrors: boolean.

If set true we set app.silent to true in koa.

/**
 * 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?

@MichalLytek
Copy link
Contributor

@19majkel94 should we care about this? Probably we can simply call res.end() after sending the error and not calling next() here:
https://github.com/pleerock/routing-controllers/blob/master/src/driver/express/ExpressDriver.ts#L343
Right?

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.

@xujif
Copy link
Author

xujif commented Sep 11, 2017

When can i upgrade to next version?

@NoNameProvided
Copy link
Member

@xujif You do not have to update to a new version, just create an after middleware like my example above.

@xujif
Copy link
Author

xujif commented Sep 11, 2017

@NoNameProvided I mean the fix of #282 which is closed.

@NoNameProvided
Copy link
Member

It's not released yet, so for now you can only install if from the master branch via

npm install git://github.com/pleerock/routing-controllers.git#master --save

Do this only for testing.

@xujif
Copy link
Author

xujif commented Sep 13, 2017

Is there a release planing?

@github-actions
Copy link

Stale issue message

@github-actions github-actions bot added the status: awaiting answer Awaiting answer from the author of issue or PR. label Feb 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: awaiting answer Awaiting answer from the author of issue or PR. type: question Questions about the usage of the library.
Development

No branches or pull requests

3 participants