Nest: How to print errors

Created on 20 Dec 2017  路  8Comments  路  Source: nestjs/nest

Hey, just a quick support request:

I have made a mistake now my app throws an error:

[Nest] 28   - 2017-12-20 17:13:56   [ExceptionsHandler] [object Object]

This log is of course pretty useless (although nicely colored). So i've tried overwriting the logging in my server.ts:

app.useGlobalFilters({
    catch: (e: any, res: any) => {
      console.error(e);
      res.status(500).send(e);
    }
  });

Unfortunately this does nothing at all. Help would be much appreciated!

Most helpful comment

Hi @Kamshak,
In 4.5.2 you should be able to catch each exception, including those that not extend Error. In order to do that, just left the parentheses empty @Catch() https://docs.nestjs.com/exception-filters

All 8 comments

Tried this as well:

@Catch(Error)
class Handler {
  catch(e: any, res: any) {
    console.error(e);
    res.status(500).send(e);
  }
}

app.useGlobalFilters(new Handler());

could you explain more ?
is this an error from un handled code , or from Nest ?
and you may have a look at #Nest-Flub

@Kamshak It looks like you might be using the RPC transport? If so, have very similar issues. #316 #303

Actually not using RPC transport on account of res usage. Ignore me.

Hi @Kamshak,
In 4.5.2 you should be able to catch each exception, including those that not extend Error. In order to do that, just left the parentheses empty @Catch() https://docs.nestjs.com/exception-filters

Thanks!

As of nest 5.0.0, the 2nd param of the catch method is not a response object, but an ArgumentHost, which encapsulates the response object. Also, we can render a template based on status.

import { ExceptionFilter, Catch, HttpException, ArgumentsHost, HttpStatus } from '@nestjs/common';

@Catch()
export class ErrorFilter implements ExceptionFilter {
  catch(error: Error, host: ArgumentsHost) {
    let response = host.switchToHttp().getResponse();
    let status = (error instanceof HttpException) ? error.getStatus(): HttpStatus.INTERNAL_SERVER_ERROR;

    if (status === HttpStatus.UNAUTHORIZED) 
        return response.status(status).render('views/401');
    if (status === HttpStatus.NOT_FOUND) 
        return response.status(status).render('views/404');
    if (status === HttpStatus.INTERNAL_SERVER_ERROR) {
        if (process.env.NODE_ENV === 'production') {
          console.error(error.stack);
          return response.status(status).render('views/500');
        }
        else {
          let message = error.stack;
          return response.status(status).send(message); 
        } 
    }
  }
}

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rafal-rudnicki picture rafal-rudnicki  路  3Comments

breitsmiley picture breitsmiley  路  3Comments

artaommahe picture artaommahe  路  3Comments

marshall007 picture marshall007  路  3Comments

FranciZ picture FranciZ  路  3Comments