Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: include detailed error #229

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 1 addition & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down
29 changes: 0 additions & 29 deletions src/loaders/sentryConfiguration.ts

This file was deleted.

13 changes: 13 additions & 0 deletions src/middlewares/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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));
Expand Down
69 changes: 52 additions & 17 deletions src/modules/returnToSlack.ts
Original file line number Diff line number Diff line change
@@ -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 = <UserId>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
)}\`\`\``;
};
Loading