Skip to content

Commit 9984660

Browse files
committed
Improve error handling in uploadFile function for better response feedback
Signed-off-by: Jannik Hollenbach <jannik.hollenbach@owasp.org>
1 parent 328881a commit 9984660

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

hook-sdk/nodejs/hook-wrapper.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,26 @@ async function uploadFile(url, fileContents) {
3939
method: "PUT",
4040
headers: { "content-type": "" },
4141
});
42+
43+
if (!response.ok) {
44+
// The request was made and the server responded with a status code
45+
// that falls out of the range of 2xx
46+
const error = new Error(`HTTP error! status: ${response.status}`);
47+
error.response = response;
48+
throw error;
49+
}
4250
} catch (error) {
4351
if (error.response) {
4452
// The request was made and the server responded with a status code
4553
// that falls out of the range of 2xx
4654
console.error(
4755
`File Upload Failed with Response Code: ${error.response.status}`,
4856
);
49-
console.error(`Error Response Body: ${error.response.data}`);
50-
} else if (error.request) {
51-
console.error(
52-
"No response received from FileStorage when uploading finding",
53-
);
54-
console.error(error);
57+
const errorBody = await error.response.text();
58+
console.error(`Error Response Body: ${errorBody}`);
5559
} else {
5660
// Something happened in setting up the request that triggered an Error
57-
console.log("Error", error.message);
61+
console.error("Error uploading findings from hook", error.message);
5862
}
5963
process.exit(1);
6064
}

0 commit comments

Comments
 (0)