Skip to content

Commit f071157

Browse files
committed
fix: removing custom error
1 parent 1aab57c commit f071157

File tree

4 files changed

+38
-44
lines changed

4 files changed

+38
-44
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,6 @@ For a complete list of options, consult the [undici documentation](https://undic
156156

157157
Closes the client and it's connections.
158158

159-
## HttpClientError
160-
161-
Errors thrown from the library will be of the class `HttpClientError`
162-
163159
[@metrics/metric]: https://github.com/metrics-js/metric '@metrics/metric'
164160
[abslog]: https://github.com/trygve-lie/abslog 'abslog'
165161
[undici]: https://undici.nodejs.org/

examples/failing.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import HttpClient, { HttpClientError } from '../lib/http-client.js';
1+
import HttpClient from '../lib/http-client.js';
22

33
/**
44
* Example of the circuit breaker opening on a failing request.
@@ -22,9 +22,7 @@ try {
2222
method: 'GET',
2323
});
2424
} catch (err) {
25-
if (err instanceof HttpClientError) {
26-
if (err.code === HttpClientError.ServerDown) {
27-
console.error('Server unavailable..');
28-
}
25+
if (err.toString() === 'Error: Breaker is open') {
26+
console.error('Server unavailable..');
2927
}
3028
}

lib/http-client.js

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default class HttpClient {
2323
#agent;
2424
#breaker;
2525
#followRedirects;
26+
// eslint-disable-next-line no-unused-private-class-members
2627
#hasFallback = false;
2728
#logger;
2829
#throwOn400;
@@ -84,7 +85,7 @@ export default class HttpClient {
8485
}
8586

8687
async #request(options = {}) {
87-
if (this.#followRedirects) {
88+
if (this.#followRedirects || options.redirectable) {
8889
const { redirect } = interceptors;
8990
options.dispatcher = this.#agent.compose(
9091
redirect({ maxRedirections: 1 }),
@@ -97,7 +98,13 @@ export default class HttpClient {
9798
...options,
9899
});
99100

100-
if (this.#throwOn400 && statusCode >= 400 && statusCode <= 499) {
101+
// TODO Look into this, as the resovlers have their own handling of this
102+
// In order to be backward compatible, both throw options must be false
103+
if (
104+
(this.#throwOn400 || options.throwable) &&
105+
statusCode >= 400 &&
106+
statusCode <= 499
107+
) {
101108
// Body must be consumed; https://github.com/nodejs/undici/issues/583#issuecomment-855384858
102109
const errBody = await body.text();
103110
this.#logger.trace(
@@ -106,9 +113,12 @@ export default class HttpClient {
106113
throw createError(statusCode);
107114
}
108115

109-
if (this.#throwOn500 && statusCode >= 500 && statusCode <= 599) {
116+
if (
117+
(this.#throwOn500 || options.throwable) &&
118+
statusCode >= 500 &&
119+
statusCode <= 599
120+
) {
110121
// Body must be consumed; https://github.com/nodejs/undici/issues/583#issuecomment-855384858
111-
await body.text();
112122
const errBody = await body.text();
113123
this.#logger.trace(
114124
`HTTP ${statusCode} error catched by client. Body: ${errBody}`,
@@ -138,20 +148,7 @@ export default class HttpClient {
138148
* @returns {Promise<any>}
139149
*/
140150
async request(options = {}) {
141-
try {
142-
return await this.#breaker.fire(options);
143-
} catch (error) {
144-
if (!this.#hasFallback) {
145-
throw new HttpClientError(
146-
`Error on ${options.method} ${options.origin}${options.path}`,
147-
{
148-
code: error.code,
149-
cause: error,
150-
options,
151-
},
152-
);
153-
}
154-
}
151+
return await this.#breaker.fire(options);
155152
}
156153

157154
/**
@@ -169,15 +166,15 @@ export default class HttpClient {
169166
/**
170167
* Error class for the client
171168
*/
172-
export class HttpClientError extends Error {
173-
// Not sure if there is a need for this tbh, but I threw it in there so we can see how it feels.
174-
static ServerDown = 'EOPENBREAKER';
175-
176-
constructor(message, { code, cause, options }) {
177-
super(message);
178-
this.name = 'HttpClientError';
179-
this.code = code;
180-
this.cause = cause;
181-
this.options = options;
182-
}
183-
}
169+
// export class HttpClientError extends Error {
170+
// // Not sure if there is a need for this tbh, but I threw it in there so we can see how it feels.
171+
// static ServerDown = 'EOPENBREAKER';
172+
//
173+
// constructor(message, { code, cause, options }) {
174+
// super(message);
175+
// this.name = 'HttpClientError';
176+
// this.code = code;
177+
// this.cause = cause;
178+
// this.options = options;
179+
// }
180+
// }

tests/http-client.test.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ async function queryUrl({
4040
await client.request({ path, origin: url, method: 'GET' });
4141
} catch (err) {
4242
// Push the actual cause here for the tests
43-
if (!suppressErrors) errors.push(err.cause);
43+
if (!suppressErrors) errors.push(err);
4444
}
4545
}
4646
if (errors.length > 0) {
47-
throw new Error(errors.toString());
47+
throw new Error(errors);
4848
}
4949
}
5050
await test('http-client - basics', async (t) => {
@@ -90,8 +90,7 @@ await test('http-client - basics', async (t) => {
9090
});
9191
},
9292
{
93-
name: 'HttpClientError',
94-
message: 'Error on GET https://does-not-exist.domain/',
93+
name: 'Error',
9594
},
9695
);
9796
await client.close();
@@ -210,7 +209,7 @@ await test('http-client - circuit breaker behaviour', async (t) => {
210209
method: 'GET',
211210
});
212211
} catch (err) {
213-
if (err.cause.code === 'EOPENBREAKER') {
212+
if (err.toString() === 'Error: Breaker is open') {
214213
broken++;
215214
}
216215
}
@@ -222,6 +221,7 @@ await test('http-client - circuit breaker behaviour', async (t) => {
222221
);
223222
await afterEach(client);
224223
});
224+
225225
await t.test('can reset breaker', async () => {
226226
beforeEach();
227227
const invalidUrl = `http://${host}:3023`;
@@ -250,4 +250,7 @@ await test('http-client - circuit breaker behaviour', async (t) => {
250250
assert.strictEqual(response.statusCode, 200);
251251
await afterEach(client);
252252
});
253+
// await t.test('has a .metrics property', async () => {
254+
// const client = new HttpClient({ threshold: 50, reset: breakerReset });
255+
// });
253256
});

0 commit comments

Comments
 (0)