From fcd50df49dfc54e8cb361a4906ed820e111f0dec Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Wed, 12 Aug 2020 19:08:40 +0200 Subject: [PATCH] Use Error instances for throw Former-commit-id: edcc000d571b0342a90097e65e2a36bbceffb0e9 --- .../typescript/clients/applications-api.ts | 231 ++++++++++++------ 1 file changed, 154 insertions(+), 77 deletions(-) diff --git a/ccm-core-apiclient/src/main/typescript/clients/applications-api.ts b/ccm-core-apiclient/src/main/typescript/clients/applications-api.ts index 78eee4f44..c10decd7d 100644 --- a/ccm-core-apiclient/src/main/typescript/clients/applications-api.ts +++ b/ccm-core-apiclient/src/main/typescript/clients/applications-api.ts @@ -2,6 +2,7 @@ import { ApiResponse, LibreCcmApiClient, ListView, + ApiClientError, ApiError, } from "@libreccm/ccm-apiclient-commons"; @@ -34,14 +35,12 @@ export class WebApiClient { limit: number, offset: number ): Promise> { + const url = `${this.#APPS_API_PREFIX}`; try { - const response: ApiResponse = await this.#apiClient.get( - `${this.#APPS_API_PREFIX}`, - { - limit: limit.toString(), - offset: offset.toString(), - } - ); + const response: ApiResponse = await this.#apiClient.get(url, { + limit: limit.toString(), + offset: offset.toString(), + }); if (response.ok) { const result: Record< @@ -63,53 +62,86 @@ export class WebApiClient { offset: result.offset as number, }; } else { - throw `Failed to get application instances: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "get", + "Failed to get application instances", + url + ); } } catch (err) { - throw `Failed to get application instances: ${err}`; + if (err instanceof ApiClientError) { + throw err; + } else { + throw new ApiClientError( + `Failed to get application instances: ${err}` + ); + } } } async getApplication( appIdentifier: string | number ): Promise { + const url = `${this.#APPS_API_PREFIX}/${buildIdentifierParam( + appIdentifier + )}`; try { - const response: ApiResponse = await this.#apiClient.get( - `${this.#APPS_API_PREFIX}/${buildIdentifierParam( - appIdentifier - )}` - ); + const response: ApiResponse = await this.#apiClient.get(url); if (response.ok) { return buildCcmApplicationFromRecord( (await response.json()) as Record ); } else { - throw `Failed to get application instance ${appIdentifier}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "get", + `Failed to get application instance ${appIdentifier}`, + url + ); } } catch (err) { - throw `Failed to get application instance ${appIdentifier}: ${err}`; + if (err instanceof ApiClientError) { + throw err; + } else { + throw new ApiClientError( + `Failed to get application instance ${appIdentifier}: ${err}` + ); + } } } async createApplication(application: CcmApplication): Promise { + const url = `${this.#APPS_API_PREFIX}`; try { const response: ApiResponse = await this.#apiClient.post( - `${this.#APPS_API_PREFIX}`, + url, JSON.stringify(application) ); if (response.ok) { return; } else { - throw `Failed to create new application instance of - type ${application.applicationType}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "post", + `Failed to create new application instance of type + ${application.applicationType}`, + url + ); } } catch (err) { - throw `Failed to create new application instance of - type ${application.applicationType}: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to create new application instance of type + ${application.applicationType}: ${err}` + ); + } } } @@ -117,44 +149,64 @@ export class WebApiClient { appIdentifier: string | number, application: CcmApplication ): Promise { + const url = `${this.#APPS_API_PREFIX}/${buildIdentifierParam( + appIdentifier + )}`; try { const response: ApiResponse = await this.#apiClient.put( - `${this.#APPS_API_PREFIX}/${buildIdentifierParam( - appIdentifier - )}`, + url, JSON.stringify(application) ); if (response.ok) { return; } else { - throw `Failed to update pplication instance ${appIdentifier}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "put", + `Failed to update application instance ${appIdentifier}`, + url + ); } } catch (err) { - throw `Failed to update application instance ${appIdentifier}: - ${application.applicationType}: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to update application instance ${appIdentifier}: + ${application.applicationType}: ${err}` + ); + } } } async deleteApplication(appIdentifier: string | number): Promise { + const url = `${this.#APPS_API_PREFIX}/${buildIdentifierParam( + appIdentifier + )}`; try { - const response: ApiResponse = await this.#apiClient.delete( - `${this.#APPS_API_PREFIX}/${buildIdentifierParam( - appIdentifier - )}` - ); + const response: ApiResponse = await this.#apiClient.delete(url); if (response.ok) { return; } else { - throw `Failed to delete application - instance ${appIdentifier}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "delete", + `Failed to delete application instance ${appIdentifier}`, + url + ); } } catch (err) { - throw `Failed to delete application - instance ${appIdentifier}: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to delete application instance ${appIdentifier}: ${err}` + ); + } } } @@ -163,16 +215,14 @@ export class WebApiClient { limit: number, offset: number ): Promise> { + const url = `${this.#APPS_API_PREFIX}/${buildIdentifierParam( + appIdentifier + )}/domains`; try { - const response: ApiResponse = await this.#apiClient.get( - `${this.#APPS_API_PREFIX}/${buildIdentifierParam( - appIdentifier - )}/domains`, - { - limit: limit.toString(), - offset: offset.toString(), - } - ); + const response: ApiResponse = await this.#apiClient.get(url, { + limit: limit.toString(), + offset: offset.toString(), + }); if (response.ok) { const result: Record< @@ -194,13 +244,22 @@ export class WebApiClient { offset: result.limit as number, }; } else { - throw `Failed to get domains of application - instance ${appIdentifier}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "get", + `Failed to get domains of application instance ${appIdentifier}`, + url + ); } } catch (err) { - throw `Failed to get domains of application - instance ${appIdentifier}: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to get domains of application instance ${appIdentifier}: ${err}` + ); + } } } @@ -208,24 +267,33 @@ export class WebApiClient { appIdentifier: string | number, domainIdenfifier: string | number ): Promise { + const appParam = buildIdentifierParam(appIdentifier); + const domainParam = buildIdentifierParam(domainIdenfifier); + const url = `${ + this.#APPS_API_PREFIX + }/${appParam}/domains/${domainParam}`; try { - const appParam = buildIdentifierParam(appIdentifier); - const domainParam = buildIdentifierParam(domainIdenfifier); - - const response: ApiResponse = await this.#apiClient.put( - `${this.#APPS_API_PREFIX}/${appParam}/domains/${domainParam}` - ); + const response: ApiResponse = await this.#apiClient.put(url); if (response.ok) { return; } else { - throw `Failed to add domain ${domainIdenfifier} to - application ${appIdentifier}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "put", + `Failed to add domain ${domainIdenfifier} to application ${appIdentifier}`, + url + ); } } catch (err) { - throw `Failed to add domain ${domainIdenfifier} to - application ${appIdentifier}: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to add domain ${domainIdenfifier} to application ${appIdentifier}: ${err}` + ); + } } } @@ -233,24 +301,33 @@ export class WebApiClient { appIdentifier: string | number, domainIdenfifier: string | number ): Promise { + const appParam = buildIdentifierParam(appIdentifier); + const domainParam = buildIdentifierParam(domainIdenfifier); + const url = `${ + this.#APPS_API_PREFIX + }/${appParam}/domains/${domainParam}`; try { - const appParam = buildIdentifierParam(appIdentifier); - const domainParam = buildIdentifierParam(domainIdenfifier); - - const response: ApiResponse = await this.#apiClient.delete( - `${this.#APPS_API_PREFIX}/${appParam}/domains/${domainParam}` - ); + const response: ApiResponse = await this.#apiClient.delete(url); if (response.ok) { return; } else { - throw `Failed to remove domain ${domainIdenfifier} from - application ${appIdentifier}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "delete", + `Failed to remove domain ${domainIdenfifier} from application instance ${appIdentifier}`, + url + ); } } catch (err) { - throw `Failed to remove domain ${domainIdenfifier} from - application ${appIdentifier}: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to remove domain ${domainIdenfifier} from application ${appIdentifier}: ${err}` + ); + } } } }