Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 17 additions & 24 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ export async function postData({
});

if (!response.ok) {
const errorBody = await response.json();
const error = {
status: response.status,
message: errorBody.message || 'API request failed',
};


throw error;
throw (await makeError(response));
}

const result = await response.json();
Expand All @@ -63,14 +56,7 @@ export async function patchData({
});

if (!response.ok) {

const errorBody = await response.json();
const error = {
status: response.status,
message: errorBody.message || 'API request failed',
};

throw error;
throw (await makeError(response));
}

const result = await response.json();
Expand All @@ -93,16 +79,23 @@ export async function getData({
});

if (!response.ok) {

const errorBody = await response.json();
const error = {
status: response.status,
message: errorBody.message || 'API request failed',
};

throw error;
throw (await makeError(response));
}

const result = await response.json();
return result;
}

async function makeError(response: Response) {
let errorBody: any;
try {
errorBody = await response.json();
} catch (_) {
errorBody = null;
}

return {
status: response.status,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in case if response json fail, response.status will be undefined, so let's set code 500 as fallback here

Suggested change
status: response.status,
status: response.status || 500,

message: errorBody?.message || "API request failed",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also add success: false to response object, to make response object parameters a bit more consistent.

Suggested change
message: errorBody?.message || "API request failed",
message: errorBody?.message || "API request failed",
success: false,

};
}