Skip to content

Commit 5a1eb82

Browse files
author
Ryan Mottley
committed
fix: add more error handling to SNP message handler
1 parent 8f9c6e9 commit 5a1eb82

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

src/event-stream/snp-event-stream.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,22 @@ export class SnpEventStreamHandler {
5959

6060
async handleMsg(messageId: string, timestamp: string, path: string, body: any) {
6161
this.logger.debug(`Received SNP stream event ${path}, msgId: ${messageId}`);
62+
let response;
6263

63-
const response = await this.eventServer.fastifyInstance.inject({
64-
method: 'POST',
65-
url: path,
66-
payload: body,
67-
});
64+
try {
65+
response = await this.eventServer.fastifyInstance.inject({
66+
method: 'POST',
67+
url: path,
68+
payload: body,
69+
});
70+
} catch (error) {
71+
// eslint-disable-next-line prettier/prettier
72+
const errorMessage = `Failed to process SNP message ${messageId} at path ${path}: ${(error as Error).message}`;
73+
this.logger.error(errorMessage);
74+
throw new Error(errorMessage);
75+
}
6876

69-
if (response.statusCode < 200 || response.statusCode > 299) {
77+
if (response?.statusCode < 200 || response?.statusCode > 299) {
7078
const errorMessage = `Failed to process SNP message ${messageId} at path ${path}, status: ${response.statusCode}, body: ${response.body}`;
7179
this.logger.error(errorMessage);
7280
throw new Error(errorMessage);

tests/snp/snp-ingestion.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,49 @@ describe('SNP integration tests', () => {
127127
hash: sampleEventsLastBlockHash,
128128
});
129129
});
130+
131+
test('handleMsg throws error when inject() fails', async () => {
132+
const snpClient = new SnpEventStreamHandler({
133+
db,
134+
eventServer,
135+
lastMessageId: '0',
136+
});
137+
138+
const originalInject = eventServer.fastifyInstance.inject.bind(eventServer.fastifyInstance);
139+
eventServer.fastifyInstance.inject = () => {
140+
throw new Error('Simulated inject failure');
141+
};
142+
143+
await expect(
144+
snpClient.handleMsg('test-msg-id', '2024-01-01T00:00:00Z', '/test/path', {})
145+
).rejects.toThrow(
146+
'Failed to process SNP message test-msg-id at path /test/path: Simulated inject failure'
147+
);
148+
149+
eventServer.fastifyInstance.inject = originalInject;
150+
});
151+
152+
test('handleMsg throws error when response status is not 200', async () => {
153+
const snpClient = new SnpEventStreamHandler({
154+
db,
155+
eventServer,
156+
lastMessageId: '0',
157+
});
158+
159+
const originalInject = eventServer.fastifyInstance.inject.bind(eventServer.fastifyInstance);
160+
eventServer.fastifyInstance.inject = (() => {
161+
return Promise.resolve({
162+
statusCode: 500,
163+
body: 'Internal Server Error',
164+
});
165+
}) as typeof eventServer.fastifyInstance.inject;
166+
167+
await expect(
168+
snpClient.handleMsg('test-msg-id', '2024-01-01T00:00:00Z', '/test/path', {})
169+
).rejects.toThrow(
170+
'Failed to process SNP message test-msg-id at path /test/path, status: 500, body: Internal Server Error'
171+
);
172+
173+
eventServer.fastifyInstance.inject = originalInject;
174+
});
130175
});

0 commit comments

Comments
 (0)