Skip to content

Commit 00982f5

Browse files
feat: microservice now able to handle multiple errors
1 parent cc61e1f commit 00982f5

File tree

3 files changed

+119
-105
lines changed

3 files changed

+119
-105
lines changed

src/controllers/errorController.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ import { Request, Response, NextFunction } from "express";
22
import { categorizeError } from "../services/categorizationService";
33

44
export interface ProcessedError {
5-
channelId: string,
6-
message: string;
5+
channelId: string;
76
type: string;
8-
priority: string;
9-
extra?: {
10-
timestamp: string;
11-
};
7+
errors: ErrorItem[];
8+
timestamp: string;
9+
priority?: string;
10+
}
11+
12+
export interface ErrorItem {
13+
message: string;
14+
stack: string;
1215
}
1316

1417
let lastProcessedError: ProcessedError | null = null;
@@ -19,21 +22,28 @@ export const handleIncomingError = (
1922
next: NextFunction
2023
): void => {
2124
try {
22-
const { message, stack, channelId } = req.body;
25+
const { channelId, type, errors, timestamp } = req.body;
2326

24-
if (!message || !stack || channelId) {
27+
if (!channelId || !type || !Array.isArray(errors) || errors.length === 0) {
2528
res.status(400).json({ error: "Invalid error report format." });
2629
return;
2730
}
2831

29-
const severity = categorizeError(message);
32+
const highestSeverity = errors
33+
.map(err => categorizeError(err.message))
34+
.reduce((prev, current) =>
35+
current === "High" ? current :
36+
(prev === "High" ? prev :
37+
(current === "Medium" ? current : prev)),
38+
"Low"
39+
);
3040

3141
lastProcessedError = {
3242
channelId,
33-
message,
34-
type: "Error",
35-
priority: severity,
36-
extra: { timestamp: new Date().toISOString() },
43+
type,
44+
errors,
45+
timestamp: timestamp || new Date().toISOString(),
46+
priority: highestSeverity
3747
};
3848

3949
res.status(202).json({ status: "accepted" });

src/routes/tick.ts

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -23,62 +23,62 @@ router.post("/tick", async (req: Request, res: Response) => {
2323
return;
2424
}
2525

26-
const message = `
27-
Error Report Details:
28-
Message: ${refinedError.message}
29-
Type: ${refinedError.type}
30-
Priority: ${refinedError.priority}
31-
Timestamp: ${refinedError.extra?.timestamp}
32-
Reported By: Code Error Agent
33-
Event: Processed Error Report
34-
Status: ${refinedError.priority === "High" ? "error" : "info"}
35-
Processing Time: ${new Date().toISOString()}
36-
Performed By: your-username
37-
Source: error processing
38-
Full Error Details: ${JSON.stringify(refinedError, null, 2)}
39-
`.trim();
26+
const errorSummary = refinedError.errors.map(err => ({
27+
message: err.message,
28+
stack: err.stack
29+
}));
4030

41-
const telexPayload = {
42-
"event_name": "Code Error Monitor Agent",
43-
"message": message,
44-
"status": "success",
45-
"username": "Agent Sapa"
46-
};
4731

48-
console.log(telexPayload.message);
49-
const response = await axios.post(`${webhookUrl}/${payload.channel_id}`, telexPayload, {
50-
headers: {
51-
"Content-Type": "application/json",
52-
"User-Agent": "Code Error Agent/1.0.0",
53-
},
54-
});
55-
56-
if (response.status !== 200 && response.status !== 202) {
57-
throw new Error(`Failed to forward error to Telex: ${response.statusText}`);
58-
}
59-
} catch (error: any) {
60-
console.error(`[${new Date().toISOString()}] Error in tick endpoint:`, error);
61-
if (payload?.return_url) {
62-
try {
63-
await axios.post(payload.return_url, {
64-
message: `❌ Error processing error event: ${error.message}`,
65-
username: "Code Error Agent",
66-
event_name: "Error Processing Failure",
67-
status: "error",
68-
timestamp: new Date().toISOString(),
69-
performed_by: "your-username",
70-
metadata: {
71-
error: error.message,
32+
const message = `
33+
Errors:
34+
${errorSummary.map((err, index) => `
35+
Error ${index + 1}:
36+
Message: ${err.message}
37+
Stack: ${err.stack}
38+
`).join('\n')}
39+
`.trim();
40+
41+
const telexPayload = {
42+
"event_name": "Code Error Monitor Agent",
43+
"message": message,
44+
"status": "success",
45+
"username": "Agent Sapa"
46+
};
47+
48+
console.log(telexPayload.message);
49+
const response = await axios.post(`${webhookUrl}/${payload.channel_id}`, telexPayload, {
50+
headers: {
51+
"Content-Type": "application/json",
52+
"User-Agent": "Code Error Agent/1.0.0",
7253
},
7354
});
74-
} catch (notifyError) {
75-
console.error(
76-
`[${new Date().toISOString()}] Failed to notify Telex about error:`,
77-
notifyError
78-
);
55+
56+
if (response.status !== 200 && response.status !== 202) {
57+
throw new Error(`Failed to forward error to Telex: ${response.statusText}`);
58+
}
59+
} catch (error: any) {
60+
console.error(`[${new Date().toISOString()}] Error in tick endpoint:`, error);
61+
if (payload?.return_url) {
62+
try {
63+
await axios.post(payload.return_url, {
64+
message: `❌ Error processing error event: ${error.message}`,
65+
username: "Code Error Agent",
66+
event_name: "Error Processing Failure",
67+
status: "error",
68+
timestamp: new Date().toISOString(),
69+
performed_by: "your-username",
70+
metadata: {
71+
error: error.message,
72+
},
73+
});
74+
} catch (notifyError) {
75+
console.error(
76+
`[${new Date().toISOString()}] Failed to notify Telex about error:`,
77+
notifyError
78+
);
79+
}
80+
}
7981
}
80-
}
81-
}
82-
});
83-
84-
export default router;
82+
});
83+
84+
export default router;

src/services/zeromqService.ts

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as zmq from "zeromq";
22
import {
3-
getLastProcessedError,
43
ProcessedError,
54
} from "../controllers/errorController";
65
import axios from "axios";
@@ -12,8 +11,8 @@ async function initializeServer() {
1211
const publishSocket = new zmq.Publisher();
1312

1413
try {
15-
await replySocket.bind("tcp://code-error-microservice.onrender.com:3030");
16-
await publishSocket.bind("tcp://code-error-microservice.onrender.com:3031");
14+
await replySocket.bind("tcp://127.0.0.1:3030");
15+
await publishSocket.bind("tcp://127.0.0.1:3031");
1716
console.log("ZeroMQ server bound to ports 3030 (Reply) and 3031 (Publish)");
1817

1918
const serverPublish = async (message: string) => {
@@ -29,57 +28,62 @@ async function initializeServer() {
2928
parsedMessage = JSON.parse(msg.toString()) as unknown as ProcessedError;
3029
} catch (error) {
3130
parsedMessage = msg.toString() as unknown as ProcessedError;
31+
console.error("Failed to parse message:", error);
32+
await replySocket.send(JSON.stringify({ status: "error", message: "Invalid message format" }));
33+
continue;
3234
}
33-
34-
const refinedError: ProcessedError | null = parsedMessage;
35-
if (!refinedError) {
36-
console.warn("No processed error available for reporting.");
35+
if (!parsedMessage || !parsedMessage.channelId || !parsedMessage.errors) {
36+
console.warn("Invalid message format");
37+
await replySocket.send(JSON.stringify({ status: "error", message: "Invalid message format" }));
3738
continue;
3839
}
3940

40-
const message = `
41-
Error Report Details:
42-
Message: ${refinedError.message}
43-
Type: ${refinedError.type}
44-
Priority: ${refinedError.priority}
45-
Timestamp: ${refinedError.extra?.timestamp}
46-
Reported By: Code Error Agent
47-
Event: Processed Error Report
48-
Status: ${refinedError.priority === "High" ? "error" : "info"}
49-
Processing Time: ${new Date().toISOString()}
50-
Performed By: your-username
51-
Source: error processing
52-
Full Error Details: ${JSON.stringify(refinedError, null, 2)}
53-
`.trim();
41+
const errorSummary = parsedMessage.errors.map(err => ({
42+
message: err.message,
43+
stack: err.stack
44+
}));
45+
46+
const message = `
47+
Errors:
48+
${errorSummary.map((err, index) => `
49+
Error ${index + 1}:
50+
Message: ${err.message}
51+
Stack: ${err.stack}
52+
`).join('\n')}
53+
`.trim();
5454

5555
const telexPayload = {
5656
event_name: "Code Error Monitor Agent",
5757
message: message,
5858
status: "success",
59-
username: "Agent Sapa",
59+
username: "Code Error Agent",
6060
};
61+
try {
62+
const response = await axios.post(
63+
`${webhookUrl}/${parsedMessage.channelId}`,
64+
telexPayload,
65+
{
66+
headers: {
67+
"Content-Type": "application/json",
68+
"User-Agent": "Code Error Agent/1.0.0",
69+
},
70+
}
71+
);
6172

62-
const response = await axios.post(
63-
`${webhookUrl}/${refinedError.channelId}`,
64-
telexPayload,
65-
{
66-
headers: {
67-
"Content-Type": "application/json",
68-
"User-Agent": "Code Error Agent/1.0.0",
69-
},
70-
}
71-
);
72-
73-
console.log("response data", response?.data);
74-
75-
await replySocket.send(JSON.stringify({ status: "success" }));
73+
console.log("response data", response?.data);
74+
await replySocket.send(JSON.stringify({ status: "success" }));
75+
} catch (error) {
76+
console.error("Failed to send to webhook:", error);
77+
await replySocket.send(JSON.stringify({ status: "error", message: "Failed to send to webhook" }));
78+
}
7679
}
7780

7881
return { serverPublish };
7982
} catch (error) {
8083
console.error("ZeroMQ server error:", error);
8184
throw error;
82-
}
85+
}
86+
8387
}
8488

85-
export const zeromqClient = initializeServer();
89+
export const zeromqClient = initializeServer();

0 commit comments

Comments
 (0)