Use Error subclasses instead of throwing strings

Jens Pelzetter 2020-08-14 07:14:48 +02:00
parent 66bc3d20bb
commit 0c659aa9a2
1 changed files with 200 additions and 74 deletions

View File

@ -123,20 +123,32 @@ export class CategorizationApiClient {
path: string,
category: Category
): Promise<void> {
const url = `${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`;
try {
const response: ApiResponse = await this.#apiClient.put(
`${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`,
url,
JSON.stringify(category)
);
if (response.ok) {
return;
} else {
throw `Failed to update category ${path} of domain ${domain}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"put",
`Failed to update category ${path} of domain ${domain}`,
url
);
}
} catch (err) {
throw `Failed to update category ${path} of domain ${domain}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to update category ${path} of domain ${domain}: ${err}`
);
}
}
}
@ -144,20 +156,32 @@ export class CategorizationApiClient {
categoryId: number,
category: Category
): Promise<void> {
const url = `${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`;
try {
const response: ApiResponse = await this.#apiClient.put(
`${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`,
url,
JSON.stringify(category)
);
if (response.ok) {
return;
} else {
throw `Failed to update category with ID ${categoryId}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"put",
`Failed to update category with ID ${categoryId}`,
url
);
}
} catch (err) {
throw `Failed to update category ${categoryId}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to update category ${categoryId}: ${err}`
);
}
}
}
@ -166,70 +190,112 @@ export class CategorizationApiClient {
category: Category
): Promise<void> {
try {
const url = `${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`;
const response: ApiResponse = await this.#apiClient.put(
`${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`,
url,
JSON.stringify(category)
);
if (response.ok) {
return;
} else {
throw `Failed to update category with UUID ${uuid}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"put",
`Failed to update category with UUID ${uuid}`,
url
);
}
} catch (err) {
throw `Failed to update category with UUID ${uuid}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to update category with UUID ${uuid}: ${err}`
);
}
}
}
async deleteCategoryOfDomain(domain: string, path: string): Promise<void> {
const url = `${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`;
try {
const response: ApiResponse = await this.#apiClient.delete(
`${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`
);
const response: ApiResponse = await this.#apiClient.delete(url);
if (response.ok) {
return;
} else {
throw `Failed to update category ${path} of domain ${domain}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"delete",
`Failed to update category ${path} of domain ${domain}`,
url
);
}
} catch (err) {
throw `Failed to delete category ${path} of domain ${domain}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to delete category ${path} of domain ${domain}: ${err}`
);
}
}
}
async deleteCategoryWithId(categoryId: number): Promise<void> {
const url = `${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`;
try {
const response: ApiResponse = await this.#apiClient.delete(
`${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`
);
const response: ApiResponse = await this.#apiClient.delete(url);
if (response.ok) {
return;
} else {
throw `Failed to delete category with ID ${categoryId}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"delete",
`Failed to delete category with ID ${categoryId}`,
url
);
}
} catch (err) {
throw `Failed to delete category ${categoryId}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to delete category ${categoryId}: ${err}`
);
}
}
}
async deleteCategoryWithUuid(uuid: string): Promise<void> {
const url = `${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`;
try {
const response: ApiResponse = await this.#apiClient.delete(
`${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`
);
const response: ApiResponse = await this.#apiClient.delete(url);
if (response.ok) {
return;
} else {
throw `Failed to delete category with UUID ${uuid}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"delete",
`Failed to delete category with UUID ${uuid}`,
url
);
}
} catch (err) {
throw `Failed to delete category with UUID ${uuid}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to delete category with UUID ${uuid}: ${err}`
);
}
}
}
@ -239,11 +305,12 @@ export class CategorizationApiClient {
limit = 20,
offset = 0
): Promise<ListView<Category>> {
const url = `${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`;
try {
const response: ApiResponse = await this.#apiClient.get(
`${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`,
{ limit: limit.toString(), offset: offset.toString() }
);
const response: ApiResponse = await this.#apiClient.get(url, {
limit: limit.toString(),
offset: offset.toString(),
});
if (response.ok) {
const result: Record<
@ -265,12 +332,22 @@ export class CategorizationApiClient {
offset: result.offset as number,
};
} else {
throw `Failed to get subcategories of category ${path} of
domain ${domain}: ${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"get",
`Failed to get subcategories of category ${path} of domain ${domain}`,
url
);
}
} catch (err) {
throw `Failed to get subcategories of category ${path} of
domain ${domain}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to get subcategories of category ${path} of domain ${domain}: ${err}`
);
}
}
}
@ -279,14 +356,12 @@ export class CategorizationApiClient {
limit = 20,
offset = 0
): Promise<ListView<Category>> {
const url = `${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`;
try {
const response: ApiResponse = await this.#apiClient.get(
`${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`,
{
limit: limit.toString(),
offset: offset.toString(),
}
);
const response: ApiResponse = await this.#apiClient.get(url, {
limit: limit.toString(),
offset: offset.toString(),
});
if (response.ok) {
const result: Record<
@ -308,12 +383,22 @@ export class CategorizationApiClient {
offset: result.offset as number,
};
} else {
throw `Failed to get subcategories of category with ID
${categoryId}: ${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"get",
`Failed to get subcategories of category with ID ${categoryId}`,
url
);
}
} catch (err) {
throw `Failed to get subcategories of category with
ID ${categoryId}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to get subcategories of category with ID ${categoryId}: ${err}`
);
}
}
}
@ -322,14 +407,12 @@ export class CategorizationApiClient {
limit = 20,
offset = 0
): Promise<ListView<Category>> {
const url = `${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`;
try {
const response: ApiResponse = await this.#apiClient.get(
`${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`,
{
limit: limit.toString(),
offset: offset.toString(),
}
);
const response: ApiResponse = await this.#apiClient.get(url, {
limit: limit.toString(),
offset: offset.toString(),
});
if (response.ok) {
const result: Record<
@ -351,12 +434,22 @@ export class CategorizationApiClient {
offset: result.offset as number,
};
} else {
throw `Failed to get subcategories of category with UUID
${uuid}: ${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"get",
`Failed to get subcategories of category with UUID ${uuid}`,
url
);
}
} catch (err) {
throw `Failed to get subcategories of category with
UUID ${uuid}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to get subcategories of category with UUID ${uuid}: ${err}`
);
}
}
}
@ -375,9 +468,10 @@ export class CategorizationApiClient {
path: string,
category: Category
): Promise<string> {
const url = `${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`;
try {
const response: ApiResponse = await this.#apiClient.post(
`${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`,
url,
JSON.stringify(category)
);
@ -385,12 +479,22 @@ export class CategorizationApiClient {
const result: string = await response.text();
return result;
} else {
throw `Failed to add new subcategory to category ${path} of
domain ${domain}: ${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"post",
`Failed to add new subcategory to category ${path} of domain ${domain}`,
url
);
}
} catch (err) {
throw `Failed to add new subcategory to category ${path} of
domain ${domain}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to add new subcategory to category ${path} of domain ${domain}: ${err}`
);
}
}
}
@ -398,6 +502,7 @@ export class CategorizationApiClient {
categoryId: number,
category: Category
): Promise<string> {
const url = `${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`;
try {
const response: ApiResponse = await this.#apiClient.post(
`${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`,
@ -408,12 +513,22 @@ export class CategorizationApiClient {
const result: string = await response.text();
return result;
} else {
throw `Failed to add new subcategory to category with
ID ${categoryId}: ${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"post",
`Failed to add new subcategory to category with ID ${categoryId}`,
url
);
}
} catch (err) {
throw `Failed to add new subcategory to category with
ID ${categoryId}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to add new subcategory to category with ID ${categoryId}: ${err}`
);
}
}
}
@ -421,9 +536,10 @@ export class CategorizationApiClient {
uuid: string,
category: Category
): Promise<string> {
const url = `${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`;
try {
const response: ApiResponse = await this.#apiClient.post(
`${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`,
url,
JSON.stringify(category)
);
@ -431,12 +547,22 @@ export class CategorizationApiClient {
const result: string = await response.text();
return result;
} else {
throw `Failed to add new subcategory to category with
UUID ${uuid}: ${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"post",
`Failed to add new subcategory to category with UUID ${uuid}`,
url
);
}
} catch (err) {
throw `Failed to add new subcategory to category with
UUID ${uuid}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to add new subcategory to category with UUID ${uuid}: ${err}`
);
}
}
}