Skip to content

Commit 0d633e3

Browse files
fix: skip non json messages (#53)
1 parent 3013c98 commit 0d633e3

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/gc-pubsub.server.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,23 @@ describe('GCPubSubServer', () => {
183183
});
184184
expect(handler.calledOnce).to.be.true;
185185
});
186+
187+
it('should return undefined when data is not in JSON format', async () => {
188+
const result = await server.handleMessage({
189+
ackId: 'id',
190+
// @ts-ignore
191+
publishTime: new Date(),
192+
attributes: {},
193+
id: 'id',
194+
received: 0,
195+
deliveryAttempt: 1,
196+
ack: () => {},
197+
modAck: () => {},
198+
nack: () => {},
199+
data: Buffer.from('text'),
200+
});
201+
expect(result).to.be.undefined;
202+
});
186203
});
187204

188205
describe('sendMessage', () => {

lib/gc-pubsub.server.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,15 @@ export class GCPubSubServer
155155

156156
public async handleMessage(message: Message) {
157157
const { data, attributes } = message;
158-
const rawMessage = JSON.parse(data.toString());
158+
let rawMessage;
159+
try {
160+
rawMessage = JSON.parse(data.toString());
161+
} catch (error: any) {
162+
this.logger.error(
163+
`Unsupported JSON message data format for message '${message.id}'`,
164+
);
165+
return;
166+
}
159167

160168
let packet;
161169
if (attributes.pattern) {

0 commit comments

Comments
 (0)