diff --git a/ccm-core-apiclient/src/main/typescript/clients/categorization-api.ts b/ccm-core-apiclient/src/main/typescript/clients/categorization-api.ts index 00832f931..a8a75b6b1 100644 --- a/ccm-core-apiclient/src/main/typescript/clients/categorization-api.ts +++ b/ccm-core-apiclient/src/main/typescript/clients/categorization-api.ts @@ -2,6 +2,8 @@ import { ApiResponse, LibreCcmApiClient, ListView, + ApiError, + ApiClientError, } from "@libreccm/ccm-apiclient-commons"; import { @@ -26,59 +28,93 @@ export class CategorizationApiClient { domain: string, path: string ): Promise { + const url = `${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`; try { - const response: ApiResponse = await this.#apiClient.get( - `${this.#CATEGORIES_API_PREFIX}/${domain}/${path}` - ); + const response: ApiResponse = await this.#apiClient.get(url); if (response.ok) { return buildCategoryFromRecord( (await response.json()) as Record ); } else { - throw `Failed to get category ${path} of domain ${domain}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "get", + `Failed to get category ${path} of domain ${domain}`, + url + ); } } catch (err) { - throw `Failed to get category ${path} of domain ${domain}: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to get category ${path} of domain ${domain}: ${err}` + ); + } } } async getCategoryById(categoryId: number): Promise { + const url = `${ + this.#CATEGORIES_API_PREFIX + }/categories/ID-${categoryId}`; try { - const response: ApiResponse = await this.#apiClient.get( - `${this.#CATEGORIES_API_PREFIX}/categories/ID-${categoryId}` - ); + const response: ApiResponse = await this.#apiClient.get(url); if (response.ok) { return buildCategoryFromRecord( (await response.json()) as Record ); } else { - throw `Failed to get category with ID ${categoryId}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "get", + `Failed to get category with ID ${categoryId}`, + url + ); } } catch (err) { - throw `Failed to get category with Id ${categoryId}: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to get category with Id ${categoryId}: ${err}` + ); + } } } async getCategoryByUuid(uuid: string): Promise { try { - const response: ApiResponse = await this.#apiClient.get( - `${this.#CATEGORIES_API_PREFIX}/categories/UUID-${uuid}` - ); + const url = `${ + this.#CATEGORIES_API_PREFIX + }/categories/UUID-${uuid}`; + const response: ApiResponse = await this.#apiClient.get(url); if (response.ok) { return buildCategoryFromRecord( (await response.json()) as Record ); } else { - throw `Failed to get category with UUID ${uuid}: - ${response.status} ${response.statusText}`; + throw new ApiError( + response.status, + response.statusText, + "get", + `Failed to get category with UUID ${uuid}`, + url + ); } } catch (err) { - throw `Failed to get category with UUID ${uuid}: ${err}`; + if (err instanceof ApiError) { + throw err; + } else { + throw new ApiClientError( + `Failed to get category with UUID ${uuid}: ${err}` + ); + } } } @@ -495,12 +531,10 @@ export class CategorizationApiClient { ): Promise> { try { const response: ApiResponse = await this.#apiClient.get( - `${ - this.#CATEGORIES_API_PREFIX - }/UUID-${uuid}/@objects`, + `${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}/@objects`, { limit: limit.toString(), - offset: offset.toString() + offset: offset.toString(), } );