Skip to content

Commit 06ea05d

Browse files
committed
♻️ refactor error handling and logging in streamContacts
1 parent 52d7e9b commit 06ea05d

File tree

2 files changed

+11
-23
lines changed

2 files changed

+11
-23
lines changed

src/models/controller.model.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
ServerError,
1515
} from ".";
1616
import { calendarEventsSchema, contactsSchema } from "../schemas";
17-
import { isProduction } from "../util";
1817
import { shouldSkipCallEvent } from "../util/call-event.util";
1918
import { errorLogger, infoLogger } from "../util/logger.util";
2019
import { parsePhoneNumber } from "../util/phone-number-utils";
@@ -54,12 +53,13 @@ export class Controller {
5453
this.contactCache = contactCache;
5554
this.ajv = new Ajv();
5655

57-
const { PUBSUB_TOPIC_NAME: topicName } = process.env;
56+
if (this.adapter.streamContacts) {
57+
const { PUBSUB_TOPIC_NAME: topicName } = process.env;
5858

59-
if (isProduction() && typeof this.adapter.streamContacts === "function") {
6059
if (!topicName) {
6160
throw new Error("No pubsub topic name provided.");
6261
}
62+
6363
this.pubSubClient = new PubSubClient(topicName);
6464
infoLogger(
6565
"Controller",
@@ -215,7 +215,7 @@ export class Controller {
215215
} catch (error) {
216216
errorLogger(
217217
"streamContacts",
218-
"Could not publish contacts",
218+
`Could not publish contacts`,
219219
providerConfig.apiKey,
220220
error
221221
);

src/models/pubsub-client.model.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { PubSub } from "@google-cloud/pubsub";
2-
import { errorLogger, infoLogger } from "../util";
32
import { PubSubContactsMessage } from "./pubsub-contacts-message.model";
43

54
export class PubSubClient {
@@ -12,24 +11,13 @@ export class PubSubClient {
1211
}
1312

1413
async publishMessage(message: PubSubContactsMessage) {
15-
try {
16-
if (!this.topicName) {
17-
throw new Error("No pubsub topic name provided.");
18-
}
19-
20-
const json = JSON.stringify(message);
21-
const dataBuffer = Buffer.from(json);
22-
const topic = this.client.topic(this.topicName);
23-
await topic.publishMessage({ data: dataBuffer });
24-
25-
infoLogger(
26-
PubSubClient.name,
27-
`Published ${message.contacts.length} contacts for user ${message.userId} to topic ${this.topicName}`
28-
);
29-
} catch (error) {
30-
console.error(error);
31-
const message = (error as Error).message;
32-
errorLogger(PubSubClient.name, `Could not publish to pubsub: ${message}`);
14+
if (!this.topicName) {
15+
throw new Error("No pubsub topic name provided.");
3316
}
17+
18+
const json = JSON.stringify(message);
19+
const dataBuffer = Buffer.from(json);
20+
const topic = this.client.topic(this.topicName);
21+
await topic.publishMessage({ data: dataBuffer });
3422
}
3523
}

0 commit comments

Comments
 (0)