Skip to content
Merged
Show file tree
Hide file tree
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
23 changes: 20 additions & 3 deletions packages/generator/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,34 @@ export class HTTPError extends Error {
readonly status: number;
readonly statusText: string;
readonly response: Response;
constructor(response: Response) {
readonly responseText: string | null;
readonly responseJson: unknown | null;
constructor(
response: Response,
responseText: string | null,
responseJson: unknown | null
) {
super(\`HTTP Error: \${response.status} \${response.statusText}\`);
this.name = 'HTTPError';
this.status = response.status;
this.statusText = response.statusText;
this.response = response;
this.responseText = responseText;
this.responseJson = responseJson;
}
}

function _checkOk(response: Response): Response {
if (!response.ok) throw new HTTPError(response);
async function _checkOk(response: Response): Promise<Response> {
if (!response.ok) {
const responseText = await response.text().catch(() => null);
let responseJson: unknown | null = null;
if (responseText) {
try {
responseJson = JSON.parse(responseText);
} catch {}
}
throw new HTTPError(response, responseText, responseJson);
}
return response;
}
`;
Expand Down
Loading