1

I have a global filter for exceptions. I want to create a logger. But in the filter, I can't see in which line or file the error occurred. Is it possible to know about it in the filter?

I want to do this because I don't want to add a logger to every part of the project. Any idea would be appreciated.

Vahid Najafi
  • 4,654
  • 11
  • 43
  • 88

1 Answers1

3

Every error, including Nest's HttpException class and child classes, should have a .stack property that tells where the error was thrown, what line, and what happened to get to that point. You can log that and then grok it as necessary to get the information you want

Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147
  • Thanks Jay. How should I access to `.stack` in the filter class? Any example code? – Vahid Najafi Jun 26 '21 at 07:47
  • Nest's HttpEceptions `extends Error`, so you should be able to do `excception.stack` – Jay McDoniel Jun 26 '21 at 17:54
  • Thank you so much. That's what I was looking for. Btw, is there any way to know which line is it in ts file not compiled js file? – Vahid Najafi Jun 26 '21 at 20:15
  • https://stackoverflow.com/questions/59000552/how-to-print-stack-trace-with-reference-to-typescript-source-in-nest-js/59001971#59001971 – Jay McDoniel Jun 27 '21 at 15:07
  • Sorry Jay, I have one more question if you don't mind. Whenever a database error occurs, it doesn't show the line of error in the app level, it just refers to the internal library (typeorm). Any idea? – Vahid Najafi Jun 29 '21 at 06:48