Skip to content

Commit 0ab7d73

Browse files
committed
Added try catch for creating notion page
1 parent e5682bb commit 0ab7d73

File tree

1 file changed

+41
-24
lines changed

1 file changed

+41
-24
lines changed

apps/contact/helpers/notion.ts

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -116,34 +116,36 @@ const createContact = async (
116116
databaseID: string,
117117
source: string,
118118
) => {
119-
const response = await notion.pages.create(
120-
createContactObject(id, email, name, content, databaseID, source),
121-
);
119+
try {
120+
const response = await notion.pages.create(
121+
createContactObject(id, email, name, content, databaseID, source),
122+
);
122123

123-
// isFullPage checks if the response is type PageObjectResponse => contains url
124-
if (response.id && isFullPage(response)) {
124+
// isFullPage checks if the response is type PageObjectResponse => contains url
125+
if (response.id && isFullPage(response)) {
126+
return {
127+
id: response.id,
128+
url: response.url,
129+
};
130+
// In case the page is created but the response is type PartialPageObjectResponse => doesn't contain url
131+
} else if (response.id && !isFullPage(response)) {
132+
// Notion allows navigation to the created page using only the id without '-'
133+
// https://dev.to/adamcoster/change-a-url-without-breaking-existing-links-4m0d
134+
const cleanId = response.id.replace(/-/g, "");
135+
const pageUrl = `https://www.notion.so/${cleanId}`;
136+
return {
137+
id: response.id,
138+
url: pageUrl,
139+
};
140+
}
125141
return {
126-
id: response.id,
127-
url: response.url,
142+
message: "Failed to create notion page",
128143
};
129-
// In case the page is created but the response is type PartialPageObjectResponse => doesn't contain url
130-
} else if (response.id && !isFullPage(response)) {
131-
// Notion allows navigation to the created page using only the id without '-'
132-
// https://dev.to/adamcoster/change-a-url-without-breaking-existing-links-4m0d
133-
const cleanId = response.id.replace(/-/g, "");
134-
const pageUrl = `https://www.notion.so/${cleanId}`;
144+
} catch (error) {
135145
return {
136-
id: response.id,
137-
url: pageUrl,
146+
message: "Failed to create notion page",
138147
};
139148
}
140-
141-
await notifyContactError(name, email, content);
142-
throw {
143-
body: {
144-
message: "Failed to create notion page",
145-
},
146-
};
147149
};
148150

149151
export const processContact = async (event: {
@@ -165,7 +167,11 @@ export const processContact = async (event: {
165167
};
166168
}
167169

168-
const { id: notionPageID, url } = await createContact(
170+
const {
171+
id: notionPageID,
172+
url,
173+
message: errorMessage,
174+
} = await createContact(
169175
`Message from ${name} (${id})`,
170176
email,
171177
name,
@@ -174,6 +180,17 @@ export const processContact = async (event: {
174180
source,
175181
);
176182

177-
await notifyContactCreated(name, email, url);
183+
if (errorMessage) {
184+
await notifyContactError(name, email, message);
185+
throw {
186+
body: {
187+
message: errorMessage,
188+
},
189+
};
190+
}
191+
192+
if (url) {
193+
await notifyContactCreated(name, email, url);
194+
}
178195
return notionPageID;
179196
};

0 commit comments

Comments
 (0)