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: queue notification #14

Merged
merged 2 commits into from
Sep 4, 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 .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"adminjs",
"aicommit",
"claudeai",
"fastq",
"healthz",
"nofollow",
"openai",
Expand Down
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dotenv": "^16.4.5",
"express": "^4.19.2",
"express-rate-limit": "^7.3.1",
"fastq": "^1.17.1",
"helmet": "^7.1.0",
"openai": "^4.52.7",
"typescript": "^5.5.3"
Expand Down
42 changes: 2 additions & 40 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import h from 'helmet';
import { appConfig } from './config';
import { rateLimit } from 'express-rate-limit';
import { logger, statusCode, html } from './util';
import { logger, statusCode, html, queueNotification } from './util';
import { NextFunction, Request, Response } from 'express';

export function helmet() {
Expand Down Expand Up @@ -70,46 +70,8 @@ export function errorMiddleware() {
]);

return async (error: Error, req: Request, res: Response, next: NextFunction) => {
// if (appConfig.NODE_ENV === 'production' && !(error instanceof NotFoundError)) {
if (appConfig.NODE_ENV === 'production') {
try {
const n = await fetch(appConfig.NOTIFY_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-KEY': appConfig.NOTIFY_X_API_KEY,
},
body: JSON.stringify({
message: `Error: ${error.message}`,
details: JSON.stringify(
{
request: {
method: req.method,
url: req.url,
headers: req.headers,
query: req.query,
body: req.body,
},
error: {
name: error?.name,
message: error?.message,
stack: error?.stack,
cause: error?.cause,
},
},
null,
2,
),
}),
});

if (!n.ok) {
const text = await n.text();
throw new Error(`Notification service responded with status ${n.status}: ${text}`);
}
} catch (error) {
logger.error('Failed to send error notification', error);
}
queueNotification(req, error);
}

for (const [ErrorClass, statusCode] of errorMap) {
Expand Down
50 changes: 50 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,57 @@
import fastq from 'fastq';
import { Request } from 'express';
import { styleText } from 'node:util';
import { appConfig } from './config';
import { CacheType, ConfigItem, Logger } from './types';

// @ts-expect-error - fix this
const queue = fastq.promise(sendNotification, 1);

export function queueNotification(req: Request, error: Error) {
queue.push({ req, error });
}

export async function sendNotification(req: Request, error: Error) {
try {
const n = await fetch(appConfig.NOTIFY_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-KEY': appConfig.NOTIFY_X_API_KEY,
},
body: JSON.stringify({
message: `Error: ${error?.message}`,
details: JSON.stringify(
{
request: {
method: req.method,
url: req.url,
headers: req.headers,
query: req.query,
body: req.body,
},
error: {
name: error?.name,
message: error?.message,
stack: error?.stack,
cause: error?.cause,
},
},
null,
2,
),
}),
});

if (!n.ok) {
const text = await n.text();
logger.error(`Notification service responded with status ${n.status}: ${text}`);
}
} catch (error) {
logger.error('Failed to send error notification', error);
}
}

export const statusCode = Object.freeze({
INTERNAL_SERVER_ERROR: 500 as number,
FORBIDDEN: 403,
Expand Down
Loading