@@ -23,6 +23,7 @@ export default class HttpClient {
23
23
#agent;
24
24
#breaker;
25
25
#followRedirects;
26
+ // eslint-disable-next-line no-unused-private-class-members
26
27
#hasFallback = false ;
27
28
#logger;
28
29
#throwOn400;
@@ -84,7 +85,7 @@ export default class HttpClient {
84
85
}
85
86
86
87
async #request( options = { } ) {
87
- if ( this . #followRedirects) {
88
+ if ( this . #followRedirects || options . redirectable ) {
88
89
const { redirect } = interceptors ;
89
90
options . dispatcher = this . #agent. compose (
90
91
redirect ( { maxRedirections : 1 } ) ,
@@ -97,7 +98,13 @@ export default class HttpClient {
97
98
...options ,
98
99
} ) ;
99
100
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
+ ) {
101
108
// Body must be consumed; https://github.com/nodejs/undici/issues/583#issuecomment-855384858
102
109
const errBody = await body . text ( ) ;
103
110
this . #logger. trace (
@@ -106,9 +113,12 @@ export default class HttpClient {
106
113
throw createError ( statusCode ) ;
107
114
}
108
115
109
- if ( this . #throwOn500 && statusCode >= 500 && statusCode <= 599 ) {
116
+ if (
117
+ ( this . #throwOn500 || options . throwable ) &&
118
+ statusCode >= 500 &&
119
+ statusCode <= 599
120
+ ) {
110
121
// Body must be consumed; https://github.com/nodejs/undici/issues/583#issuecomment-855384858
111
- await body . text ( ) ;
112
122
const errBody = await body . text ( ) ;
113
123
this . #logger. trace (
114
124
`HTTP ${ statusCode } error catched by client. Body: ${ errBody } ` ,
@@ -138,20 +148,7 @@ export default class HttpClient {
138
148
* @returns {Promise<any> }
139
149
*/
140
150
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 ) ;
155
152
}
156
153
157
154
/**
@@ -169,15 +166,15 @@ export default class HttpClient {
169
166
/**
170
167
* Error class for the client
171
168
*/
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
+ // }
0 commit comments