Use Error subclasses instead of throwing strings

Jens Pelzetter 2020-08-14 11:52:24 +02:00
parent 0c659aa9a2
commit 676156d790
1 changed files with 158 additions and 75 deletions

View File

@ -572,11 +572,12 @@ export class CategorizationApiClient {
limit = 20,
offset = 0
): Promise<ListView<Categorization>> {
const url = `${this.#CATEGORIES_API_PREFIX}/$domain/${path}/@objects`;
try {
const response: ApiResponse = await this.#apiClient.get(
`${this.#CATEGORIES_API_PREFIX}/$domain/${path}/@objects`,
{ 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<
@ -598,12 +599,22 @@ export class CategorizationApiClient {
offset: result.offset as number,
};
} else {
throw `Failed to get objects in category ${path} of
domain ${domain}: ${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"get",
`Failed to get objects in category ${path} of domain ${domain}`,
url
);
}
} catch (err) {
throw `Failed to get objects in category ${path} of
domain ${domain}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to get objects in category ${path} of domain ${domain}: ${err}`
);
}
}
}
@ -612,14 +623,12 @@ export class CategorizationApiClient {
limit = 20,
offset = 0
): Promise<ListView<Categorization>> {
const url = `${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}/@objects`;
try {
const response: ApiResponse = await this.#apiClient.get(
`${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}/@objects`,
{
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<
@ -641,12 +650,22 @@ export class CategorizationApiClient {
offset: result.offset as number,
};
} else {
throw `Failed to get objects in category with
ID ${categoryId}: ${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"get",
`Failed to get objects in category with ID ${categoryId}`,
url
);
}
} catch (err) {
throw `Failed to get objects in category with
Id ${categoryId}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to get objects in category with Id ${categoryId}: ${err}`
);
}
}
}
@ -655,14 +674,12 @@ export class CategorizationApiClient {
limit = 20,
offset = 0
): Promise<ListView<Categorization>> {
const url = `${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}/@objects`;
try {
const response: ApiResponse = await this.#apiClient.get(
`${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}/@objects`,
{
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<
@ -684,12 +701,22 @@ export class CategorizationApiClient {
offset: result.offset as number,
};
} else {
throw `Failed to get objects in category with
UUID ${uuid}: ${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"get",
`Failed to get objects in category with UUID ${uuid}`,
url
);
}
} catch (err) {
throw `Failed to get objects in category with
UUId ${uuid}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to get objects in category with UUId ${uuid}: ${err}`
);
}
}
}
@ -698,9 +725,10 @@ export class CategorizationApiClient {
path: string,
categorization: Categorization
): 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(categorization)
);
@ -708,12 +736,22 @@ export class CategorizationApiClient {
const result: string = await response.text();
return result;
} else {
throw `Failed to add object to category ${path} of
domain ${domain}: ${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"post",
`Failed to add object to category ${path} of domain ${domain}`,
url
);
}
} catch (err) {
throw `Failed to add object to category ${path} of
domain ${domain}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to add object to category ${path} of domain ${domain}: ${err}`
);
}
}
}
@ -721,9 +759,10 @@ export class CategorizationApiClient {
categoryId: number,
categorization: Categorization
): Promise<string> {
const url = `${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`;
try {
const response: ApiResponse = await this.#apiClient.post(
`${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`,
url,
JSON.stringify(categorization)
);
@ -731,12 +770,22 @@ export class CategorizationApiClient {
const result: string = await response.text();
return result;
} else {
throw `Failed to add object to category with
ID ${categoryId}: ${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"post",
`Failed to add object to category with ID ${categoryId}`,
url
);
}
} catch (err) {
throw `Failed to add object to category with
ID ${categoryId}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to add object to category with ID ${categoryId}: ${err}`
);
}
}
}
@ -744,9 +793,10 @@ export class CategorizationApiClient {
uuid: string,
categorization: Categorization
): 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(categorization)
);
@ -754,12 +804,22 @@ export class CategorizationApiClient {
const result: string = await response.text();
return result;
} else {
throw `Failed to add object to category with
UUID ${uuid}: ${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"post",
`Failed to add object to category with UUID ${uuid}`,
url
);
}
} catch (err) {
throw `Failed to add object to category with
UUID ${uuid}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to add object to category with UUID ${uuid}: ${err}`
);
}
}
}
@ -768,23 +828,30 @@ export class CategorizationApiClient {
path: string,
objectIdentifier: string
): Promise<void> {
const url = `${
this.#CATEGORIES_API_PREFIX
}/${domain}/${path}/@objects/${objectIdentifier}`;
try {
const response: ApiResponse = await this.#apiClient.delete(
`${
this.#CATEGORIES_API_PREFIX
}/${domain}/${path}/@objects/${objectIdentifier}`
);
const response: ApiResponse = await this.#apiClient.delete(url);
if (response.ok) {
return;
} else {
throw `Failed to remove object ${objectIdentifier} from
category ${path} of domain ${domain}:
${response.status} ${response.statusText}.`;
throw new ApiError(
response.status,
response.statusText,
"delete",
`Failed to remove object ${objectIdentifier} from category ${path} of domain ${domain}`,
url
);
}
} catch (err) {
throw `Failed to remove object ${objectIdentifier} from
category ${path} of domain ${domain}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(`Failed to remove object ${objectIdentifier} from
category ${path} of domain ${domain}: ${err}`);
}
}
}
@ -792,23 +859,31 @@ export class CategorizationApiClient {
categoryId: number,
objectIdentifier: string
): Promise<void> {
const url = `${
this.#CATEGORIES_API_PREFIX
}/ID-${categoryId}/@objects/${objectIdentifier}`;
try {
const response: ApiResponse = await this.#apiClient.delete(
`${
this.#CATEGORIES_API_PREFIX
}/ID-${categoryId}/@objects/${objectIdentifier}`
);
const response: ApiResponse = await this.#apiClient.delete(url);
if (response.ok) {
return;
} else {
throw `Failed to remove object ${objectIdentifier} from
category with ID ${categoryId}:
${response.status} ${response.statusText}.`;
throw new ApiError(
response.status,
response.statusText,
"delete",
`Failed to remove object ${objectIdentifier} from category with ID ${categoryId}`,
url
);
}
} catch (err) {
throw `Failed to remove object ${objectIdentifier} from
category with ID ${categoryId}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to remove object ${objectIdentifier} from category with ID ${categoryId}: ${err}`
);
}
}
}
@ -816,23 +891,31 @@ export class CategorizationApiClient {
uuid: string,
objectIdentifier: string
): Promise<void> {
const url = `${
this.#CATEGORIES_API_PREFIX
}/UUID-${uuid}/@objects/${objectIdentifier}`;
try {
const response: ApiResponse = await this.#apiClient.delete(
`${
this.#CATEGORIES_API_PREFIX
}/UUID-${uuid}/@objects/${objectIdentifier}`
);
const response: ApiResponse = await this.#apiClient.delete(url);
if (response.ok) {
return;
} else {
throw `Failed to remove object ${objectIdentifier} from
category with UUID ${uuid}:
${response.status} ${response.statusText}.`;
throw new ApiError(
response.status,
response.statusText,
"delete",
`Failed to remove object ${objectIdentifier} from category with UUID ${uuid}`,
url
);
}
} catch (err) {
throw `Failed to remove object ${objectIdentifier} from
category with UUID ${uuid}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to remove object ${objectIdentifier} from category with UUID ${uuid}: ${err}`
);
}
}
}
}