npm.io
1.2.6 • Published 4 years ago

taticbr-notify-error

Licence
MIT
Version
1.2.6
Deps
4
Size
154 kB
Vulns
3
Weekly
0

This project contains a code to save errors and send them by notification adapter to your nest.js project.

Installation

npm install taticbr-notify-error

or

yarn add taticbr-notify-error

How to use

Implementing on your application

First, import NotifyErrorModule and imports it in your module, don't forget to provide your Event Emitter Adapter and mongoDB connection.

Example:

import { Module } from '@nestjs/common';
import {
  EventEmitterAdapter,
  EventEmitterProvider,
} from 'src/infrastructure/adapters/eventEmitter/';
import { NotifyErrorModule } from 'taticbr-notify-error';

@Module({
  imports: [
    NotifyErrorModule.forRoot({
      eventEmitterAdapter: EventEmitterAdapter,
      mongoUrl: 'mongodb://localhost:27017/DBName',
    }),
  ],
  providers: [EventEmitterProvider],
})
export class PimModule {}

Event Emitter should follow abstract class definition:

export abstract class EventEmitterPort {
  request: (topic: string, payload: unknown) => Promise<void>;
}

Saving error to notify

To save an error, you must import SaveNotifyError class, create it instance on your service and use like the following example:

import { Injectable } from '@nestjs/common';
import { SaveNotifyError } from 'taticbr-notify-error';

@Injectable()
export class MyService {
  constructor(private readonly saveError: SaveNotifyError) {}

  private async execute() {
    try {
      // ... execution ...
    } catch (error) {
      this.saveError.save({ error: error.message, errorType: 'pim' });
    }
  }
}

Configuration

You can configure this module by setting some environemnt variables

Environment Variables Utility Default
VERIFY_ERROR_SCHEDULE_CRON Defines a cron expression to define at what delay the error submit task runner will run. every one hour
PROCESS_ERROR_COLLECTION_NAME Defines the mongo collection name to save process errors processError
EVENT_EMITTER_TOPIC Defines the name of the topic that will be sent to the event emitter NotifyLoadPimDelayTopic