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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 15 additions & 4 deletions tests/client.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
);
Expand Down