From fd5884de2adf69987b63677f781de6d6f7482f84 Mon Sep 17 00:00:00 2001
From: Daniel Walsh <walshydev@gmail.com>
Date: Sun, 21 May 2023 09:36:23 +0100
Subject: [PATCH] Better API errors

---
 index.js     | 11 +++++++++--
 src/index.ts | 12 +++++++++++-
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/index.js b/index.js
index b492455..7df67c8 100644
--- a/index.js
+++ b/index.js
@@ -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 () => {
@@ -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;
diff --git a/src/index.ts b/src/index.ts
index 2848443..a63f38e 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -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;
 	};
 
@@ -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"})`;