From b371948ea725e71379598fec7ce0a89adb053e1c Mon Sep 17 00:00:00 2001 From: Stefan Nebel Date: Wed, 30 Sep 2026 08:23:40 +0200 Subject: [PATCH] fix: preserve HTTP errors after body timeouts --- package-lock.json | 4 ++-- package.json | 2 +- src/transport.ts | 2 +- tests/client.test.mjs | 19 +++++++++++++++---- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8eca0d6..3117803 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@energy-tracker/api-client", - "version": "1.0.1", + "version": "1.0.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@energy-tracker/api-client", - "version": "1.0.1", + "version": "1.0.2", "license": "MIT", "devDependencies": { "@types/node": "^20.0.0", diff --git a/package.json b/package.json index 3a979c6..8116454 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@energy-tracker/api-client", - "version": "1.0.1", + "version": "1.0.2", "description": "Typed async client for the Energy Tracker public API", "license": "MIT", "author": "Stefan Nebel", diff --git a/src/transport.ts b/src/transport.ts index f82e9f3..f7362e8 100644 --- a/src/transport.ts +++ b/src/transport.ts @@ -183,9 +183,9 @@ export class Transport { }); } } catch (cause) { - if (controller.signal.reason === timeoutError) throw timeoutError; if (request.signal?.aborted) throw request.signal.reason; if (cause instanceof EnergyTrackerAPIError) throw cause; + if (controller.signal.reason === timeoutError) throw timeoutError; throw new NetworkError('Request failed', { cause }); } finally { clearTimeout(timer); diff --git a/tests/client.test.mjs b/tests/client.test.mjs index a6d9bb9..10b6db4 100644 --- a/tests/client.test.mjs +++ b/tests/client.test.mjs @@ -103,19 +103,30 @@ test('network errors retain cause without HTTP status', async () => { }, ); }); -for (const status of [200, 429, 503]) { +for (const [status, ErrorType] of [ + [200, api.TimeoutError], + [400, api.ValidationError], + [401, api.AuthenticationError], + [403, api.ForbiddenError], + [404, api.ResourceNotFoundError], + [409, api.ConflictError], + [429, api.RateLimitError], + [500, api.EnergyTrackerAPIError], + [503, api.ServiceUnavailableError], +]) { test(`timeout covers streaming HTTP ${status} body and does not retry`, async (t) => { let requests = 0; const baseUrl = await serve(t, (req, res) => { requests++; - res.writeHead(status, { 'Content-Type': 'application/json' }); + res.writeHead(status, { 'Content-Type': 'application/json', 'Retry-After': '42' }); res.write('['); }); await assert.rejects( () => client({ baseUrl, timeout: 0.2 }).devices.listStandard(), (error) => { - assert.ok(error instanceof api.TimeoutError); - assert.equal(error.statusCode, null); + assert.equal(error.constructor, ErrorType); + assert.equal(error.statusCode, status === 200 ? null : status); + if (status === 429) assert.equal(error.retryAfter, 42); return true; }, );