From 6e203b459bc6f7a0a0ed37c23166bcb02adb3672 Mon Sep 17 00:00:00 2001 From: Taiyu Yoshizawa Date: Tue, 24 Mar 2026 23:10:03 +0900 Subject: [PATCH 1/2] Improve HTTPError to include response text and JSON --- packages/generator/src/client.ts | 23 ++- packages/generator/tests/client.test.ts | 238 ++++++++++++++++++---- packages/generator/tests/generate.test.ts | 17 +- 3 files changed, 230 insertions(+), 48 deletions(-) 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..9f845a6 100644 --- a/packages/generator/tests/client.test.ts +++ b/packages/generator/tests/client.test.ts @@ -99,17 +99,28 @@ 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 !== null) { + try { responseJson = JSON.parse(responseText); } catch {} + } + throw new HTTPError(response, responseText, responseJson); + } return response; } @@ -129,17 +140,28 @@ 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 !== null) { + try { responseJson = JSON.parse(responseText); } catch {} + } + throw new HTTPError(response, responseText, responseJson); + } return response; } @@ -176,17 +198,28 @@ 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 !== null) { + try { responseJson = JSON.parse(responseText); } catch {} + } + throw new HTTPError(response, responseText, responseJson); + } return response; } @@ -224,17 +257,28 @@ 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 !== null) { + try { responseJson = JSON.parse(responseText); } catch {} + } + throw new HTTPError(response, responseText, responseJson); + } return response; } @@ -284,17 +328,28 @@ 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 !== null) { + try { responseJson = JSON.parse(responseText); } catch {} + } + throw new HTTPError(response, responseText, responseJson); + } return response; } @@ -342,17 +397,28 @@ 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 !== null) { + try { responseJson = JSON.parse(responseText); } catch {} + } + throw new HTTPError(response, responseText, responseJson); + } return response; } @@ -390,17 +456,28 @@ 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 !== null) { + try { responseJson = JSON.parse(responseText); } catch {} + } + throw new HTTPError(response, responseText, responseJson); + } return response; } @@ -460,17 +537,28 @@ 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 !== null) { + try { responseJson = JSON.parse(responseText); } catch {} + } + throw new HTTPError(response, responseText, responseJson); + } return response; } @@ -508,17 +596,28 @@ 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 !== null) { + try { responseJson = JSON.parse(responseText); } catch {} + } + throw new HTTPError(response, responseText, responseJson); + } return response; } @@ -572,17 +671,28 @@ 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 !== null) { + try { responseJson = JSON.parse(responseText); } catch {} + } + throw new HTTPError(response, responseText, responseJson); + } return response; } @@ -636,17 +746,28 @@ 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 !== null) { + try { responseJson = JSON.parse(responseText); } catch {} + } + throw new HTTPError(response, responseText, responseJson); + } return response; } @@ -701,17 +822,28 @@ 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 !== null) { + try { responseJson = JSON.parse(responseText); } catch {} + } + throw new HTTPError(response, responseText, responseJson); + } return response; } @@ -764,17 +896,28 @@ 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 !== null) { + try { responseJson = JSON.parse(responseText); } catch {} + } + throw new HTTPError(response, responseText, responseJson); + } return response; } @@ -837,17 +980,28 @@ 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 !== null) { + 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..f3c5585 100644 --- a/packages/generator/tests/generate.test.ts +++ b/packages/generator/tests/generate.test.ts @@ -146,17 +146,28 @@ 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 !== null) { + try { responseJson = JSON.parse(responseText); } catch {} + } + throw new HTTPError(response, responseText, responseJson); + } return response; } From 70dbc1625910304167af011e4727119a2cccab43 Mon Sep 17 00:00:00 2001 From: Taiyu Yoshizawa Date: Tue, 24 Mar 2026 23:11:31 +0900 Subject: [PATCH 2/2] fix:snapshots --- packages/generator/tests/client.test.ts | 168 ++++++++++++++++------ packages/generator/tests/generate.test.ts | 12 +- 2 files changed, 135 insertions(+), 45 deletions(-) diff --git a/packages/generator/tests/client.test.ts b/packages/generator/tests/client.test.ts index 9f845a6..af8fe3d 100644 --- a/packages/generator/tests/client.test.ts +++ b/packages/generator/tests/client.test.ts @@ -101,7 +101,11 @@ describe('generateClient', () => { readonly response: Response; readonly responseText: string | null; readonly responseJson: unknown | null; - constructor(response: Response, responseText: string | null, 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; @@ -116,8 +120,10 @@ describe('generateClient', () => { if (!response.ok) { const responseText = await response.text().catch(() => null); let responseJson: unknown | null = null; - if (responseText !== null) { - try { responseJson = JSON.parse(responseText); } catch {} + if (responseText) { + try { + responseJson = JSON.parse(responseText); + } catch {} } throw new HTTPError(response, responseText, responseJson); } @@ -142,7 +148,11 @@ describe('generateClient', () => { readonly response: Response; readonly responseText: string | null; readonly responseJson: unknown | null; - constructor(response: Response, responseText: string | null, 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; @@ -157,8 +167,10 @@ describe('generateClient', () => { if (!response.ok) { const responseText = await response.text().catch(() => null); let responseJson: unknown | null = null; - if (responseText !== null) { - try { responseJson = JSON.parse(responseText); } catch {} + if (responseText) { + try { + responseJson = JSON.parse(responseText); + } catch {} } throw new HTTPError(response, responseText, responseJson); } @@ -200,7 +212,11 @@ describe('generateClient', () => { readonly response: Response; readonly responseText: string | null; readonly responseJson: unknown | null; - constructor(response: Response, responseText: string | null, 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; @@ -215,8 +231,10 @@ describe('generateClient', () => { if (!response.ok) { const responseText = await response.text().catch(() => null); let responseJson: unknown | null = null; - if (responseText !== null) { - try { responseJson = JSON.parse(responseText); } catch {} + if (responseText) { + try { + responseJson = JSON.parse(responseText); + } catch {} } throw new HTTPError(response, responseText, responseJson); } @@ -259,7 +277,11 @@ describe('generateClient', () => { readonly response: Response; readonly responseText: string | null; readonly responseJson: unknown | null; - constructor(response: Response, responseText: string | null, 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; @@ -274,8 +296,10 @@ describe('generateClient', () => { if (!response.ok) { const responseText = await response.text().catch(() => null); let responseJson: unknown | null = null; - if (responseText !== null) { - try { responseJson = JSON.parse(responseText); } catch {} + if (responseText) { + try { + responseJson = JSON.parse(responseText); + } catch {} } throw new HTTPError(response, responseText, responseJson); } @@ -330,7 +354,11 @@ describe('generateClient', () => { readonly response: Response; readonly responseText: string | null; readonly responseJson: unknown | null; - constructor(response: Response, responseText: string | null, 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; @@ -345,8 +373,10 @@ describe('generateClient', () => { if (!response.ok) { const responseText = await response.text().catch(() => null); let responseJson: unknown | null = null; - if (responseText !== null) { - try { responseJson = JSON.parse(responseText); } catch {} + if (responseText) { + try { + responseJson = JSON.parse(responseText); + } catch {} } throw new HTTPError(response, responseText, responseJson); } @@ -399,7 +429,11 @@ describe('generateClient', () => { readonly response: Response; readonly responseText: string | null; readonly responseJson: unknown | null; - constructor(response: Response, responseText: string | null, 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; @@ -414,8 +448,10 @@ describe('generateClient', () => { if (!response.ok) { const responseText = await response.text().catch(() => null); let responseJson: unknown | null = null; - if (responseText !== null) { - try { responseJson = JSON.parse(responseText); } catch {} + if (responseText) { + try { + responseJson = JSON.parse(responseText); + } catch {} } throw new HTTPError(response, responseText, responseJson); } @@ -458,7 +494,11 @@ describe('generateClient', () => { readonly response: Response; readonly responseText: string | null; readonly responseJson: unknown | null; - constructor(response: Response, responseText: string | null, 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; @@ -473,8 +513,10 @@ describe('generateClient', () => { if (!response.ok) { const responseText = await response.text().catch(() => null); let responseJson: unknown | null = null; - if (responseText !== null) { - try { responseJson = JSON.parse(responseText); } catch {} + if (responseText) { + try { + responseJson = JSON.parse(responseText); + } catch {} } throw new HTTPError(response, responseText, responseJson); } @@ -539,7 +581,11 @@ describe('generateClient', () => { readonly response: Response; readonly responseText: string | null; readonly responseJson: unknown | null; - constructor(response: Response, responseText: string | null, 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; @@ -554,8 +600,10 @@ describe('generateClient', () => { if (!response.ok) { const responseText = await response.text().catch(() => null); let responseJson: unknown | null = null; - if (responseText !== null) { - try { responseJson = JSON.parse(responseText); } catch {} + if (responseText) { + try { + responseJson = JSON.parse(responseText); + } catch {} } throw new HTTPError(response, responseText, responseJson); } @@ -598,7 +646,11 @@ describe('generateClient', () => { readonly response: Response; readonly responseText: string | null; readonly responseJson: unknown | null; - constructor(response: Response, responseText: string | null, 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; @@ -613,8 +665,10 @@ describe('generateClient', () => { if (!response.ok) { const responseText = await response.text().catch(() => null); let responseJson: unknown | null = null; - if (responseText !== null) { - try { responseJson = JSON.parse(responseText); } catch {} + if (responseText) { + try { + responseJson = JSON.parse(responseText); + } catch {} } throw new HTTPError(response, responseText, responseJson); } @@ -673,7 +727,11 @@ describe('generateClient', () => { readonly response: Response; readonly responseText: string | null; readonly responseJson: unknown | null; - constructor(response: Response, responseText: string | null, 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; @@ -688,8 +746,10 @@ describe('generateClient', () => { if (!response.ok) { const responseText = await response.text().catch(() => null); let responseJson: unknown | null = null; - if (responseText !== null) { - try { responseJson = JSON.parse(responseText); } catch {} + if (responseText) { + try { + responseJson = JSON.parse(responseText); + } catch {} } throw new HTTPError(response, responseText, responseJson); } @@ -748,7 +808,11 @@ describe('generateClient', () => { readonly response: Response; readonly responseText: string | null; readonly responseJson: unknown | null; - constructor(response: Response, responseText: string | null, 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; @@ -763,8 +827,10 @@ describe('generateClient', () => { if (!response.ok) { const responseText = await response.text().catch(() => null); let responseJson: unknown | null = null; - if (responseText !== null) { - try { responseJson = JSON.parse(responseText); } catch {} + if (responseText) { + try { + responseJson = JSON.parse(responseText); + } catch {} } throw new HTTPError(response, responseText, responseJson); } @@ -824,7 +890,11 @@ describe('generateClient', () => { readonly response: Response; readonly responseText: string | null; readonly responseJson: unknown | null; - constructor(response: Response, responseText: string | null, 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; @@ -839,8 +909,10 @@ describe('generateClient', () => { if (!response.ok) { const responseText = await response.text().catch(() => null); let responseJson: unknown | null = null; - if (responseText !== null) { - try { responseJson = JSON.parse(responseText); } catch {} + if (responseText) { + try { + responseJson = JSON.parse(responseText); + } catch {} } throw new HTTPError(response, responseText, responseJson); } @@ -898,7 +970,11 @@ describe('generateClient', () => { readonly response: Response; readonly responseText: string | null; readonly responseJson: unknown | null; - constructor(response: Response, responseText: string | null, 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; @@ -913,8 +989,10 @@ describe('generateClient', () => { if (!response.ok) { const responseText = await response.text().catch(() => null); let responseJson: unknown | null = null; - if (responseText !== null) { - try { responseJson = JSON.parse(responseText); } catch {} + if (responseText) { + try { + responseJson = JSON.parse(responseText); + } catch {} } throw new HTTPError(response, responseText, responseJson); } @@ -982,7 +1060,11 @@ describe('generateClient', () => { readonly response: Response; readonly responseText: string | null; readonly responseJson: unknown | null; - constructor(response: Response, responseText: string | null, 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; @@ -997,8 +1079,10 @@ describe('generateClient', () => { if (!response.ok) { const responseText = await response.text().catch(() => null); let responseJson: unknown | null = null; - if (responseText !== null) { - try { responseJson = JSON.parse(responseText); } catch {} + if (responseText) { + try { + responseJson = JSON.parse(responseText); + } catch {} } throw new HTTPError(response, responseText, responseJson); } diff --git a/packages/generator/tests/generate.test.ts b/packages/generator/tests/generate.test.ts index f3c5585..ac427db 100644 --- a/packages/generator/tests/generate.test.ts +++ b/packages/generator/tests/generate.test.ts @@ -148,7 +148,11 @@ describe('generateFromObject', () => { readonly response: Response; readonly responseText: string | null; readonly responseJson: unknown | null; - constructor(response: Response, responseText: string | null, 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; @@ -163,8 +167,10 @@ describe('generateFromObject', () => { if (!response.ok) { const responseText = await response.text().catch(() => null); let responseJson: unknown | null = null; - if (responseText !== null) { - try { responseJson = JSON.parse(responseText); } catch {} + if (responseText) { + try { + responseJson = JSON.parse(responseText); + } catch {} } throw new HTTPError(response, responseText, responseJson); }