Skip to content

Commit

Permalink
Merge pull request #85 from cloudflare/walshy/better-api-errors
Browse files Browse the repository at this point in the history
Better API errors
  • Loading branch information
sidharthachatterjee committed May 23, 2023
2 parents 46e7cec + fd5884d commit 2df29b9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22075,7 +22075,16 @@ try {
`https://api.cloudflare.com/client/v4/accounts/${accountId}/pages/projects/${projectName}`,
{ headers: { Authorization: `Bearer ${apiToken}` } }
);
if (response.status !== 200) {
console.error(`Cloudflare API returned non-200: ${response.status}`);
const json = await response.text();
console.error(`API returned: ${json}`);
throw new Error("Failed to get Pages project, API returned non-200");
}
const { result } = await response.json();
if (result === null) {
throw new Error("Failed to get Pages project, project does not exist. Check the project name or create it!");
}
return result;
};
const createPagesDeployment = async () => {
Expand Down Expand Up @@ -22156,8 +22165,6 @@ try {
};
(async () => {
const project = await getProject();
if (!project)
throw new Error("Unable to find pages project");
const productionEnvironment = githubBranch === project.production_branch || branch === project.production_branch;
const environmentName = `${projectName} (${productionEnvironment ? "Production" : "Preview"})`;
let gitHubDeployment;
Expand Down
12 changes: 11 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,18 @@ try {
`https://api.cloudflare.com/client/v4/accounts/${accountId}/pages/projects/${projectName}`,
{ headers: { Authorization: `Bearer ${apiToken}` } }
);
if (response.status !== 200) {
console.error(`Cloudflare API returned non-200: ${response.status}`);
const json = await response.text();
console.error(`API returned: ${json}`);
throw new Error("Failed to get Pages project, API returned non-200");
}

const { result } = (await response.json()) as { result: Project | null };
if (result === null) {
throw new Error("Failed to get Pages project, project does not exist. Check the project name or create it!");
}

return result;
};

Expand Down Expand Up @@ -126,7 +137,6 @@ try {

(async () => {
const project = await getProject();
if (!project) throw new Error("Unable to find pages project");

const productionEnvironment = githubBranch === project.production_branch || branch === project.production_branch;
const environmentName = `${projectName} (${productionEnvironment ? "Production" : "Preview"})`;
Expand Down

0 comments on commit 2df29b9

Please sign in to comment.