Skip to content

Commit ec8fa05

Browse files
refactor: add checkFetchResponse after each doFetch
revert status code handling code
1 parent f2165a5 commit ec8fa05

1 file changed

Lines changed: 5 additions & 12 deletions

File tree

packages/binding-http/src/http-client-impl.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,6 @@ export default class HttpClient implements ProtocolClient {
128128

129129
const result = await this.doFetch(request);
130130

131-
this.checkFetchResponse(result);
132-
133131
debug(`HttpClient received headers: ${JSON.stringify(result.headers.raw())}`);
134132
debug(`HttpClient received Content-Type: ${result.headers.get("content-type")}`);
135133

@@ -154,8 +152,6 @@ export default class HttpClient implements ProtocolClient {
154152

155153
debug(`HttpClient received ${result.status} from ${result.url}`);
156154

157-
this.checkFetchResponse(result);
158-
159155
debug(`HttpClient received headers: ${JSON.stringify(result.headers.raw())}`);
160156
}
161157

@@ -218,8 +214,6 @@ export default class HttpClient implements ProtocolClient {
218214
debug(`HttpClient received ${result.status} from ${request.url}`);
219215
debug(`HttpClient received Content-Type: ${result.headers.get("content-type")}`);
220216

221-
this.checkFetchResponse(result);
222-
223217
// in browsers node-fetch uses the native fetch, which returns a ReadableStream
224218
// not complaint with node. Therefore we have to force the conversion here.
225219
const body = ProtocolHelpers.toNodeStream(result.body as Readable);
@@ -432,14 +426,13 @@ export default class HttpClient implements ProtocolClient {
432426

433427
if (HttpClient.isOAuthTokenExpired(result, this.credential)) {
434428
this.credential = await (this.credential as OAuthCredential).refreshToken();
435-
return await this._fetch(await this.credential.sign(request));
436-
} else if (result.status === 401) {
437-
throw new Error("Client error: Unauthorized");
438-
} else if (result.status >= 400) {
439-
// report generic error
440-
throw new Error(`request to ${request.url} failed, reason: HTTP status result ${result.status} `);
429+
const resultAuth = await this._fetch(await this.credential.sign(request));
430+
this.checkFetchResponse(resultAuth);
431+
return resultAuth;
441432
}
442433

434+
this.checkFetchResponse(result);
435+
443436
return result;
444437
}
445438

0 commit comments

Comments
 (0)