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, ApiResponse,
LibreCcmApiClient, LibreCcmApiClient,
ListView, ListView,
ApiError,
ApiClientError,
} from "@libreccm/ccm-apiclient-commons"; } from "@libreccm/ccm-apiclient-commons";
import { import {
@ -26,59 +28,93 @@ export class CategorizationApiClient {
domain: string, domain: string,
path: string path: string
): Promise<Category> { ): Promise<Category> {
const url = `${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`;
try { try {
const response: ApiResponse = await this.#apiClient.get( const response: ApiResponse = await this.#apiClient.get(url);
`${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`
);
if (response.ok) { if (response.ok) {
return buildCategoryFromRecord( return buildCategoryFromRecord(
(await response.json()) as Record<string, unknown> (await response.json()) as Record<string, unknown>
); );
} else { } else {
throw `Failed to get category ${path} of domain ${domain}: throw new ApiError(
${response.status} ${response.statusText}`; response.status,
response.statusText,
"get",
`Failed to get category ${path} of domain ${domain}`,
url
);
} }
} catch (err) { } 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> { async getCategoryById(categoryId: number): Promise<Category> {
const url = `${
this.#CATEGORIES_API_PREFIX
}/categories/ID-${categoryId}`;
try { try {
const response: ApiResponse = await this.#apiClient.get( const response: ApiResponse = await this.#apiClient.get(url);
`${this.#CATEGORIES_API_PREFIX}/categories/ID-${categoryId}`
);
if (response.ok) { if (response.ok) {
return buildCategoryFromRecord( return buildCategoryFromRecord(
(await response.json()) as Record<string, unknown> (await response.json()) as Record<string, unknown>
); );
} else { } else {
throw `Failed to get category with ID ${categoryId}: throw new ApiError(
${response.status} ${response.statusText}`; response.status,
response.statusText,
"get",
`Failed to get category with ID ${categoryId}`,
url
);
} }
} catch (err) { } 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> { async getCategoryByUuid(uuid: string): Promise<Category> {
try { try {
const response: ApiResponse = await this.#apiClient.get( const url = `${
`${this.#CATEGORIES_API_PREFIX}/categories/UUID-${uuid}` this.#CATEGORIES_API_PREFIX
); }/categories/UUID-${uuid}`;
const response: ApiResponse = await this.#apiClient.get(url);
if (response.ok) { if (response.ok) {
return buildCategoryFromRecord( return buildCategoryFromRecord(
(await response.json()) as Record<string, unknown> (await response.json()) as Record<string, unknown>
); );
} else { } else {
throw `Failed to get category with UUID ${uuid}: throw new ApiError(
${response.status} ${response.statusText}`; response.status,
response.statusText,
"get",
`Failed to get category with UUID ${uuid}`,
url
);
} }
} catch (err) { } 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>> { ): Promise<ListView<Categorization>> {
try { try {
const response: ApiResponse = await this.#apiClient.get( const response: ApiResponse = await this.#apiClient.get(
`${ `${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}/@objects`,
this.#CATEGORIES_API_PREFIX
}/UUID-${uuid}/@objects`,
{ {
limit: limit.toString(), limit: limit.toString(),
offset: offset.toString() offset: offset.toString(),
} }
); );