Create an API via NestJS Part III

Create Extended Error handler

  1. Create folder:
    mkdir src/shared && cd src/shared
  2. Create the filter:
    touch http-error.filter.ts
  3. Write simple error handler
src/shared/http-error.filter.ts

Include custom error handler as a provider to the app module

src/app.module.ts

Add a logger to this error handler

The next thing that we should do is creating the Logger for our ErrorHandler:

Open your http-error.filter.ts file and type simple logger:

src/shared/http-error.filter.ts
Pay attention, this code must be member of catch() function

Now if we send some incorrect request we should see:

console output

Looks pretty good, but we still don't logging crrect requests.

Let's make interceptor

Create a new file: touch src/shared/logging.interceptor.ts

And write simple logger:

src/shared/logging.interceptor.ts

Now we can connect it as a provider to app.module

src/app.module.ts

Check how it works. Start the server and send some correct request, you should get some like this: