Skip to content

Commit 6e1f259

Browse files
committed
Added missing try-catch
1 parent a9d2933 commit 6e1f259

3 files changed

Lines changed: 39 additions & 30 deletions

File tree

apps/contact/helpers/notion.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export const createContact = async (
141141
url: pageUrl,
142142
};
143143
}
144+
console.error("Notion hepler => Failed to create notion page");
144145
return {
145146
error: "Failed to create notion page",
146147
};

apps/contact/helpers/slack.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe("Slack Helper", () => {
4343
email: "john@example.com",
4444
url: "https://notion.so/test",
4545
};
46-
const errorMessage = "Error message";
46+
const userMessage = "Test message";
4747
describe("notifyContactCreated", async () => {
4848
it("should send message on slack", async () => {
4949
await notifyContactCreated(
@@ -57,7 +57,7 @@ describe("Slack Helper", () => {
5757
expect(fetchCalls[0].init.body).toContain(validContactData.name);
5858
expect(fetchCalls[0].init.body).toContain(validContactData.email);
5959
expect(fetchCalls[0].init.body).toContain(validContactData.url);
60-
expect(fetchCalls[0].init.body).not.toContain(errorMessage);
60+
expect(fetchCalls[0].init.body).not.toContain(userMessage);
6161
});
6262
});
6363

@@ -66,15 +66,15 @@ describe("Slack Helper", () => {
6666
await notifyContactError(
6767
validContactData.name,
6868
validContactData.email,
69-
errorMessage,
69+
userMessage,
7070
);
7171

7272
expect(fetchCalls.length).toBe(1);
7373
expect(fetchCalls[0].url).toBe("https://slack.com/api/chat.postMessage");
7474
expect(fetchCalls[0].init.body).toContain(validContactData.name);
7575
expect(fetchCalls[0].init.body).toContain(validContactData.email);
7676
expect(fetchCalls[0].init.body).not.toContain(validContactData.url);
77-
expect(fetchCalls[0].init.body).toContain(errorMessage);
77+
expect(fetchCalls[0].init.body).toContain(userMessage);
7878
});
7979
});
8080
});

apps/contact/helpers/slack.ts

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,23 @@ export const notifyContactCreated = async (
7575
const payload = createPayload(name, email, url);
7676
const payloadStringify = JSON.stringify(payload);
7777

78-
const result = await fetch("https://slack.com/api/chat.postMessage", {
79-
method: "POST",
80-
body: payloadStringify,
81-
headers: {
82-
"Content-Type": "application/json; charset=utf-8",
83-
"Content-Length": payloadStringify.length.toString(),
84-
Authorization: `Bearer ${SLACK_BOT_TOKEN}`,
85-
Accept: "application/json",
86-
},
87-
});
88-
const realResponse = await result.json();
89-
if (result.status !== 200 || !realResponse.ok) {
90-
console.error("Could not send notification message to Slack");
78+
try {
79+
const result = await fetch("https://slack.com/api/chat.postMessage", {
80+
method: "POST",
81+
body: payloadStringify,
82+
headers: {
83+
"Content-Type": "application/json; charset=utf-8",
84+
"Content-Length": payloadStringify.length.toString(),
85+
Authorization: `Bearer ${SLACK_BOT_TOKEN}`,
86+
Accept: "application/json",
87+
},
88+
});
89+
const realResponse = await result.json();
90+
if (result.status !== 200 || !realResponse.ok) {
91+
console.error("Could not send notification message to Slack");
92+
}
93+
} catch (error) {
94+
console.error("Could not send notification message to Slack", error);
9195
}
9296
};
9397

@@ -99,18 +103,22 @@ export const notifyContactError = async (
99103
const payload = createErrorPayload(name, email, content);
100104
const payloadStringify = JSON.stringify(payload);
101105

102-
const result = await fetch("https://slack.com/api/chat.postMessage", {
103-
method: "POST",
104-
body: payloadStringify,
105-
headers: {
106-
"Content-Type": "application/json; charset=utf-8",
107-
"Content-Length": payloadStringify.length.toString(),
108-
Authorization: `Bearer ${SLACK_BOT_TOKEN}`,
109-
Accept: "application/json",
110-
},
111-
});
112-
const realResponse = await result.json();
113-
if (result.status !== 200 || !realResponse.ok) {
114-
console.error("Could not send notification message to Slack");
106+
try {
107+
const result = await fetch("https://slack.com/api/chat.postMessage", {
108+
method: "POST",
109+
body: payloadStringify,
110+
headers: {
111+
"Content-Type": "application/json; charset=utf-8",
112+
"Content-Length": payloadStringify.length.toString(),
113+
Authorization: `Bearer ${SLACK_BOT_TOKEN}`,
114+
Accept: "application/json",
115+
},
116+
});
117+
const realResponse = await result.json();
118+
if (result.status !== 200 || !realResponse.ok) {
119+
console.error("Could not send notification message to Slack");
120+
}
121+
} catch (error) {
122+
console.error("Could not send notification message to Slack", error);
115123
}
116124
};

0 commit comments

Comments
 (0)