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

[QUESTION] "failed with status code 500" output instead of detailed error message #416

Closed
shahafa opened this issue Jan 17, 2021 · 7 comments
Assignees
Labels
question Further information is requested

Comments

@shahafa
Copy link

shahafa commented Jan 17, 2021

When I'm throwing an error from Nest application nestjs-pino always prints in the error message "failed with status code 500" even though I'm throwing an error with a detailed message any idea how to solve this?

this is how I setup nestjs-pino

    LoggerModule.forRoot({
      pinoHttp: {
        prettyPrint: { colorize: true, translateTime: true },
        serializers: {
          req: (req) => ({
            ...req,
            headers: undefined,
          }),
          res: (res) => ({
            ...res,
            headers: undefined,
          }),
        },
      },
    }),

and this is how I throw the error

import {InternalServerErrorException} from '@nestjs/common';
throw new InternalServerErrorException("This is my detailed error");

And nestjs-pino output

   res: {
      "statusCode": 500
    }
    err: {
      "type": "Error",
      "message": "failed with status code 500",
      "stack":
          Error: failed with status code 500
              at ServerResponse.onResFinished (/Users/shahaf/Source/muil/muil/node_modules/pino-http/logger.js:73:38)
              at ServerResponse.clsBind (/Users/shahaf/Source/muil/muil/node_modules/cls-hooked/context.js:172:17)
              at ServerResponse.emit (node:events:388:22)
              at ServerResponse.EventEmitter.emit (node:domain:532:15)
              at ServerResponse.emitted (/Users/shahaf/Source/muil/muil/node_modules/emitter-listener/listener.js:134:21)
              at onFinish (node:_http_outgoing:800:10)
              at callback (node:internal/streams/writable:557:21)
              at afterWrite (node:internal/streams/writable:503:5)
              at afterWriteTick (node:internal/streams/writable:490:10)
              at processTicksAndRejections (node:internal/process/task_queues:79:21)
    }
    responseTime: 94
    req: {
      "id": 1,
      "method": "POST",
      "url": "/api/test/test",
      "remoteAddress": "::1",
      "remotePort": 61221
    }
@shahafa shahafa added the question Further information is requested label Jan 17, 2021
@gmathavadoss-chwy
Copy link

gmathavadoss-chwy commented Jan 25, 2021

Any suggestion/ work around for the above?

@trycontrolmymind
Copy link

same thing

@trycontrolmymind
Copy link

This solution works for me:

main.ts

const pinoLogger = await app.resolve(PinoLogger);
app.useGlobalFilters(new AllExceptionsFilter(pinoLogger));

all-exception.filter.ts

export class AllExceptionsFilter implements ExceptionFilter {
  constructor(private readonly logger: PinoLogger) {}
}

@iamolegga
Copy link
Owner

solved in v2.0.0

import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getHello(): string {
    throw new Error('foo bar baz');
    return this.appService.getHello();
  }
}
{"level":50,"time":1630106183244,"pid":742,"hostname":"my-host","req":{"id":1,"method":"GET","url":"/","query":{},"params":{"0":""},"headers":{"host":"localhost:3000","user-agent":"curl/7.64.1","accept":"*/*"},"remoteAddress":"::1","remotePort":61739},"context":"ExceptionsHandler","msg":"foo bar baz"}

@kasvith
Copy link

kasvith commented Sep 6, 2021

@iamolegga cant we get the stack trace there as well?

@iamolegga
Copy link
Owner

@kasvith please open a new feature request for that, will take a look at it when I have time

@kasvith
Copy link

kasvith commented Sep 6, 2021

Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants