Skip to content
Merged
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
25 changes: 17 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,24 @@ export default class PineFetch<
body: normalizedBody,
});

let responseBody;
try {
// Fetch the response as text
responseBody = await response.text();
// And then try to parse it as JSON as it is likely to be JSON
responseBody = JSON.parse(responseBody);
// TODO: base this upon the returned content-type header
} catch {
// empty
}

if (response.status >= 400) {
let responseBody = 'Unknown error';
try {
responseBody = await response.text();
} catch {
// empty
}
throw new RequestError(responseBody, response.status, response.headers);
throw new RequestError(
responseBody ?? 'Unknown error',
response.status,
response.headers,
);
}
return await response.json();
return responseBody;
}
}
Loading