Skip to content

Commit 2da5372

Browse files
committed
🐛 handle convert kit api errors
1 parent d85e00d commit 2da5372

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

src/routes/(site)/snackpack/+page.server.ts

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,26 @@ type BroadCastResponse = {
2929
broadcasts: BroadcastSkinny[];
3030
};
3131

32+
async function parseResponse(res: Response) {
33+
try {
34+
return await res.json() as Promise<BroadCastResponse>;
35+
} catch {
36+
return {
37+
broadcasts: [],
38+
}
39+
}
40+
}
41+
3242
async function fetchBroadcastList() {
3343
// 1. Fetch all broadcasts from ConvertKit
3444
// 2 pages - 100 broadcasts should be enough. This will need to be updated if we ever have more than 100 issues
3545
const responses = await Promise.all([
3646
fetch(
3747
`https://api.convertkit.com/v3/broadcasts?page=1&api_secret=${env.CONVERT_KIT_SECRET}`
38-
).then((res) => res.json() as Promise<BroadCastResponse>),
48+
).then(parseResponse),
3949
fetch(
4050
`https://api.convertkit.com/v3/broadcasts?page=2&api_secret=${env.CONVERT_KIT_SECRET}`
41-
).then((res) => res.json() as Promise<BroadCastResponse>)
51+
).then(parseResponse),
4252
]);
4353

4454
const broadcasts = responses
@@ -63,29 +73,33 @@ async function fetchBroadcastList() {
6373
});
6474

6575
// Now we need to hit the ConvertKit API for every single broadcast to get info on if this broadcast was published, as well as the associated HTML for each snackpack
66-
const broadcastsWithData = (
67-
await Promise.all(
68-
broadcasts.map(async (broadcast) => {
69-
const res = await fetch(
70-
`https://api.convertkit.com/v3/broadcasts/${broadcast.id}?api_secret=${env.CONVERT_KIT_SECRET}`
71-
);
72-
const data = await res.json();
73-
return data.broadcast as Broadcast;
74-
})
76+
try {
77+
const broadcastsWithData = (
78+
await Promise.all(
79+
broadcasts.map(async (broadcast) => {
80+
const res = await fetch(
81+
`https://api.convertkit.com/v3/broadcasts/${broadcast.id}?api_secret=${env.CONVERT_KIT_SECRET}`
82+
);
83+
const data = await res.json();
84+
return data.broadcast as Broadcast;
85+
})
86+
)
7587
)
76-
)
77-
// filter for public broadcasts
78-
.filter((broadcast) => broadcast.public)
79-
// trim to only the fields we need (published_at, subject)
80-
.map((broadcast) => {
81-
return {
82-
published_at: broadcast.published_at,
83-
subject: broadcast.subject,
84-
id: broadcast.id
85-
};
86-
});
87-
88-
return broadcastsWithData;
88+
// filter for public broadcasts
89+
.filter((broadcast) => broadcast.public)
90+
// trim to only the fields we need (published_at, subject)
91+
.map((broadcast) => {
92+
return {
93+
published_at: broadcast.published_at,
94+
subject: broadcast.subject,
95+
id: broadcast.id
96+
};
97+
});
98+
99+
return broadcastsWithData;
100+
} catch (error) {
101+
return [];
102+
}
89103
}
90104

91105
export const load: PageServerLoad = async function ({ setHeaders, params, locals }) {

src/routes/(site)/snackpack/+page.svelte

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
Wanna see how good our snackpack is? Looking for something mentioned in the past?
2929
</p>
3030
<!-- Loop over data.issues -->
31+
{#if !data.issues.length}
32+
<p class="error">
33+
Oopsie daisy! Unable to load past issues.
34+
</p>
35+
{/if}
3136
<ul>
3237
{#each data.issues as issue}
3338
<li>
@@ -77,4 +82,7 @@
7782
margin: 0 auto;
7883
margin-top: 2rem;
7984
}
85+
.error {
86+
color: var(--warning);
87+
}
8088
</style>

0 commit comments

Comments
 (0)