Added webhook to send errors

This commit is contained in:
Davidson Gomes
2023-08-25 09:01:48 -03:00
parent c03919be2d
commit 9b72b3e332
7 changed files with 65 additions and 18 deletions

View File

@@ -1,11 +1,12 @@
import 'express-async-errors';
import axios from 'axios';
import compression from 'compression';
import cors from 'cors';
import express, { json, NextFunction, Request, Response, urlencoded } from 'express';
import { join } from 'path';
import { configService, Cors, HttpServer, Rabbitmq } from './config/env.config';
import { configService, Cors, HttpServer, Rabbitmq, Webhook } from './config/env.config';
import { onUnexpectedError } from './config/error.config';
import { Logger } from './config/logger.config';
import { ROOT_DIR } from './config/path.config';
@@ -54,6 +55,34 @@ function bootstrap() {
app.use(
(err: Error, req: Request, res: Response, next: NextFunction) => {
if (err) {
const webhook = configService.get<Webhook>('WEBHOOK');
if (webhook.GLOBAL.ENABLED && webhook.EVENTS.ERRORS) {
const tzoffset = new Date().getTimezoneOffset() * 60000; //offset in milliseconds
const localISOTime = new Date(Date.now() - tzoffset).toISOString();
const now = localISOTime;
const errorData = {
event: 'error',
data: {
error: err['error'] || 'Internal Server Error',
message: err['message'] || 'Internal Server Error',
status: err['status'] || 500,
response: {
message: err['message'] || 'Internal Server Error',
},
},
date_time: now,
};
logger.error(errorData);
const baseURL = configService.get<Webhook>('WEBHOOK').GLOBAL.URL;
const httpService = axios.create({ baseURL });
httpService.post('', errorData);
}
return res.status(err['status'] || 500).json({
status: err['status'] || 500,
error: err['error'] || 'Internal Server Error',