diff --git a/packages/generator/src/client.ts b/packages/generator/src/client.ts index fa95d7f..3ccea19 100644 --- a/packages/generator/src/client.ts +++ b/packages/generator/src/client.ts @@ -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 { + 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; } `; diff --git a/packages/generator/tests/client.test.ts b/packages/generator/tests/client.test.ts index 13b8929..af8fe3d 100644 --- a/packages/generator/tests/client.test.ts +++ b/packages/generator/tests/client.test.ts @@ -99,17 +99,34 @@ describe('generateClient', () => { 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 { + 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; } @@ -129,17 +146,34 @@ describe('generateClient', () => { 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 { + 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; } @@ -176,17 +210,34 @@ describe('generateClient', () => { 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 { + 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; } @@ -224,17 +275,34 @@ describe('generateClient', () => { 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 { + 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; } @@ -284,17 +352,34 @@ describe('generateClient', () => { 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 { + 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; } @@ -342,17 +427,34 @@ describe('generateClient', () => { 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 { + 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; } @@ -390,17 +492,34 @@ describe('generateClient', () => { 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 { + 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; } @@ -460,17 +579,34 @@ describe('generateClient', () => { 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 { + 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; } @@ -508,17 +644,34 @@ describe('generateClient', () => { 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 { + 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; } @@ -572,17 +725,34 @@ describe('generateClient', () => { 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 { + 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; } @@ -636,17 +806,34 @@ describe('generateClient', () => { 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 { + 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; } @@ -701,17 +888,34 @@ describe('generateClient', () => { 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 { + 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; } @@ -764,17 +968,34 @@ describe('generateClient', () => { 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 { + 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; } @@ -837,17 +1058,34 @@ describe('generateClient', () => { 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 { + 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; } diff --git a/packages/generator/tests/generate.test.ts b/packages/generator/tests/generate.test.ts index 133920e..ac427db 100644 --- a/packages/generator/tests/generate.test.ts +++ b/packages/generator/tests/generate.test.ts @@ -146,17 +146,34 @@ describe('generateFromObject', () => { 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 { + 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; }