Skip to main content
Version: v2

Internal Error Handling

To handle any internal error that might occur on pubsub side, the method handleError can be declared either in Subscriber Object or in SubscriptionService.

  • If the error handler is provided at Subscriber object level then it would override the error handler provided at SubscriptionService level.

Subscriber handleError Examples#

Typescript subscription example#

/pubsub/subscriptions/simple.topic.name.console-log.sub.ts
import { SubscriberObject, Message } from "@honestfoodcompany/pubsub";
export default: SubscriberObject = {
topicName: 'simple.topic',
subscriptionName: 'simple.topic.console-log.sub',
description: 'Will console log messages published on test.topic',
handleMessage: function(message: Message): void {
console.log(this.subscriptionName, 'received message');
console.log(message.data.toString());
message.ack();
},
handleError: function(error: Error): void {
// internal error handling logic for subscriber
console.error(error);
// Close DB connections/etc here
process.exit(1);
}
};

Javascript subscription example#

/pubsub/subscriptions/simple.topic.name.sub.js
exports.default = {
topicName: 'test.topic',
subscriptionName: 'test.topic.sub',
description: 'Will console log messages published on test.topic',
handleMessage: function (message) {
console.log(this.subscriptionName, 'received message');
console.log(message.data.toString());
message.ack();
},
handleError: function (error) {
// internal error handling logic for subscriber
console.error(error);
// Close DB connections/etc here
process.exit(1);
},
};

SubscriptionService handleError Examples#

You can also setup a global error handler that will act as the default for all subscriptions, this though also receives the subscription metadat for which the error happens.

Typescript subscription example#

/pubsub/subscription.service.ts
import { SubscriptionService, SubscriberOptions, SubscriberMetadata } from '@honestfoodcompany/pubsub';
export default class SubscriptionService extends PubSub.SubscriptionService {
static subscribers = [
//...
];
static defaultSubscriberOptions: SubscriberOptions = {};
static async init(): Promise<void> {}
static handleError(error: Error, metadata: SubscriberMetadata): void {
// global default error handling logic for all subscribers
console.error({err, metadata}, 'Default error handler');
// Close DB connections/etc here
process.exit(1);
}
}

Javascript subscription example#

/pubsub/subscription.service.js
const PubSub = require('@honestfoodcompany/pubsub');
class SubscriptionService extends PubSub.SubscriptionService {}
SubscriptionService.subscribers = [];
SubscriptionService.defaultSubscriberOptions = {};
SubscriptionService.init = () => {};
SubscriptionService.handleError = (error, metadata) => {
// global default error handling logic for all subscribers
console.error({err, metadata}, 'Default error handler');
// Close DB connections/etc here
process.exit(1);
};
exports.default = SubscriptionService;