diff --git a/package.json b/package.json index 7876227..1cbd556 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "dependencies": { "@sentry/node": "^7.57.0", "@slack/client": "^5.0.2", + "@slack/webhook": "^7.0.2", "@types/bcrypt": "^5.0.0", "@types/bcryptjs": "^2.4.2", "@types/chai-things": "^0.0.35", diff --git a/src/index.ts b/src/index.ts index 3a0a025..2b97c05 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,6 @@ import connectDB from './loaders/db'; import routes from './routes'; import helmet from 'helmet'; import errorHandler from './middlewares/errorHandler'; -import * as SentryConfig from './loaders/sentryConfiguration'; import corsMiddleware from './middlewares/cors'; import config from './config'; import sessionConfiguration from './middlewares/session/sessionConfiguration'; @@ -14,15 +13,11 @@ app.use(express.json()); app.use(helmet()); app.use(corsMiddleware); app.use(sessionConfiguration); -SentryConfig.initializeSentry(app); app.use(routes); -SentryConfig.attachSentryErrorHandler(app); app.use(errorHandler); connectDB() - .then(() => { - console.log('db connected successfully.'); - }) + .then(() => {}) .catch(err => { console.error(err); process.exit(1); diff --git a/src/loaders/sentryConfiguration.ts b/src/loaders/sentryConfiguration.ts deleted file mode 100644 index 9d793da..0000000 --- a/src/loaders/sentryConfiguration.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { Express } from 'express'; -import { - init, - Handlers, - Integrations, - autoDiscoverNodePerformanceMonitoringIntegrations -} from '@sentry/node'; -import config from '../config'; - -const initializeSentry = (app: Express) => { - init({ - environment: config.nodeEnv, - dsn: config.sentryDsn, - integrations: [ - new Integrations.Http({ tracing: true }), - new Integrations.Express({ app }), - ...autoDiscoverNodePerformanceMonitoringIntegrations() - ], - tracesSampleRate: 1.0 - }); - app.use(Handlers.requestHandler()); - app.use(Handlers.tracingHandler()); -}; - -const attachSentryErrorHandler = (app: Express) => { - app.use(Handlers.errorHandler()); -}; - -export { initializeSentry, attachSentryErrorHandler }; diff --git a/src/middlewares/errorHandler.ts b/src/middlewares/errorHandler.ts index 51e7f21..3881bf8 100644 --- a/src/middlewares/errorHandler.ts +++ b/src/middlewares/errorHandler.ts @@ -2,6 +2,11 @@ import { NextFunction, Request, Response } from 'express'; import { PiickleException } from '../intefaces/exception'; import statusCode from '../modules/statusCode'; import util from '../modules/util'; +import { generateBlock } from '../modules/returnToSlack'; +import { IncomingWebhook } from '@slack/webhook'; +import config from '../config'; + +const webhook = new IncomingWebhook(config.slackWebHookUrl); const errHandler = ( err: any, req: Request, @@ -13,6 +18,14 @@ const errHandler = ( .status(err.statusCode) .send(util.fail(err.statusCode, err.message)); } + + webhook + .send(generateBlock(req, err)) + .then(() => {}) + .catch(error => { + console.error(error); + }); + return res .status(statusCode.INTERNAL_SERVER_ERROR) .send(util.fail(statusCode.INTERNAL_SERVER_ERROR, (err as Error).message)); diff --git a/src/modules/returnToSlack.ts b/src/modules/returnToSlack.ts index 264e90c..bcc2f0b 100644 --- a/src/modules/returnToSlack.ts +++ b/src/modules/returnToSlack.ts @@ -1,23 +1,58 @@ import { Request } from 'express'; -import { UserId } from '../types/types'; -export const generateSlackMessage = (req: Request, error: any): string => { +import { IncomingWebhookSendArguments } from '@slack/webhook/dist/IncomingWebhook'; + +export const generateBlock = ( + req: Request, + err: any +): IncomingWebhookSendArguments => { const method = req.method.toUpperCase(); const originalUrl = req.originalUrl; - const uid = req.user?.id; const token = req.header('x-auth-token')?.split(' ')[1]; - - const reqInfo = { - host: req.header('Host'), - userAgent: req.header('User-Agent'), - user: req.user, - jwt: token, - body: req.body + const error = err as { + statusCode: number; + message: string; + stack: any; + }; + return { + attachments: [ + { + color: '#FF0000', + blocks: [ + { + type: 'section', + text: { + type: 'mrkdwn', + text: `[${method}] \`${originalUrl}\`\n- *statusCode*: ${error.statusCode}\n- *message*: ${error.message}` + } + }, + { + type: 'section', + text: { + type: 'mrkdwn', + text: ` + *Error 상세*\`\`\`${error.stack}\`\`\`` + } + }, + { + type: 'section', + text: { + type: 'mrkdwn', + text: ` + *Request 상세*\`\`\`${JSON.stringify( + { + host: req.header('Host'), + userAgent: req.header('User-Agent'), + user: req.user, + jwt: token, + body: req.body + }, + null, + 2 + )}\`\`\`` + } + } + ] + } + ] }; - return `🚨 [${method}] \`${originalUrl}\`\n - *statusCode*: ${ - error.statusCode - }\n - *message*: ${error.message} \`\`\`${JSON.stringify( - reqInfo, - null, - 2 - )}\`\`\``; };