From b92356b844016907a8f3e999473af7a159315dbd Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Fri, 14 Aug 2020 17:16:06 +0200 Subject: [PATCH] Use Error subclasses instead of throwing strings --- .../typescript/clients/configuration-api.ts | 126 ++++++++++++------ 1 file changed, 86 insertions(+), 40 deletions(-) diff --git a/ccm-core-apiclient/src/main/typescript/clients/configuration-api.ts b/ccm-core-apiclient/src/main/typescript/clients/configuration-api.ts index dec23b6dd..360d6dc26 100644 --- a/ccm-core-apiclient/src/main/typescript/clients/configuration-api.ts +++ b/ccm-core-apiclient/src/main/typescript/clients/configuration-api.ts @@ -1,4 +1,6 @@ import { + ApiClientError, + ApiError, ApiResponse, LibreCcmApiClient, } from "@libreccm/ccm-apiclient-commons"; @@ -21,10 +23,9 @@ export class ConfigurationApiClient { } async getConfigurations(): Promise { + const url = `${this.#CONFIGURATION_API_PREFIX}/`; try { - const response: ApiResponse = await this.#apiClient.get( - `${this.#CONFIGURATION_API_PREFIX}/` - ); + const response: ApiResponse = await this.#apiClient.get(url); if (response.ok) { const result: Record< @@ -36,19 +37,29 @@ export class ConfigurationApiClient { buildConfigurationInfoFromRecord(record) ); } else { - throw `Failed to retrieve configuration info: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "get", + `Failed to retrieve configuration info`, + url + ); } } catch (err) { - throw `Failed to retrieve configuration info: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to retrieve configuration info: ${err}` + ); + } } } async getConfiguration(confName: string): Promise { + const url = `${this.#CONFIGURATION_API_PREFIX}/${confName}`; try { - const response: ApiResponse = await this.#apiClient.get( - `${this.#CONFIGURATION_API_PREFIX}/${confName}` - ); + const response: ApiResponse = await this.#apiClient.get(url); if (response.ok) { const result: Record< @@ -58,53 +69,78 @@ export class ConfigurationApiClient { return buildConfigurationInfoFromRecord(result); } else { - throw `Failed to get configuration info for - configuration ${confName}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "get", + `Failed to get configuration info for configuration ${confName}`, + url + ); } } catch (err) { - throw `Failed to get configuration info for - configuration ${confName}: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to get configuration info for configuration ${confName}: ${err}` + ); + } } } async getSettings(confName: string): Promise> { + const url = `${this.#CONFIGURATION_API_PREFIX}/${confName}/settings`; try { - const response: ApiResponse = await this.#apiClient.get( - `${this.#CONFIGURATION_API_PREFIX}/${confName}/settings` - ); + const response: ApiResponse = await this.#apiClient.get(url); if (response.ok) { return (await response.json()) as Record; } else { - throw `Failed to get settings of - configuration ${confName}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "get", + `Failed to get settings of configuration ${confName}`, + url + ); } } catch (err) { - throw `Failed to get settings of - configuration ${confName}: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to get settings of configuration ${confName}: ${err}` + ); + } } } async getSetting(confName: string, setting: string): Promise { + const url = `${ + this.#CONFIGURATION_API_PREFIX + }/${confName}/settings/${setting}`; try { - const response: ApiResponse = await this.#apiClient.get( - `${ - this.#CONFIGURATION_API_PREFIX - }/${confName}/settings/${setting}` - ); + const response: ApiResponse = await this.#apiClient.get(url); if (response.ok) { return await response.json(); } else { - throw `Failed to get setting ${setting} of - configuration ${confName}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "get", + `Failed to get setting ${setting} of configuration ${confName}`, + url + ); } } catch (err) { - throw `Failed to get setting ${setting} of - configuration ${confName}: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to get setting ${setting} of configuration ${confName}: ${err}` + ); + } } } @@ -113,24 +149,34 @@ export class ConfigurationApiClient { setting: string, value: unknown ): Promise { + const url = `${ + this.#CONFIGURATION_API_PREFIX + }/${confName}/settings/${setting}`; try { const response: ApiResponse = await this.#apiClient.put( - `${ - this.#CONFIGURATION_API_PREFIX - }/${confName}/settings/${setting}`, + url, value as RequestBody ); if (response.ok) { return; } else { - throw `Failed to update setting ${setting} of - configuration ${confName}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "put", + `Failed to update setting ${setting} of configuration ${confName}`, + url + ); } } catch (err) { - throw `Failed to update setting ${setting} of - configuration ${confName}: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to update setting ${setting} of configuration ${confName}: ${err}` + ); + } } } }