Skip to content

Commit 5935d4e

Browse files
committed
Improved error handling
1 parent ea15558 commit 5935d4e

File tree

2 files changed

+47
-24
lines changed

2 files changed

+47
-24
lines changed

src/fedify/mq/gcloud-pubsub-mq.ts

+41-19
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type { Message, PubSub } from '@google-cloud/pubsub';
2-
31
import type {
42
MessageQueue,
53
MessageQueueEnqueueOptions,
64
MessageQueueListenOptions,
75
} from '@fedify/fedify';
6+
import type { Message, PubSub } from '@google-cloud/pubsub';
87
import type { Logger } from '@logtape/logtape';
8+
import * as Sentry from '@sentry/node';
99

1010
export class GCloudPubSubMessageQueue implements MessageQueue {
1111
private pubSubClient: PubSub;
@@ -56,6 +56,10 @@ export class GCloudPubSubMessageQueue implements MessageQueue {
5656
this.logger.error(
5757
`Failed to enqueue message [FedifyID: ${message.id}]: ${error}`,
5858
);
59+
60+
Sentry.captureException(error);
61+
62+
throw error;
5963
}
6064
}
6165

@@ -67,31 +71,49 @@ export class GCloudPubSubMessageQueue implements MessageQueue {
6771
this.subscriptionIdentifier,
6872
);
6973

70-
subscription.on('message', async (message: Message) => {
71-
const fedifyId = message.attributes.fedifyId ?? 'unknown';
74+
subscription
75+
.on('message', async (message: Message) => {
76+
const fedifyId = message.attributes.fedifyId ?? 'unknown';
7277

73-
this.logger.info(
74-
`Handling message [FedifyID: ${fedifyId}, PubSubID: ${message.id}]`,
75-
);
78+
this.logger.info(
79+
`Handling message [FedifyID: ${fedifyId}, PubSubID: ${message.id}]`,
80+
);
7681

77-
try {
78-
const json = JSON.parse(message.data.toString());
82+
try {
83+
const json = JSON.parse(message.data.toString());
7984

80-
await handler(json);
85+
await handler(json);
8186

82-
message.ack();
87+
message.ack();
8388

84-
this.logger.info(
85-
`Acknowledged message [FedifyID: ${fedifyId}, PubSubID: ${message.id}]`,
86-
);
87-
} catch (error) {
88-
message.nack();
89+
this.logger.info(
90+
`Acknowledged message [FedifyID: ${fedifyId}, PubSubID: ${message.id}]`,
91+
);
92+
} catch (error) {
93+
message.nack();
94+
95+
this.logger.error(
96+
`Failed to handle message [FedifyID: ${fedifyId}, PubSubID: ${message.id}]: ${error}`,
97+
);
8998

99+
Sentry.captureException(error);
100+
}
101+
})
102+
.on('error', (error) => {
90103
this.logger.error(
91-
`Failed to handle message [FedifyID: ${fedifyId}, PubSubID: ${message.id}]: ${error}`,
104+
`Subscription [${this.subscriptionIdentifier}] error occurred: ${error}`,
92105
);
93-
}
94-
});
106+
107+
Sentry.captureException(error);
108+
109+
// This is a fatal error, so we should throw to stop the listener / process
110+
throw error;
111+
})
112+
.on('close', () => {
113+
this.logger.info(
114+
`Subscription [${this.subscriptionIdentifier}] closed`,
115+
);
116+
});
95117

96118
return await new Promise((resolve) => {
97119
options.signal?.addEventListener('abort', () => {

src/fedify/mq/gcloud-pubsub-mq.unit.test.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ describe('enqueue', () => {
9393
);
9494
});
9595

96-
it('should log an error if the message fails to be published', async () => {
97-
mockTopic.publishMessage = vi
98-
.fn()
99-
.mockRejectedValue(new Error('Failed to publish message'));
96+
it('should throw an error and log if the message fails to be published', async () => {
97+
const error = new Error('Failed to publish message');
98+
99+
mockTopic.publishMessage = vi.fn().mockRejectedValue(error);
100100

101101
const enqueuePromise = messageQueue.enqueue(MESSAGE);
102102

103103
vi.runAllTimers();
104-
await enqueuePromise;
104+
await expect(enqueuePromise).rejects.toThrow(error);
105105

106106
expect(mockLogger.error).toHaveBeenCalledWith(
107107
`Failed to enqueue message [FedifyID: ${MESSAGE.id}]: Error: Failed to publish message`,
@@ -145,6 +145,7 @@ describe('listen', () => {
145145
if (event === 'message') {
146146
callback(MESSAGE);
147147
}
148+
return mockSubscription;
148149
},
149150
),
150151
removeAllListeners: vi.fn().mockReturnThis(),

0 commit comments

Comments
 (0)