Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[typescript-fetch] Support error handler in middleware #12716

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,25 @@ export class BaseAPI {
}) || fetchParams;
}
}
let response = await (this.configuration.fetchApi || fetch)(fetchParams.url, fetchParams.init);
let response = undefined;
try {
response = await (this.configuration.fetchApi || fetch)(fetchParams.url, fetchParams.init);
} catch (e) {
for (const middleware of this.middleware) {
if (middleware.onError) {
response = await middleware.onError({
fetch: this.fetchApi,
url: fetchParams.url,
init: fetchParams.init,
error: e,
response: response ? response.clone() : undefined,
}) || response;
}
}
if (response !== undefined) {
throw new FetchError(e, 'The request failed and the interceptors did not return an alternative response');
}
}
for (const middleware of this.middleware) {
if (middleware.post) {
response = await middleware.post({
Expand Down Expand Up @@ -207,6 +225,13 @@ export class ResponseError extends Error {
}
}

export class FetchError extends Error {
name: "FetchError" = "FetchError";
constructor(public cause: unknown, msg?: string) {
super(msg);
}
}

export class RequiredError extends Error {
name: "RequiredError" = "RequiredError";
constructor(public field: string, msg?: string) {
Expand Down Expand Up @@ -315,9 +340,18 @@ export interface ResponseContext {
response: Response;
}

export interface ErrorContext {
fetch: FetchAPI;
url: string;
init: RequestInit;
error: unknown;
response?: Response;
}

export interface Middleware {
pre?(context: RequestContext): Promise<FetchParams | void>;
post?(context: ResponseContext): Promise<Response | void>;
onError?(context: ErrorContext): Promise<Response | void>;
}

export interface ApiResponse<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,25 @@ export class BaseAPI {
}) || fetchParams;
}
}
let response = await (this.configuration.fetchApi || fetch)(fetchParams.url, fetchParams.init);
let response = undefined;
try {
response = await (this.configuration.fetchApi || fetch)(fetchParams.url, fetchParams.init);
} catch (e) {
for (const middleware of this.middleware) {
if (middleware.onError) {
response = await middleware.onError({
fetch: this.fetchApi,
url: fetchParams.url,
init: fetchParams.init,
error: e,
response: response ? response.clone() : undefined,
}) || response;
}
}
if (response !== undefined) {
throw new FetchError(e, 'The request failed and the interceptors did not return an alternative response');
}
}
for (const middleware of this.middleware) {
if (middleware.post) {
response = await middleware.post({
Expand Down Expand Up @@ -218,6 +236,13 @@ export class ResponseError extends Error {
}
}

export class FetchError extends Error {
name: "FetchError" = "FetchError";
constructor(public cause: unknown, msg?: string) {
super(msg);
}
}

export class RequiredError extends Error {
name: "RequiredError" = "RequiredError";
constructor(public field: string, msg?: string) {
Expand Down Expand Up @@ -322,9 +347,18 @@ export interface ResponseContext {
response: Response;
}

export interface ErrorContext {
fetch: FetchAPI;
url: string;
init: RequestInit;
error: unknown;
response?: Response;
}

export interface Middleware {
pre?(context: RequestContext): Promise<FetchParams | void>;
post?(context: ResponseContext): Promise<Response | void>;
onError?(context: ErrorContext): Promise<Response | void>;
}

export interface ApiResponse<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,25 @@ export class BaseAPI {
}) || fetchParams;
}
}
let response = await (this.configuration.fetchApi || fetch)(fetchParams.url, fetchParams.init);
let response = undefined;
try {
response = await (this.configuration.fetchApi || fetch)(fetchParams.url, fetchParams.init);
} catch (e) {
for (const middleware of this.middleware) {
if (middleware.onError) {
response = await middleware.onError({
fetch: this.fetchApi,
url: fetchParams.url,
init: fetchParams.init,
error: e,
response: response ? response.clone() : undefined,
}) || response;
}
}
if (response !== undefined) {
throw new FetchError(e, 'The request failed and the interceptors did not return an alternative response');
}
}
for (const middleware of this.middleware) {
if (middleware.post) {
response = await middleware.post({
Expand Down Expand Up @@ -218,6 +236,13 @@ export class ResponseError extends Error {
}
}

export class FetchError extends Error {
name: "FetchError" = "FetchError";
constructor(public cause: unknown, msg?: string) {
super(msg);
}
}

export class RequiredError extends Error {
name: "RequiredError" = "RequiredError";
constructor(public field: string, msg?: string) {
Expand Down Expand Up @@ -322,9 +347,18 @@ export interface ResponseContext {
response: Response;
}

export interface ErrorContext {
fetch: FetchAPI;
url: string;
init: RequestInit;
error: unknown;
response?: Response;
}

export interface Middleware {
pre?(context: RequestContext): Promise<FetchParams | void>;
post?(context: ResponseContext): Promise<Response | void>;
onError?(context: ErrorContext): Promise<Response | void>;
}

export interface ApiResponse<T> {
Expand Down
36 changes: 35 additions & 1 deletion samples/client/petstore/typescript-fetch/builds/enum/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,25 @@ export class BaseAPI {
}) || fetchParams;
}
}
let response = await (this.configuration.fetchApi || fetch)(fetchParams.url, fetchParams.init);
let response = undefined;
try {
response = await (this.configuration.fetchApi || fetch)(fetchParams.url, fetchParams.init);
} catch (e) {
for (const middleware of this.middleware) {
if (middleware.onError) {
response = await middleware.onError({
fetch: this.fetchApi,
url: fetchParams.url,
init: fetchParams.init,
error: e,
response: response ? response.clone() : undefined,
}) || response;
}
}
if (response !== undefined) {
throw new FetchError(e, 'The request failed and the interceptors did not return an alternative response');
}
}
for (const middleware of this.middleware) {
if (middleware.post) {
response = await middleware.post({
Expand Down Expand Up @@ -218,6 +236,13 @@ export class ResponseError extends Error {
}
}

export class FetchError extends Error {
name: "FetchError" = "FetchError";
constructor(public cause: unknown, msg?: string) {
super(msg);
}
}

export class RequiredError extends Error {
name: "RequiredError" = "RequiredError";
constructor(public field: string, msg?: string) {
Expand Down Expand Up @@ -322,9 +347,18 @@ export interface ResponseContext {
response: Response;
}

export interface ErrorContext {
fetch: FetchAPI;
url: string;
init: RequestInit;
error: unknown;
response?: Response;
}

export interface Middleware {
pre?(context: RequestContext): Promise<FetchParams | void>;
post?(context: ResponseContext): Promise<Response | void>;
onError?(context: ErrorContext): Promise<Response | void>;
}

export interface ApiResponse<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,25 @@ export class BaseAPI {
}) || fetchParams;
}
}
let response = await (this.configuration.fetchApi || fetch)(fetchParams.url, fetchParams.init);
let response = undefined;
try {
response = await (this.configuration.fetchApi || fetch)(fetchParams.url, fetchParams.init);
} catch (e) {
for (const middleware of this.middleware) {
if (middleware.onError) {
response = await middleware.onError({
fetch: this.fetchApi,
url: fetchParams.url,
init: fetchParams.init,
error: e,
response: response ? response.clone() : undefined,
}) || response;
}
}
if (response !== undefined) {
throw new FetchError(e, 'The request failed and the interceptors did not return an alternative response');
}
}
for (const middleware of this.middleware) {
if (middleware.post) {
response = await middleware.post({
Expand Down Expand Up @@ -218,6 +236,13 @@ export class ResponseError extends Error {
}
}

export class FetchError extends Error {
name: "FetchError" = "FetchError";
constructor(public cause: unknown, msg?: string) {
super(msg);
}
}

export class RequiredError extends Error {
name: "RequiredError" = "RequiredError";
constructor(public field: string, msg?: string) {
Expand Down Expand Up @@ -322,9 +347,18 @@ export interface ResponseContext {
response: Response;
}

export interface ErrorContext {
fetch: FetchAPI;
url: string;
init: RequestInit;
error: unknown;
response?: Response;
}

export interface Middleware {
pre?(context: RequestContext): Promise<FetchParams | void>;
post?(context: ResponseContext): Promise<Response | void>;
onError?(context: ErrorContext): Promise<Response | void>;
}

export interface ApiResponse<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,25 @@ export class BaseAPI {
}) || fetchParams;
}
}
let response = await (this.configuration.fetchApi || fetch)(fetchParams.url, fetchParams.init);
let response = undefined;
try {
response = await (this.configuration.fetchApi || fetch)(fetchParams.url, fetchParams.init);
} catch (e) {
for (const middleware of this.middleware) {
if (middleware.onError) {
response = await middleware.onError({
fetch: this.fetchApi,
url: fetchParams.url,
init: fetchParams.init,
error: e,
response: response ? response.clone() : undefined,
}) || response;
}
}
if (response !== undefined) {
throw new FetchError(e, 'The request failed and the interceptors did not return an alternative response');
}
}
for (const middleware of this.middleware) {
if (middleware.post) {
response = await middleware.post({
Expand Down Expand Up @@ -218,6 +236,13 @@ export class ResponseError extends Error {
}
}

export class FetchError extends Error {
name: "FetchError" = "FetchError";
constructor(public cause: unknown, msg?: string) {
super(msg);
}
}

export class RequiredError extends Error {
name: "RequiredError" = "RequiredError";
constructor(public field: string, msg?: string) {
Expand Down Expand Up @@ -322,9 +347,18 @@ export interface ResponseContext {
response: Response;
}

export interface ErrorContext {
fetch: FetchAPI;
url: string;
init: RequestInit;
error: unknown;
response?: Response;
}

export interface Middleware {
pre?(context: RequestContext): Promise<FetchParams | void>;
post?(context: ResponseContext): Promise<Response | void>;
onError?(context: ErrorContext): Promise<Response | void>;
}

export interface ApiResponse<T> {
Expand Down
Loading