Use Error subtypes instead of throwing strings

Former-commit-id: 66bc3d20bb
restapi
Jens Pelzetter 2020-08-13 14:43:34 +02:00
parent 1149a20b89
commit 6ecf9993c7
1 changed files with 56 additions and 22 deletions

View File

@ -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<Category> {
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<string, unknown>
);
} 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<Category> {
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<string, unknown>
);
} 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<Category> {
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<string, unknown>
);
} 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<ListView<Categorization>> {
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(),
}
);