From e8e1f6b500abeacf1eb111504d3f67a68cc677a6 Mon Sep 17 00:00:00 2001 From: javier-garcia-meteologica <61506396+javier-garcia-meteologica@users.noreply.github.com> Date: Mon, 4 Apr 2022 20:31:47 +0200 Subject: [PATCH] feat(typescript-fetch): use ReponseError (#10477) (#10478) --- .../src/main/resources/typescript-fetch/runtime.mustache | 9 ++++++++- .../typescript-fetch/builds/default-v3.0/runtime.ts | 9 ++++++++- .../petstore/typescript-fetch/builds/default/runtime.ts | 9 ++++++++- .../petstore/typescript-fetch/builds/enum/runtime.ts | 9 ++++++++- .../typescript-fetch/builds/es6-target/src/runtime.ts | 9 ++++++++- .../builds/multiple-parameters/runtime.ts | 9 ++++++++- .../builds/prefix-parameter-interfaces/src/runtime.ts | 9 ++++++++- .../builds/sagas-and-records/src/runtime.ts | 9 ++++++++- .../builds/typescript-three-plus/src/runtime.ts | 9 ++++++++- .../typescript-fetch/builds/with-interfaces/runtime.ts | 9 ++++++++- .../builds/with-npm-version/src/runtime.ts | 9 ++++++++- .../builds/without-runtime-checks/src/runtime.ts | 9 ++++++++- 12 files changed, 96 insertions(+), 12 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache b/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache index b9029f5fb0f..25a2731a3d3 100644 --- a/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache @@ -39,7 +39,7 @@ export class BaseAPI { if (response.status >= 200 && response.status < 300) { return response; } - throw response; + throw new ResponseError(response, 'Response returned an error code'); } private createFetchParams(context: RequestOpts, initOverrides?: RequestInit) { @@ -103,6 +103,13 @@ export class BaseAPI { } }; +export class ResponseError extends Error { + name: "ResponseError" = "ResponseError"; + constructor(public response: Response, msg?: string) { + super(msg); + } +} + export class RequiredError extends Error { name: "RequiredError" = "RequiredError"; constructor(public field: string, msg?: string) { diff --git a/samples/client/petstore/typescript-fetch/builds/default-v3.0/runtime.ts b/samples/client/petstore/typescript-fetch/builds/default-v3.0/runtime.ts index 79873e03653..514468d9bae 100644 --- a/samples/client/petstore/typescript-fetch/builds/default-v3.0/runtime.ts +++ b/samples/client/petstore/typescript-fetch/builds/default-v3.0/runtime.ts @@ -50,7 +50,7 @@ export class BaseAPI { if (response.status >= 200 && response.status < 300) { return response; } - throw response; + throw new ResponseError(response, 'Response returned an error code'); } private createFetchParams(context: RequestOpts, initOverrides?: RequestInit) { @@ -114,6 +114,13 @@ export class BaseAPI { } }; +export class ResponseError extends Error { + name: "ResponseError" = "ResponseError"; + constructor(public response: Response, msg?: string) { + super(msg); + } +} + export class RequiredError extends Error { name: "RequiredError" = "RequiredError"; constructor(public field: string, msg?: string) { diff --git a/samples/client/petstore/typescript-fetch/builds/default/runtime.ts b/samples/client/petstore/typescript-fetch/builds/default/runtime.ts index 95abeaf7377..50d88595798 100644 --- a/samples/client/petstore/typescript-fetch/builds/default/runtime.ts +++ b/samples/client/petstore/typescript-fetch/builds/default/runtime.ts @@ -50,7 +50,7 @@ export class BaseAPI { if (response.status >= 200 && response.status < 300) { return response; } - throw response; + throw new ResponseError(response, 'Response returned an error code'); } private createFetchParams(context: RequestOpts, initOverrides?: RequestInit) { @@ -114,6 +114,13 @@ export class BaseAPI { } }; +export class ResponseError extends Error { + name: "ResponseError" = "ResponseError"; + constructor(public response: Response, msg?: string) { + super(msg); + } +} + export class RequiredError extends Error { name: "RequiredError" = "RequiredError"; constructor(public field: string, msg?: string) { diff --git a/samples/client/petstore/typescript-fetch/builds/enum/runtime.ts b/samples/client/petstore/typescript-fetch/builds/enum/runtime.ts index 1f1ca3c30ae..6371c45fc60 100644 --- a/samples/client/petstore/typescript-fetch/builds/enum/runtime.ts +++ b/samples/client/petstore/typescript-fetch/builds/enum/runtime.ts @@ -50,7 +50,7 @@ export class BaseAPI { if (response.status >= 200 && response.status < 300) { return response; } - throw response; + throw new ResponseError(response, 'Response returned an error code'); } private createFetchParams(context: RequestOpts, initOverrides?: RequestInit) { @@ -114,6 +114,13 @@ export class BaseAPI { } }; +export class ResponseError extends Error { + name: "ResponseError" = "ResponseError"; + constructor(public response: Response, msg?: string) { + super(msg); + } +} + export class RequiredError extends Error { name: "RequiredError" = "RequiredError"; constructor(public field: string, msg?: string) { diff --git a/samples/client/petstore/typescript-fetch/builds/es6-target/src/runtime.ts b/samples/client/petstore/typescript-fetch/builds/es6-target/src/runtime.ts index 95abeaf7377..50d88595798 100644 --- a/samples/client/petstore/typescript-fetch/builds/es6-target/src/runtime.ts +++ b/samples/client/petstore/typescript-fetch/builds/es6-target/src/runtime.ts @@ -50,7 +50,7 @@ export class BaseAPI { if (response.status >= 200 && response.status < 300) { return response; } - throw response; + throw new ResponseError(response, 'Response returned an error code'); } private createFetchParams(context: RequestOpts, initOverrides?: RequestInit) { @@ -114,6 +114,13 @@ export class BaseAPI { } }; +export class ResponseError extends Error { + name: "ResponseError" = "ResponseError"; + constructor(public response: Response, msg?: string) { + super(msg); + } +} + export class RequiredError extends Error { name: "RequiredError" = "RequiredError"; constructor(public field: string, msg?: string) { diff --git a/samples/client/petstore/typescript-fetch/builds/multiple-parameters/runtime.ts b/samples/client/petstore/typescript-fetch/builds/multiple-parameters/runtime.ts index 95abeaf7377..50d88595798 100644 --- a/samples/client/petstore/typescript-fetch/builds/multiple-parameters/runtime.ts +++ b/samples/client/petstore/typescript-fetch/builds/multiple-parameters/runtime.ts @@ -50,7 +50,7 @@ export class BaseAPI { if (response.status >= 200 && response.status < 300) { return response; } - throw response; + throw new ResponseError(response, 'Response returned an error code'); } private createFetchParams(context: RequestOpts, initOverrides?: RequestInit) { @@ -114,6 +114,13 @@ export class BaseAPI { } }; +export class ResponseError extends Error { + name: "ResponseError" = "ResponseError"; + constructor(public response: Response, msg?: string) { + super(msg); + } +} + export class RequiredError extends Error { name: "RequiredError" = "RequiredError"; constructor(public field: string, msg?: string) { diff --git a/samples/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces/src/runtime.ts b/samples/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces/src/runtime.ts index 95abeaf7377..50d88595798 100644 --- a/samples/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces/src/runtime.ts +++ b/samples/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces/src/runtime.ts @@ -50,7 +50,7 @@ export class BaseAPI { if (response.status >= 200 && response.status < 300) { return response; } - throw response; + throw new ResponseError(response, 'Response returned an error code'); } private createFetchParams(context: RequestOpts, initOverrides?: RequestInit) { @@ -114,6 +114,13 @@ export class BaseAPI { } }; +export class ResponseError extends Error { + name: "ResponseError" = "ResponseError"; + constructor(public response: Response, msg?: string) { + super(msg); + } +} + export class RequiredError extends Error { name: "RequiredError" = "RequiredError"; constructor(public field: string, msg?: string) { diff --git a/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/runtime.ts b/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/runtime.ts index 239aa11c47f..5f45377161b 100644 --- a/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/runtime.ts +++ b/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/runtime.ts @@ -50,7 +50,7 @@ export class BaseAPI { if (response.status >= 200 && response.status < 300) { return response; } - throw response; + throw new ResponseError(response, 'Response returned an error code'); } private createFetchParams(context: RequestOpts, initOverrides?: RequestInit) { @@ -114,6 +114,13 @@ export class BaseAPI { } }; +export class ResponseError extends Error { + name: "ResponseError" = "ResponseError"; + constructor(public response: Response, msg?: string) { + super(msg); + } +} + export class RequiredError extends Error { name: "RequiredError" = "RequiredError"; constructor(public field: string, msg?: string) { diff --git a/samples/client/petstore/typescript-fetch/builds/typescript-three-plus/src/runtime.ts b/samples/client/petstore/typescript-fetch/builds/typescript-three-plus/src/runtime.ts index 239aa11c47f..5f45377161b 100644 --- a/samples/client/petstore/typescript-fetch/builds/typescript-three-plus/src/runtime.ts +++ b/samples/client/petstore/typescript-fetch/builds/typescript-three-plus/src/runtime.ts @@ -50,7 +50,7 @@ export class BaseAPI { if (response.status >= 200 && response.status < 300) { return response; } - throw response; + throw new ResponseError(response, 'Response returned an error code'); } private createFetchParams(context: RequestOpts, initOverrides?: RequestInit) { @@ -114,6 +114,13 @@ export class BaseAPI { } }; +export class ResponseError extends Error { + name: "ResponseError" = "ResponseError"; + constructor(public response: Response, msg?: string) { + super(msg); + } +} + export class RequiredError extends Error { name: "RequiredError" = "RequiredError"; constructor(public field: string, msg?: string) { diff --git a/samples/client/petstore/typescript-fetch/builds/with-interfaces/runtime.ts b/samples/client/petstore/typescript-fetch/builds/with-interfaces/runtime.ts index 95abeaf7377..50d88595798 100644 --- a/samples/client/petstore/typescript-fetch/builds/with-interfaces/runtime.ts +++ b/samples/client/petstore/typescript-fetch/builds/with-interfaces/runtime.ts @@ -50,7 +50,7 @@ export class BaseAPI { if (response.status >= 200 && response.status < 300) { return response; } - throw response; + throw new ResponseError(response, 'Response returned an error code'); } private createFetchParams(context: RequestOpts, initOverrides?: RequestInit) { @@ -114,6 +114,13 @@ export class BaseAPI { } }; +export class ResponseError extends Error { + name: "ResponseError" = "ResponseError"; + constructor(public response: Response, msg?: string) { + super(msg); + } +} + export class RequiredError extends Error { name: "RequiredError" = "RequiredError"; constructor(public field: string, msg?: string) { diff --git a/samples/client/petstore/typescript-fetch/builds/with-npm-version/src/runtime.ts b/samples/client/petstore/typescript-fetch/builds/with-npm-version/src/runtime.ts index 95abeaf7377..50d88595798 100644 --- a/samples/client/petstore/typescript-fetch/builds/with-npm-version/src/runtime.ts +++ b/samples/client/petstore/typescript-fetch/builds/with-npm-version/src/runtime.ts @@ -50,7 +50,7 @@ export class BaseAPI { if (response.status >= 200 && response.status < 300) { return response; } - throw response; + throw new ResponseError(response, 'Response returned an error code'); } private createFetchParams(context: RequestOpts, initOverrides?: RequestInit) { @@ -114,6 +114,13 @@ export class BaseAPI { } }; +export class ResponseError extends Error { + name: "ResponseError" = "ResponseError"; + constructor(public response: Response, msg?: string) { + super(msg); + } +} + export class RequiredError extends Error { name: "RequiredError" = "RequiredError"; constructor(public field: string, msg?: string) { diff --git a/samples/client/petstore/typescript-fetch/builds/without-runtime-checks/src/runtime.ts b/samples/client/petstore/typescript-fetch/builds/without-runtime-checks/src/runtime.ts index 95abeaf7377..50d88595798 100644 --- a/samples/client/petstore/typescript-fetch/builds/without-runtime-checks/src/runtime.ts +++ b/samples/client/petstore/typescript-fetch/builds/without-runtime-checks/src/runtime.ts @@ -50,7 +50,7 @@ export class BaseAPI { if (response.status >= 200 && response.status < 300) { return response; } - throw response; + throw new ResponseError(response, 'Response returned an error code'); } private createFetchParams(context: RequestOpts, initOverrides?: RequestInit) { @@ -114,6 +114,13 @@ export class BaseAPI { } }; +export class ResponseError extends Error { + name: "ResponseError" = "ResponseError"; + constructor(public response: Response, msg?: string) { + super(msg); + } +} + export class RequiredError extends Error { name: "RequiredError" = "RequiredError"; constructor(public field: string, msg?: string) {