Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

fix: better error handling

flooorianflo1c4496375a68
diff --git a/apps/web/app/api/analytics/[endpoint]/export/route.ts b/apps/web/app/api/analytics/[endpoint]/export/route.ts
index 8728375fff..4e30853a9d 100644
--- a/apps/web/app/api/analytics/[endpoint]/export/route.ts
+++ b/apps/web/app/api/analytics/[endpoint]/export/route.ts
@@ -39,11 +39,11 @@ export const GET = withAuth(
...parsedParams,
});
- // convert the response to csv
+ // convert the response to CSV
const csv = json2csv(
endpoint === "timeseries"
? response.map((entry: { start: Date; clicks: number }) => ({
- Date: entry.start,
+ date: entry.start,
clicks: entry.clicks,
}))
: response,
@@ -58,7 +58,6 @@ export const GET = withAuth(
},
);
- // return the csv as a response
return new Response(csv, {
headers: {
"Content-Type": "text/csv",
diff --git a/apps/web/ui/analytics/export-button.tsx b/apps/web/ui/analytics/export-button.tsx
index b4728660f8..37193f6d51 100644
--- a/apps/web/ui/analytics/export-button.tsx
+++ b/apps/web/ui/analytics/export-button.tsx
@@ -19,9 +19,10 @@ export default function ExportButton() {
const exportData = async () => {
const zipFile = new zip();
- for (const endpoint of exportableEndpoints) {
- try {
- const data = await fetch(
+
+ try {
+ for (const endpoint of exportableEndpoints) {
+ const response = await fetch(
`${baseApiPath}/${endpoint}/export?${queryString}`,
{
method: "GET",
@@ -29,12 +30,19 @@ export default function ExportButton() {
"Content-Type": "application/json",
},
},
- ).then((res) => res.blob());
- zipFile.file(`${endpoint}.csv`, data);
- } catch (error) {
- console.error(error);
- toast.error("Failed to export");
+ );
+
+ if (response.status === 200) {
+ const data = await response.blob();
+ zipFile.file(`${endpoint}.csv`, data);
+ } else {
+ throw new Error("Failed to export");
+ }
}
+ } catch (error) {
+ console.error(error);
+ toast.error(error.message);
+ return;
}
zipFile.generateAsync({ type: "blob" }).then((blob) => {
@@ -43,6 +51,7 @@ export default function ExportButton() {
a.href = url;
a.download = "analytics-export.zip";
a.click();
+ toast.success("Exported successfully");
});
};
@@ -56,7 +65,6 @@ export default function ExportButton() {
setLoading(true);
await exportData();
setLoading(false);
- toast.success("Exported successfully");
}}
>
<IconMenu text="Export" icon={<Download className="h-4 w-4" />} />
@@ -70,7 +78,6 @@ export default function ExportButton() {
setLoading(true);
await exportData();
setLoading(false);
- toast.success("Exported successfully");
}}
>
{loading ? (