Usage Options

Eyewitness provides a few usage and configuration options, which developers can modify as per their need. Below you will see how each option works out.

Do Not Report Exceptions

We understand that there are certain exceptions which should not be reported as a bug, like

  • If user enters wrong credentials, we throw InvalidCredentials exception, it should not be reported by eyewitness to our inbox
  • Some access exception that you throw from guard, etc.

To do this, you need to simply pass an array to the function to let know the Eyewitness of all the exceptions that it needs to not watch.

/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 {
doNotReport(): Array<any> {
return [
NotFoundException,
UnauthorizedException,
];
}
catch(exception: any, host: ArgumentsHost) {
Eyewitness.doNotReport(this.doNotReport());
Eyewitness.watch(exception, host);
// ...
}
}

Notice the doNotReport method, it takes an array of the exceptions that you want eyewitness to ignore.'

caution

Not getting to know about the exception is also a way to make a bug-free system. If I don't know it, then it doesn't exists.

doNotReport method can be misused pretty easily, please use it wisely.

Webhooks

As we mentioned before, eyewitness's main job is to report unwanted exceptions directly to your inbox. But there are (and will be) cases where you may want to also inform some system (like Microsoft Teams, Slack, or any error logging system etc) about the exception.

We provide webhook options, where developers can easily setup their webhooks while configuring the modules, and eyewitness along with sending mail, will also send the complete report to yur specified endpoints.

Setup Webhook

src/app.module.ts
import { Module } from '@nestjs/common';
import { EyewitnessModule } from '@lib/eyewitness';
@Module({
imports: [
EyewitnessModule.register({
emails: ['sample@gxxxx.com'],
mailman: {...},
webhooks: [
{
url: 'https://webhook.site/e34f3063-038f-4303-a082-b8845a019f3d',
method: WebhookSupportMethod.GET,
header: {},
},
],
}),
],
controllers: [],
providers: [],
})
export class AppModule {}

Notice the highlighted webhooks array, url, method, header are the options, you need to pass to the object.

Incase any exception happens on your system, you will receive following payload as request on your webhook.

{
"exception": {
"name": "BadGatewayException",
"message": "Oops! This should'nt have happened",
"stack": "Error: Oops! This should'nt have happened\n at AppController.getHello (/Users/vinayaksarawagi/Sites/eyewitness-test/dist/main.js:194:15)\n at /Users/vinayaksarawagi/Sites/eyewitness-test/node_modules/@nestjs/core/router/router-execution-context.js:38:29\n at InterceptorsConsumer.intercept (/Users/vinayaksarawagi/Sites/eyewitness-test/node_modules/@nestjs/core/interceptors/interceptors-consumer.js:11:20)\n at /Users/vinayaksarawagi/Sites/eyewitness-test/node_modules/@nestjs/core/router/router-execution-context.js:46:60\n at /Users/vinayaksarawagi/Sites/eyewitness-test/node_modules/@nestjs/core/router/router-proxy.js:9:23\n at Layer.handle [as handle_request] (/Users/vinayaksarawagi/Sites/eyewitness-test/node_modules/express/lib/router/layer.js:95:5)\n at next (/Users/vinayaksarawagi/Sites/eyewitness-test/node_modules/express/lib/router/route.js:137:13)\n at Route.dispatch (/Users/vinayaksarawagi/Sites/eyewitness-test/node_modules/express/lib/router/route.js:112:3)\n at Layer.handle [as handle_request] (/Users/vinayaksarawagi/Sites/eyewitness-test/node_modules/express/lib/router/layer.js:95:5)\n at /Users/vinayaksarawagi/Sites/eyewitness-test/node_modules/express/lib/router/index.js:281:22"
},
"request": {
"timestamp": "Fri Oct 09 2020 01:04:38 GMT+0530 (India Standard Time)",
"headers": "{\"host\":\"localhost:3000\",\"connection\":\"keep-alive\",\"upgrade-insecure-requests\":\"1\",\"user-agent\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36\",\"accept\":\"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\",\"sec-fetch-site\":\"none\",\"sec-fetch-mode\":\"navigate\",\"sec-fetch-user\":\"?1\",\"sec-fetch-dest\":\"document\",\"accept-encoding\":\"gzip, deflate, br\",\"accept-language\":\"en-GB,en-US;q=0.9,en;q=0.8\"}",
"payload": "{}",
"url": "http://localhost:3000/"
}
}
Extra Bonus

Incase, you want to send custom request objects, we provide a requestBuilder property, which expects a function. Below is an example on how you can use it.

src/app.module.ts
import { Module } from '@nestjs/common';
import { EyewitnessModule } from '@lib/eyewitness';
@Module({
imports: [
EyewitnessModule.register({
emails: ['sample@gxxxx.com'],
mailman: {...},
webhooks: [
{
url: 'https://webhook.site/e34f3063-038f-4303-a082-b8845a019f3d',
method: WebhookSupportMethod.GET,
header: {},
requestBuilder: (payload: any) => ({
message: payload.exception.message,
extra: 'Built using custom `requestBuilder` method',
}),
},
],
}),
],
controllers: [],
providers: [],
})
export class AppModule {}

We have configured our custom payload requestBuilder method, now whenever any exception is witnessed on your server, you will receive following payload:

{
"message":"Oops! This should'nt have happened",
"extra":"Built using custom `requestBuilder` method"
}