import { Application } from 'egg' ; import { AppPluginBase } from '../../basic/AppPluginBase' ; import { IncomingWebhookSendArguments } from '@slack/webhook/dist/IncomingWebhook' ; import moment from 'moment' ; import { ErrorManagerConfig } from './ErrorManagerConfig' ; export class AppErrorManager extends AppPluginBase < ErrorManagerConfig > { constructor ( config , app ) { super ( config , app ) ; } public async onReady ( ) { } public async onStart ( ) { process . on ( 'uncaughtException' , ( error ) => { this . handleError ( 'Hypertrons AppErrorManager Uncaught Exception' , error ) ; } ) ; process . on ( 'unhandledRejection' , ( error , _ ) => { this . handleError ( 'Hypertrons AppErrorManager Unhandled Rejection' , error ) ; } ) ; } public async onClose ( ) { } public async handleError ( from , err ) { try { if ( this . config . slack ) { const slackMessage = { blocks : [ { type : 'section' , text : { type : 'mrkdwn' , text : '*From*:     ' + '`' + from + '`' + '\n' + '*Time*:     ' + '`' + moment ( ) . format ( ) + '`' + '\n' + '*Details*:  ' + '```' + err . toString ( ) + '\n' + divider + err . stack + '\n' + divider + JSON . stringify ( err ) + '```' , } , } , { type : 'divider' , } , ] , } ; try { this . config . slack . forEach ( c => this . app . imManager . sendToSlack ( slackMessage , c ) ) ; } catch ( error ) { this . logger . error ( error ) ; } } } catch ( error ) { this . logger . error ( error ) ; } } } const divider = '--------------------------------------------------------------------------------' ;