Skip to content

Commit

Permalink
Log on error and capture downstream response code (#9)
Browse files Browse the repository at this point in the history
Co-authored-by: Jon Breen <[email protected]>
  • Loading branch information
cultpodcasts and cultpodcasts committed Mar 6, 2024
1 parent 332b8cb commit 72ecbd6
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,16 @@ export default {
dataPoint.blobs?.push(data.skip);
dataPoint.blobs?.push(data.orderby);
}

if (dataPoint) {
if (request.cf) {
if (request.cf.city) {
dataPoint.blobs?.push(request.cf.city as string);
}
if (request.cf.country) {
dataPoint.blobs?.push(request.cf.country as string);
}
}
}
let response = await fetch(url, {
cf: {
cacheEverything: true,
Expand All @@ -85,6 +94,10 @@ export default {
body: requestBody,
method: "POST"
});
if (dataPoint) {
dataPoint.blobs?.push(response.status.toString());
env.Analytics.writeDataPoint(dataPoint);
}

const headers = new Headers();
headers.set("Cache-Control", "max-age=600");
Expand All @@ -100,20 +113,6 @@ export default {
body["@odata.context"] = null;
let bodyJson = JSON.stringify(body);

if (dataPoint) {
if (request.cf) {
if (request.cf.city) {
dataPoint.blobs?.push(request.cf.city as string);
}
if (request.cf.country) {
dataPoint.blobs?.push(request.cf.country as string);
}
}
//console.log(JSON.stringify(dataPoint));
}

env.Analytics.writeDataPoint(dataPoint);

return new Response(bodyJson, { headers })
});
}
Expand All @@ -133,12 +132,12 @@ export default {
let url: URL | undefined;
let urlParam = data.url;
if (urlParam == null) {
return new Response(JSON.stringify({ error: "Missing url param."}), { headers, status: 400 });
return new Response(JSON.stringify({ error: "Missing url param." }), { headers, status: 400 });
}
try {
url = new URL(urlParam);
} catch {
return new Response(JSON.stringify({error: `Invalid url '${data.url}'.`}), { headers, status: 400 });
return new Response(JSON.stringify({ error: `Invalid url '${data.url}'.` }), { headers, status: 400 });
}
let insert = env.DB
.prepare("INSERT INTO urls (url, timestamp, timestamp_date, ip_address, country, user_agent) VALUES (?, ?, ?, ?, ?, ?)")
Expand Down

0 comments on commit 72ecbd6

Please sign in to comment.