Skip to content
Closed
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
26 changes: 8 additions & 18 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,17 @@ async function bootstrap() {
app.set('view engine', 'hbs');
app.set('views', join(ROOT_DIR, 'views'));
app.use(express.static(join(ROOT_DIR, 'public')));

app.use('/store', express.static(join(ROOT_DIR, 'store')));

app.use('/', router);

app.use(
(err: Error, req: Request, res: Response, next: NextFunction) => {
if (err) {
const webhook = configService.get<Webhook>('WEBHOOK');

if (webhook.EVENTS.ERRORS_WEBHOOK && webhook.EVENTS.ERRORS_WEBHOOK != '' && webhook.EVENTS.ERRORS) {
const tzoffset = new Date().getTimezoneOffset() * 60000; //offset in milliseconds
if (webhook.EVENTS.ERRORS_WEBHOOK && webhook.EVENTS.ERRORS_WEBHOOK !== '' && webhook.EVENTS) {
const tzoffset = new Date().getTimezoneOffset() * 60000;
const localISOTime = new Date(Date.now() - tzoffset).toISOString();
const now = localISOTime;
const globalApiKey = configService.get<Auth>('AUTHENTICATION').API_KEY.KEY;
const serverUrl = configService.get<HttpServer>('SERVER').URL;

Expand All @@ -86,17 +83,14 @@ async function bootstrap() {
message: err['message'] || 'Internal Server Error',
},
},
date_time: now,
date_time: localISOTime,
api_key: globalApiKey,
server_url: serverUrl,
};

logger.error(errorData);

const baseURL = webhook.EVENTS.ERRORS_WEBHOOK;
const httpService = axios.create({ baseURL });

httpService.post('', errorData);
axios.create({ baseURL }).post('', errorData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: The axios POST request is not awaited or handled.

If you want to ensure error delivery, handle the promise or log failures from the POST request.

Suggested change
axios.create({ baseURL }).post('', errorData);
axios.create({ baseURL })
.post('', errorData)
.catch((postError) => {
logger.error('Failed to deliver error webhook:', postError);
});

}

return res.status(err['status'] || 500).json({
Expand All @@ -112,28 +106,26 @@ async function bootstrap() {
},
(req: Request, res: Response, next: NextFunction) => {
const { method, url } = req;

res.status(HttpStatus.NOT_FOUND).json({
status: HttpStatus.NOT_FOUND,
error: 'Not Found',
response: {
message: [`Cannot ${method.toUpperCase()} ${url}`],
},
});

next();
},
);

const httpServer = configService.get<HttpServer>('SERVER');
const PORT = process.env.PORT || httpServer.PORT || 8080;

ServerUP.app = app;
let server = ServerUP[httpServer.TYPE];

if (server === null) {
logger.warn('SSL cert load failed — falling back to HTTP.');
logger.info("Ensure 'SSL_CONF_PRIVKEY' and 'SSL_CONF_FULLCHAIN' env vars point to valid certificate files.");

httpServer.TYPE = 'http';
server = ServerUP[httpServer.TYPE];
}
Expand All @@ -142,16 +134,14 @@ async function bootstrap() {

if (process.env.SENTRY_DSN) {
logger.info('Sentry - ON');

// Add this after all routes,
// but before any and other error-handling middlewares are defined
Sentry.setupExpressErrorHandler(app);
}

server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT));
server.listen(PORT, () => {
logger.log(`${httpServer.TYPE.toUpperCase()} - ON: ${PORT}`);
});

initWA();

onUnexpectedError();
}

Expand Down