@@ -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+ // }
0 commit comments