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, limit = 20,
offset = 0 offset = 0
): Promise<ListView<Categorization>> { ): Promise<ListView<Categorization>> {
const url = `${this.#CATEGORIES_API_PREFIX}/$domain/${path}/@objects`;
try { try {
const response: ApiResponse = await this.#apiClient.get( const response: ApiResponse = await this.#apiClient.get(url, {
`${this.#CATEGORIES_API_PREFIX}/$domain/${path}/@objects`, limit: limit.toString(),
{ limit: limit.toString(), offset: offset.toString() } offset: offset.toString(),
); });
if (response.ok) { if (response.ok) {
const result: Record< const result: Record<
@ -598,12 +599,22 @@ export class CategorizationApiClient {
offset: result.offset as number, offset: result.offset as number,
}; };
} else { } else {
throw `Failed to get objects in category ${path} of throw new ApiError(
domain ${domain}: ${response.status} ${response.statusText}`; response.status,
response.statusText,
"get",
`Failed to get objects in category ${path} of domain ${domain}`,
url
);
} }
} catch (err) { } catch (err) {
throw `Failed to get objects in category ${path} of if (err instanceof ApiError) {
domain ${domain}: ${err}`; 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, limit = 20,
offset = 0 offset = 0
): Promise<ListView<Categorization>> { ): Promise<ListView<Categorization>> {
const url = `${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}/@objects`;
try { try {
const response: ApiResponse = await this.#apiClient.get( const response: ApiResponse = await this.#apiClient.get(url, {
`${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}/@objects`, limit: limit.toString(),
{ offset: offset.toString(),
limit: limit.toString(), });
offset: offset.toString(),
}
);
if (response.ok) { if (response.ok) {
const result: Record< const result: Record<
@ -641,12 +650,22 @@ export class CategorizationApiClient {
offset: result.offset as number, offset: result.offset as number,
}; };
} else { } else {
throw `Failed to get objects in category with throw new ApiError(
ID ${categoryId}: ${response.status} ${response.statusText}`; response.status,
response.statusText,
"get",
`Failed to get objects in category with ID ${categoryId}`,
url
);
} }
} catch (err) { } catch (err) {
throw `Failed to get objects in category with if (err instanceof ApiError) {
Id ${categoryId}: ${err}`; 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, limit = 20,
offset = 0 offset = 0
): Promise<ListView<Categorization>> { ): Promise<ListView<Categorization>> {
const url = `${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}/@objects`;
try { try {
const response: ApiResponse = await this.#apiClient.get( const response: ApiResponse = await this.#apiClient.get(url, {
`${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}/@objects`, limit: limit.toString(),
{ offset: offset.toString(),
limit: limit.toString(), });
offset: offset.toString(),
}
);
if (response.ok) { if (response.ok) {
const result: Record< const result: Record<
@ -684,12 +701,22 @@ export class CategorizationApiClient {
offset: result.offset as number, offset: result.offset as number,
}; };
} else { } else {
throw `Failed to get objects in category with throw new ApiError(
UUID ${uuid}: ${response.status} ${response.statusText}`; response.status,
response.statusText,
"get",
`Failed to get objects in category with UUID ${uuid}`,
url
);
} }
} catch (err) { } catch (err) {
throw `Failed to get objects in category with if (err instanceof ApiError) {
UUId ${uuid}: ${err}`; 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, path: string,
categorization: Categorization categorization: Categorization
): 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(categorization) JSON.stringify(categorization)
); );
@ -708,12 +736,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 object to category ${path} of throw new ApiError(
domain ${domain}: ${response.status} ${response.statusText}`; response.status,
response.statusText,
"post",
`Failed to add object to category ${path} of domain ${domain}`,
url
);
} }
} catch (err) { } catch (err) {
throw `Failed to add object to category ${path} of if (err instanceof ApiError) {
domain ${domain}: ${err}`; 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, categoryId: number,
categorization: Categorization categorization: Categorization
): 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}`, url,
JSON.stringify(categorization) JSON.stringify(categorization)
); );
@ -731,12 +770,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 object to category with throw new ApiError(
ID ${categoryId}: ${response.status} ${response.statusText}`; response.status,
response.statusText,
"post",
`Failed to add object to category with ID ${categoryId}`,
url
);
} }
} catch (err) { } catch (err) {
throw `Failed to add object to category with if (err instanceof ApiError) {
ID ${categoryId}: ${err}`; 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, uuid: string,
categorization: Categorization categorization: Categorization
): 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(categorization) JSON.stringify(categorization)
); );
@ -754,12 +804,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 object to category with throw new ApiError(
UUID ${uuid}: ${response.status} ${response.statusText}`; response.status,
response.statusText,
"post",
`Failed to add object to category with UUID ${uuid}`,
url
);
} }
} catch (err) { } catch (err) {
throw `Failed to add object to category with if (err instanceof ApiError) {
UUID ${uuid}: ${err}`; 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, path: string,
objectIdentifier: string objectIdentifier: string
): Promise<void> { ): Promise<void> {
const url = `${
this.#CATEGORIES_API_PREFIX
}/${domain}/${path}/@objects/${objectIdentifier}`;
try { try {
const response: ApiResponse = await this.#apiClient.delete( const response: ApiResponse = await this.#apiClient.delete(url);
`${
this.#CATEGORIES_API_PREFIX
}/${domain}/${path}/@objects/${objectIdentifier}`
);
if (response.ok) { if (response.ok) {
return; return;
} else { } else {
throw `Failed to remove object ${objectIdentifier} from throw new ApiError(
category ${path} of domain ${domain}: response.status,
${response.status} ${response.statusText}.`; response.statusText,
"delete",
`Failed to remove object ${objectIdentifier} from category ${path} of domain ${domain}`,
url
);
} }
} catch (err) { } catch (err) {
throw `Failed to remove object ${objectIdentifier} from if (err instanceof ApiError) {
category ${path} of domain ${domain}: ${err}`; 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, categoryId: number,
objectIdentifier: string objectIdentifier: string
): Promise<void> { ): Promise<void> {
const url = `${
this.#CATEGORIES_API_PREFIX
}/ID-${categoryId}/@objects/${objectIdentifier}`;
try { try {
const response: ApiResponse = await this.#apiClient.delete( const response: ApiResponse = await this.#apiClient.delete(url);
`${
this.#CATEGORIES_API_PREFIX
}/ID-${categoryId}/@objects/${objectIdentifier}`
);
if (response.ok) { if (response.ok) {
return; return;
} else { } else {
throw `Failed to remove object ${objectIdentifier} from throw new ApiError(
category with ID ${categoryId}: response.status,
${response.status} ${response.statusText}.`; response.statusText,
"delete",
`Failed to remove object ${objectIdentifier} from category with ID ${categoryId}`,
url
);
} }
} catch (err) { } catch (err) {
throw `Failed to remove object ${objectIdentifier} from if (err instanceof ApiError) {
category with ID ${categoryId}: ${err}`; 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, uuid: string,
objectIdentifier: string objectIdentifier: string
): Promise<void> { ): Promise<void> {
const url = `${
this.#CATEGORIES_API_PREFIX
}/UUID-${uuid}/@objects/${objectIdentifier}`;
try { try {
const response: ApiResponse = await this.#apiClient.delete( const response: ApiResponse = await this.#apiClient.delete(url);
`${
this.#CATEGORIES_API_PREFIX
}/UUID-${uuid}/@objects/${objectIdentifier}`
);
if (response.ok) { if (response.ok) {
return; return;
} else { } else {
throw `Failed to remove object ${objectIdentifier} from throw new ApiError(
category with UUID ${uuid}: response.status,
${response.status} ${response.statusText}.`; response.statusText,
"delete",
`Failed to remove object ${objectIdentifier} from category with UUID ${uuid}`,
url
);
} }
} catch (err) { } catch (err) {
throw `Failed to remove object ${objectIdentifier} from if (err instanceof ApiError) {
category with UUID ${uuid}: ${err}`; throw err;
} else {
throw new ApiClientError(
`Failed to remove object ${objectIdentifier} from category with UUID ${uuid}: ${err}`
);
}
} }
} }
} }