Setup

To start using eyewitness, we need to configure it first. Now, we will see how we can configure the package to start the error reporting.

Configure Module

As our first step, we need to import EyewitnessModule module first to AppModule class.

So, there are basically two steps to configure the package:

1. Static Configuration

import { Module } from '@nestjs/common';
import { EyewitnessModule } from '@lib/eyewitness';
@Module({
imports: [
EyewitnessModule.register({
emails: ['sample@gxxxx.com'],
mailman: {
host: '',
username: '',
password: '',
port: '',
from: '',
path: '',
},
}),
],
controllers: [],
providers: [],
})
export class AppModule {}

2. Dynamic Configuration (Recommended)

There will be cases in your project, where you are loading your environment and project configurations from different places and asynchronously. So, you can configure your module like dynamic module, ie load configurations asynchronously.

info

We recommend using a seperate eyewitness.config.ts.

import { Module } from '@nestjs/common';
import { EyewitnessModule } from '@lib/eyewitness';
import { ConfigService } from '@nestjs/config';
@Module({
imports: [
EyewitnessModule.registerAsync({
imports: [ConfigService],
useFactory: function(config: ConfigService) {
return config.get('eyewitness'); // contains all the configuration values
},
inject: [ConfigService],
}),
],
providers:[],
exports: [],
})
export class AppModule {}

To learn more about the ConfigService and its fundamentals, please head over to configuration section.

info

The EyewitnessModule gets registered in Global Scope, so once configured, it can be used wherever your exception filter resides.

Now that you have loaded your module, it is now time to create an exception filter before we can start using eyewitness ๐Ÿ‘€.

Create Exception Filter

note

If you already have an exception filter, please feel free to skip to next step.

For those, who don't have an exception filter, can head over to exception filters section in NestJS website, it explains on how to create and use one.

info

We recommend, using a global exception filter, so that eyewitness watches as much as exceptions possible.

Use Eyewitness

We have created exception filter, and now its time to use Eyewitness in it. In following steps we will explain how to start using Eyewitness.

/src/core/exceptions/filter.ts
iport {
Catch,
ArgumentsHost,
NotFoundException,
UnauthorizedException,
} from "@nestjs/common";
import { BaseExceptionFilter } from "@nestjs/core";
import { Eyewitness } from "@lib/eyewitness";
@Catch()
export class ExceptionFilter extends BaseExceptionFilter {
catch(exception: any, host: ArgumentsHost) {
Eyewitness.watch(exception, host);
const ctx = host.switchToHttp();
const response = ctx.getResponse<any>();
let message =
exception.message || "Something went wrong. Please try again later";
const status = exception.status ? exception.status : 500;
return response.status(status).json({
success: false,
code: status,
message,
});
}
}

And boom ๐Ÿ’ฅ ! You have now configured eyewitness. Simple, right?

Test it out

Now, as a good step, you should now test your configuration.

To keep it dead simple, throw an exception from any of the apis in your local/staging environment and see the magic unfold. If everything works out well, you will receive an email in your inbox, like the one below: