@dev-codenix/nest-watchman
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Examples

Discord Email
examples not implemented yet

Description

Watchman is an error cather for Nest framework.

Installation

$ npm install @dev-codenix/nest-watchman --save

Usage

in ExceptionFilter

// all-exception-filter.ts 
constructor(private watchManService: WatchmanService) {
  super();
}
 public catch(exception: IException, host: ArgumentsHost): void {
  this.watchManService.watch(exception,{
    host,
    metaData:{},
    trackUUID:'uuid'
  });
  ...
}
  • host
    • host object exist in exception filter. we get the express request and response from this object
  • metaData
    • you can pass anything in this field, but it doesn't any effect on default message. you can access to this data in custom strategy through super.metaData
  • trackUUID
    • if you want to have an uuid to track your errors. note that you can pass uuid in your exception object we have this interface for this purpose IException

where ever you want

this.watchManService.watch(exception,{
  metaData:{},
  trackUUID:'uuid'
});

you don't need to pass host here

Module Configuration

Main config properties

  • strategy
  • catchOnlyInternalExceptions
    • if you pass true Watchman will catch only the internal server errors. default=false
  • strategyConfig
    • it will change base on your strategy

forRoot Config

// app.module.ts
@Module({
  imports: [
    WatchmanModule.forRoot({
          strategy: DiscordBaseStrategy,
          catchOnlyInternalExceptions: true,
          strategyConfig: {
            webHookUrl: 'https://discord.com/api/webhooks/id/token',
            mentionList: ['everyone'],
          },
    }),
  ],
})

Async Config

Using useFactory method

// app.module.ts
@Module({
  imports: [
    WatchmanModule.forRootAsync({
      imports:[AppConfigModule],
      useFactory: (configService: AppConfigService) => {
        return {
          strategy: DiscordBaseStrategy,
          catchOnlyInternalExceptions: true,
          strategyConfig: {
            webHookUrl: configService.WATCHMAN_WEBHOOK,
            mentionList: ['everyone'],
          },
        };
      },
      inject: [AppConfigService],
    }),
  ],
})

Using useClass method

// app.module.ts
@Module({
  imports: [
    WatchmanModule.forRootAsync({
      imports:[DiscordConfigModule],
      useClass: DiscordConfigService
    }),
  ],
})

// discord.config.service.ts
@Injectable()
export class DiscordConfigService implements WatchmanModuleFactory{
  constructor(private configService: ConfigService) {}
  get WATCHMAN_WEBHOOK(): string {
    return this.configService.get<string>('App.WATCHMAN_WEBHOOK');
  }
  createWatchmanModuleOptions(): Promise<WatchmanModuleOptions> | WatchmanModuleOptions {
    return {
      strategy: DiscordBaseStrategy,
      catchOnlyInternalExceptions: false,
      strategyConfig: {
        webHookUrl: this.WATCHMAN_WEBHOOK,
        mentionList: ['everyone'],
      }
    }
  }
}

you can implement your config service class from WatchmanModuleFactory interface

Custom Strategy

// discord.strategy.ts
@Injectable()
export class DiscordStrategy extends DiscordBaseStrategy {
  constructor(private configService: AppConfigService) {
    super({
      webHookUrl: configService.WATCHMAN_WEBHOOK,
      mentionList: ["here"]
    });
  }
  withMetaDataMessageFormat(): IDiscordBody {
    return super.withMetaDataMessageFormat();
  }
  simpleMessageFormat(): IDiscordBody {
    return super.simpleMessageFormat();
  }
}

Strategies

Discord

these are the configs that you should pass in strategyConfig property or in custom strategy's constructor

{
  webHookUrl: configService.WATCHMAN_WEBHOOK,
  mentionList: ['everyone']
}
  • webHookUrl
    • discord web hook url. you can find it here
  • mentionList
    • list of persons that you want to mention in the discord channel. example here, everyone

Slack

in development process

Email

in development process

Microsoft Teams

in development process

Stay in touch

License

Watchman is MIT licensed.

Package Sidebar

Install

npm i @dev-codenix/nest-watchman

Weekly Downloads

3

Version

1.0.1

License

MIT

Unpacked Size

34.6 kB

Total Files

29

Last publish

Collaborators

  • emdjoo