Skip to content

Commit

Permalink
Throw Cloudflare API errors
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlueder committed Aug 14, 2022
1 parent d6b7161 commit 6c6aa8e
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async function handleRequest(request) {
const response = await handleRequestInternal(request);
return response;
} catch(err) {
console.error(err);
console.error(err.constructor.name, err);
throw err;
}
}
Expand Down Expand Up @@ -152,6 +152,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 @@ -171,6 +179,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 @@ -185,6 +196,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 @@ -202,6 +216,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 Down

0 comments on commit 6c6aa8e

Please sign in to comment.