Use Error subclasses instead of throwing strings
parent
70568a65ff
commit
b92356b844
|
|
@ -1,4 +1,6 @@
|
|||
import {
|
||||
ApiClientError,
|
||||
ApiError,
|
||||
ApiResponse,
|
||||
LibreCcmApiClient,
|
||||
} from "@libreccm/ccm-apiclient-commons";
|
||||
|
|
@ -21,10 +23,9 @@ export class ConfigurationApiClient {
|
|||
}
|
||||
|
||||
async getConfigurations(): Promise<ConfigurationInfo[]> {
|
||||
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<ConfigurationInfo> {
|
||||
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<Record<string, unknown>> {
|
||||
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<string, unknown>;
|
||||
} 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<unknown> {
|
||||
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<void> {
|
||||
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}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue