Skip to content

Commit

Permalink
Merge pull request #19 from davidlueder/exception-handling
Browse files Browse the repository at this point in the history
Improve exception handling
  • Loading branch information
willswire authored Aug 17, 2022
2 parents d52fee9 + ed5e64c commit b0ea3b7
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @param {Request} request
* @returns {Promise<Response>}
*/
async function handleRequest(request) {
async function handleRequest(request) {
const { protocol, pathname } = new URL(request.url);

// Require HTTPS (TLS) connection to be secure.
Expand Down Expand Up @@ -142,6 +142,14 @@ class BadRequestException {
}
}

class CloudflareApiException {
constructor(reason) {
this.status = 500;
this.statusText = "Internal Server Error";
this.reason = reason;
}
}

class Cloudflare {
constructor(options) {
this.cloudflare_url = "https://api.cloudflare.com/client/v4";
Expand All @@ -161,6 +169,9 @@ class Cloudflare {
}
);
var body = await response.json();
if(body.success !== true || body.result.length === 0) {
throw new CloudflareApiException("Failed to find zone '" + name + "'");
}
return body.result[0];
};

Expand All @@ -175,6 +186,9 @@ class Cloudflare {
}
);
var body = await response.json();
if(body.success !== true || body.result.length === 0) {
throw new CloudflareApiException("Failed to find dns record '" + name + "'");
}
return body.result[0];
};

Expand All @@ -192,6 +206,9 @@ class Cloudflare {
}
);
var body = await response.json();
if(body.success !== true) {
throw new CloudflareApiException("Failed to update dns record");
}
return body.result[0];
};
}
Expand All @@ -200,6 +217,7 @@ class Cloudflare {
addEventListener("fetch", (event) => {
event.respondWith(
handleRequest(event.request).catch((err) => {
console.error(err.constructor.name, err);
const message = err.reason || err.stack || "Unknown Error";

return new Response(message, {
Expand Down

0 comments on commit b0ea3b7

Please sign in to comment.