Use Error subclasses instead of throwing strings

Former-commit-id: 0c659aa9a2
restapi
Jens Pelzetter 2020-08-14 07:14:48 +02:00
parent 6ecf9993c7
commit 7e9242c005
1 changed files with 200 additions and 74 deletions

View File

@ -123,20 +123,32 @@ export class CategorizationApiClient {
path: string, path: string,
category: Category category: Category
): Promise<void> { ): Promise<void> {
const url = `${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`;
try { try {
const response: ApiResponse = await this.#apiClient.put( const response: ApiResponse = await this.#apiClient.put(
`${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`, url,
JSON.stringify(category) JSON.stringify(category)
); );
if (response.ok) { if (response.ok) {
return; return;
} else { } else {
throw `Failed to update category ${path} of domain ${domain}: throw new ApiError(
${response.status} ${response.statusText}`; response.status,
response.statusText,
"put",
`Failed to update category ${path} of domain ${domain}`,
url
);
} }
} catch (err) { } 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, categoryId: number,
category: Category category: Category
): Promise<void> { ): Promise<void> {
const url = `${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`;
try { try {
const response: ApiResponse = await this.#apiClient.put( const response: ApiResponse = await this.#apiClient.put(
`${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`, url,
JSON.stringify(category) JSON.stringify(category)
); );
if (response.ok) { if (response.ok) {
return; return;
} else { } else {
throw `Failed to update category with ID ${categoryId}: throw new ApiError(
${response.status} ${response.statusText}`; response.status,
response.statusText,
"put",
`Failed to update category with ID ${categoryId}`,
url
);
} }
} catch (err) { } 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 category: Category
): Promise<void> { ): Promise<void> {
try { try {
const url = `${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`;
const response: ApiResponse = await this.#apiClient.put( const response: ApiResponse = await this.#apiClient.put(
`${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`, url,
JSON.stringify(category) JSON.stringify(category)
); );
if (response.ok) { if (response.ok) {
return; return;
} else { } else {
throw `Failed to update category with UUID ${uuid}: throw new ApiError(
${response.status} ${response.statusText}`; response.status,
response.statusText,
"put",
`Failed to update category with UUID ${uuid}`,
url
);
} }
} catch (err) { } 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> { async deleteCategoryOfDomain(domain: string, path: string): Promise<void> {
const url = `${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`;
try { try {
const response: ApiResponse = await this.#apiClient.delete( const response: ApiResponse = await this.#apiClient.delete(url);
`${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`
);
if (response.ok) { if (response.ok) {
return; return;
} else { } else {
throw `Failed to update category ${path} of domain ${domain}: throw new ApiError(
${response.status} ${response.statusText}`; response.status,
response.statusText,
"delete",
`Failed to update category ${path} of domain ${domain}`,
url
);
} }
} catch (err) { } 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> { async deleteCategoryWithId(categoryId: number): Promise<void> {
const url = `${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`;
try { try {
const response: ApiResponse = await this.#apiClient.delete( const response: ApiResponse = await this.#apiClient.delete(url);
`${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`
);
if (response.ok) { if (response.ok) {
return; return;
} else { } else {
throw `Failed to delete category with ID ${categoryId}: throw new ApiError(
${response.status} ${response.statusText}`; response.status,
response.statusText,
"delete",
`Failed to delete category with ID ${categoryId}`,
url
);
} }
} catch (err) { } 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> { async deleteCategoryWithUuid(uuid: string): Promise<void> {
const url = `${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`;
try { try {
const response: ApiResponse = await this.#apiClient.delete( const response: ApiResponse = await this.#apiClient.delete(url);
`${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`
);
if (response.ok) { if (response.ok) {
return; return;
} else { } else {
throw `Failed to delete category with UUID ${uuid}: throw new ApiError(
${response.status} ${response.statusText}`; response.status,
response.statusText,
"delete",
`Failed to delete category with UUID ${uuid}`,
url
);
} }
} catch (err) { } 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, limit = 20,
offset = 0 offset = 0
): Promise<ListView<Category>> { ): Promise<ListView<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}`, limit: limit.toString(),
{ limit: limit.toString(), offset: offset.toString() } offset: offset.toString(),
); });
if (response.ok) { if (response.ok) {
const result: Record< const result: Record<
@ -265,12 +332,22 @@ export class CategorizationApiClient {
offset: result.offset as number, offset: result.offset as number,
}; };
} else { } else {
throw `Failed to get subcategories of category ${path} of throw new ApiError(
domain ${domain}: ${response.status} ${response.statusText}`; response.status,
response.statusText,
"get",
`Failed to get subcategories of category ${path} of domain ${domain}`,
url
);
} }
} catch (err) { } catch (err) {
throw `Failed to get subcategories of category ${path} of if (err instanceof ApiError) {
domain ${domain}: ${err}`; 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, limit = 20,
offset = 0 offset = 0
): Promise<ListView<Category>> { ): Promise<ListView<Category>> {
const url = `${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`;
try { try {
const response: ApiResponse = await this.#apiClient.get( const response: ApiResponse = await this.#apiClient.get(url, {
`${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`,
{
limit: limit.toString(), limit: limit.toString(),
offset: offset.toString(), offset: offset.toString(),
} });
);
if (response.ok) { if (response.ok) {
const result: Record< const result: Record<
@ -308,12 +383,22 @@ export class CategorizationApiClient {
offset: result.offset as number, offset: result.offset as number,
}; };
} else { } else {
throw `Failed to get subcategories of category with ID throw new ApiError(
${categoryId}: ${response.status} ${response.statusText}`; response.status,
response.statusText,
"get",
`Failed to get subcategories of category with ID ${categoryId}`,
url
);
} }
} catch (err) { } catch (err) {
throw `Failed to get subcategories of category with if (err instanceof ApiError) {
ID ${categoryId}: ${err}`; 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, limit = 20,
offset = 0 offset = 0
): Promise<ListView<Category>> { ): Promise<ListView<Category>> {
const url = `${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`;
try { try {
const response: ApiResponse = await this.#apiClient.get( const response: ApiResponse = await this.#apiClient.get(url, {
`${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`,
{
limit: limit.toString(), limit: limit.toString(),
offset: offset.toString(), offset: offset.toString(),
} });
);
if (response.ok) { if (response.ok) {
const result: Record< const result: Record<
@ -351,12 +434,22 @@ export class CategorizationApiClient {
offset: result.offset as number, offset: result.offset as number,
}; };
} else { } else {
throw `Failed to get subcategories of category with UUID throw new ApiError(
${uuid}: ${response.status} ${response.statusText}`; response.status,
response.statusText,
"get",
`Failed to get subcategories of category with UUID ${uuid}`,
url
);
} }
} catch (err) { } catch (err) {
throw `Failed to get subcategories of category with if (err instanceof ApiError) {
UUID ${uuid}: ${err}`; 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, path: string,
category: Category category: Category
): Promise<string> { ): Promise<string> {
const url = `${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`;
try { try {
const response: ApiResponse = await this.#apiClient.post( const response: ApiResponse = await this.#apiClient.post(
`${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`, url,
JSON.stringify(category) JSON.stringify(category)
); );
@ -385,12 +479,22 @@ export class CategorizationApiClient {
const result: string = await response.text(); const result: string = await response.text();
return result; return result;
} else { } else {
throw `Failed to add new subcategory to category ${path} of throw new ApiError(
domain ${domain}: ${response.status} ${response.statusText}`; response.status,
response.statusText,
"post",
`Failed to add new subcategory to category ${path} of domain ${domain}`,
url
);
} }
} catch (err) { } catch (err) {
throw `Failed to add new subcategory to category ${path} of if (err instanceof ApiError) {
domain ${domain}: ${err}`; 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, categoryId: number,
category: Category category: Category
): Promise<string> { ): Promise<string> {
const url = `${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`;
try { try {
const response: ApiResponse = await this.#apiClient.post( const response: ApiResponse = await this.#apiClient.post(
`${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`, `${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`,
@ -408,12 +513,22 @@ export class CategorizationApiClient {
const result: string = await response.text(); const result: string = await response.text();
return result; return result;
} else { } else {
throw `Failed to add new subcategory to category with throw new ApiError(
ID ${categoryId}: ${response.status} ${response.statusText}`; response.status,
response.statusText,
"post",
`Failed to add new subcategory to category with ID ${categoryId}`,
url
);
} }
} catch (err) { } catch (err) {
throw `Failed to add new subcategory to category with if (err instanceof ApiError) {
ID ${categoryId}: ${err}`; 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, uuid: string,
category: Category category: Category
): Promise<string> { ): Promise<string> {
const url = `${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`;
try { try {
const response: ApiResponse = await this.#apiClient.post( const response: ApiResponse = await this.#apiClient.post(
`${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`, url,
JSON.stringify(category) JSON.stringify(category)
); );
@ -431,12 +547,22 @@ export class CategorizationApiClient {
const result: string = await response.text(); const result: string = await response.text();
return result; return result;
} else { } else {
throw `Failed to add new subcategory to category with throw new ApiError(
UUID ${uuid}: ${response.status} ${response.statusText}`; response.status,
response.statusText,
"post",
`Failed to add new subcategory to category with UUID ${uuid}`,
url
);
} }
} catch (err) { } catch (err) {
throw `Failed to add new subcategory to category with if (err instanceof ApiError) {
UUID ${uuid}: ${err}`; throw err;
} else {
throw new ApiClientError(
`Failed to add new subcategory to category with UUID ${uuid}: ${err}`
);
}
} }
} }