Skip to content

Commit 89e8afb

Browse files
committed
fix: better redirect handling, avoid unnecessary should retry calls
1 parent 44804d2 commit 89e8afb

File tree

1 file changed

+24
-35
lines changed

1 file changed

+24
-35
lines changed

src/schema-to-typescript/common/core/common-http-client.ts

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -829,26 +829,19 @@ export class CommonHttpClient {
829829
return url;
830830
}
831831

832-
protected processErrorIfNecessary(error: unknown) {
833-
if (this.options.processError) {
834-
return this.options.processError(error instanceof Error ? error : new Error(String(error)));
835-
}
836-
return error;
837-
}
838-
839832
protected async handleRedirect(error: CommonHttpClientError) {
840833
if (this.options.followRedirects === false) {
841-
throw this.processErrorIfNecessary(error);
834+
throw error;
842835
}
843836

844837
const {request, response, url} = error;
845838

846839
if (!request || !response) {
847-
throw this.processErrorIfNecessary(error);
840+
throw error;
848841
}
849842

850-
if (response.status <= 300 || response.status >= 400 || !response.headers['location']) {
851-
throw this.processErrorIfNecessary(error);
843+
if (response.status < 300 || response.status >= 400 || !response.headers['location']) {
844+
throw error;
852845
}
853846

854847
const redirectHandler =
@@ -858,15 +851,15 @@ export class CommonHttpClient {
858851

859852
if (!action || !('type' in action)) {
860853
error.message = `Invalid redirect handler result for ${error.message}.`;
861-
throw this.processErrorIfNecessary(error);
854+
throw error;
862855
}
863856

864857
const redirectPreservingMethod = response.status === 307 || response.status === 308;
865858
const newUrl = new URL(response.headers['location'], url);
866859

867860
if (action.type === 'error') {
868861
error.message = `Redirect to ${newUrl.toString()} not allowed by redirect handler. ${error.message}`;
869-
throw this.processErrorIfNecessary(action.error ?? error);
862+
throw action.error ?? error;
870863
} else if (action.type === 'response') {
871864
return action.response;
872865
} else if (action.type === 'redirect') {
@@ -876,9 +869,7 @@ export class CommonHttpClient {
876869
path: newUrl.pathname,
877870
method: redirectPreservingMethod ? request.method : 'GET'
878871
}));
879-
return this.performFetchRequest(newUrl, fetchRequest, this.options.fetch ?? defaultFetch).catch((error) =>
880-
this.handleRequestError(error)
881-
);
872+
return this.performFetchRequest(newUrl, fetchRequest, this.options.fetch ?? defaultFetch);
882873
} else if (action.type === 'externalRedirect') {
883874
const fetchRequest = action.request ?? {
884875
// Change method to GET for 301, 302, 303 redirects
@@ -888,28 +879,24 @@ export class CommonHttpClient {
888879
credentials: request.credentials,
889880
redirect: 'error'
890881
};
891-
return this.performFetchRequest(newUrl, fetchRequest, this.options.externalFetch ?? defaultFetch).catch(
892-
(error) => this.handleRequestError(error)
893-
);
882+
return this.performFetchRequest(newUrl, fetchRequest, this.options.externalFetch ?? defaultFetch);
894883
} else {
895884
error.message = `Invalid redirect handler result for ${error.message}.`;
896-
throw this.processErrorIfNecessary(error);
885+
throw error;
897886
}
898887
}
899888

900-
protected handleRequestError(e: unknown): never | Promise<CommonHttpClientFetchResponse> {
901-
const error = e as CommonHttpClientError;
902-
return this.handleRedirect(error);
903-
}
904-
905889
/**
906890
* Request wrapper. It performs the request and handles errors.
907891
*/
908892
public async request(request: CommonHttpClientRequest): Promise<CommonHttpClientFetchResponse> {
909893
try {
910894
return await this.performRequest(request);
911-
} catch (e) {
912-
return await this.handleRequestError(e);
895+
} catch (error) {
896+
if (this.options.processError) {
897+
throw this.options.processError(error instanceof Error ? error : new Error(String(error)));
898+
}
899+
throw error;
913900
}
914901
}
915902

@@ -986,14 +973,16 @@ export class CommonHttpClient {
986973
}
987974
}
988975
if (!fetchResponse.ok) {
989-
throw new this.options.errorClass(
990-
url,
991-
fetchRequest,
992-
fetchResponse,
993-
this.options,
994-
this.options.formatHttpErrorMessage
995-
? this.options.formatHttpErrorMessage(fetchResponse, fetchRequest)
996-
: `HTTP Error ${fetchRequest.method} ${url.toString()} ${fetchResponse.status} (${fetchResponse.statusText})`
976+
return this.handleRedirect(
977+
new this.options.errorClass(
978+
url,
979+
fetchRequest,
980+
fetchResponse,
981+
this.options,
982+
this.options.formatHttpErrorMessage
983+
? this.options.formatHttpErrorMessage(fetchResponse, fetchRequest)
984+
: `HTTP Error ${fetchRequest.method} ${url.toString()} ${fetchResponse.status} (${fetchResponse.statusText})`
985+
) as CommonHttpClientError
997986
);
998987
}
999988
return fetchResponse;

0 commit comments

Comments
 (0)