Use Error subclasses instead of throwing strings

Jens Pelzetter 2020-08-14 16:42:39 +02:00
parent 676156d790
commit 6510eb65cb
2 changed files with 257 additions and 110 deletions

View File

@ -1,4 +1,6 @@
import {
ApiClientError,
ApiError,
ApiResponse,
LibreCcmApiClient,
} from "@libreccm/ccm-apiclient-commons";
@ -20,10 +22,9 @@ export class ImExportClient {
}
async listImports(): Promise<ImportManifest[]> {
const url = `${this.#IMEXPORT_API_PREFIX}/imports`;
try {
const response: ApiResponse = await this.#apiClient.get(
`${this.#IMEXPORT_API_PREFIX}/imports`
);
const response: ApiResponse = await this.#apiClient.get(url);
if (response.ok) {
const records: Record<
@ -35,28 +36,49 @@ export class ImExportClient {
buildImportManifestFromRecord(record)
);
} else {
throw `Failed to list available import archives:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"get",
`Failed to list available import archives`,
url
);
}
} catch (err) {
throw `Failed to list available import archives: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to list available import archives: ${err}`
);
}
}
}
async importArchive(importName: string): Promise<void> {
const url = `${this.#IMEXPORT_API_PREFIX}/imports/${importName}`;
try {
const response: ApiResponse = await this.#apiClient.post(
`${this.#IMEXPORT_API_PREFIX}/imports/${importName}`
);
const response: ApiResponse = await this.#apiClient.post(url);
if (response.ok) {
return;
} else {
throw `Failed to import archive ${importName}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"post",
`Failed to import archive ${importName}`,
url
);
}
} catch (err) {
throw `Failed to import archive ${importName}: ${err}`;
if (err instanceof ApiClientError) {
throw err;
} else {
throw new ApiClientError(
`Failed to import archive ${importName}: ${err}`
);
}
}
}
@ -64,22 +86,36 @@ export class ImExportClient {
exportName: string,
entityIds: string[]
): Promise<void> {
const url = `${this.#IMEXPORT_API_PREFIX}/exports/${exportName}`;
try {
const response: ApiResponse = await this.#apiClient.post(
`${this.#IMEXPORT_API_PREFIX}/exports/${exportName}`,
url,
JSON.stringify(entityIds)
);
if (response.ok) {
return;
} else {
throw `Failed to export entities
${entityIds.join(", ")} as ${exportName}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"post",
`Failed to export entities ${entityIds.join(
", "
)} as ${exportName}`,
url
);
}
} catch (err) {
throw `Failed to export entities
${entityIds.join(", ")} as ${exportName}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to export entities ${entityIds.join(
", "
)} as ${exportName}: ${err}`
);
}
}
}
}

View File

@ -2,6 +2,8 @@ import {
ApiResponse,
LibreCcmApiClient,
ListView,
ApiError,
ApiClientError,
} from "@libreccm/ccm-apiclient-commons";
import { buildIdentifierParam } from "@libreccm/ccm-apiclient-commons";
@ -31,14 +33,12 @@ export class RolesApiClient {
}
async getRoles(limit: number, offset: number): Promise<ListView<Role>> {
const url = this.#ROLES_API_PREFIX;
try {
const response: ApiResponse = await this.#apiClient.get(
this.#ROLES_API_PREFIX,
{
const response: ApiResponse = await this.#apiClient.get(url, {
limit: limit.toString(),
offset: offset.toString(),
}
);
});
if (response.ok) {
const result: Record<
string,
@ -59,35 +59,52 @@ export class RolesApiClient {
offset: result.offset as number,
};
} else {
throw `Failed to get roles:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"get",
`Failed to get roles`,
url
);
}
} catch (err) {
throw `Failed to get roles: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(`Failed to get roles: ${err}`);
}
}
}
async getRole(roleIdentifier: string): Promise<Role> {
try {
const roleParam: string = buildIdentifierParam(roleIdentifier);
const response: ApiResponse = await this.#apiClient.get(
`${this.#ROLES_API_PREFIX}/${roleParam}`
);
const url = `${this.#ROLES_API_PREFIX}/${roleParam}`;
try {
const response: ApiResponse = await this.#apiClient.get(url);
if (response.ok) {
return buildRoleFromRecord(
(await response.json()) as Record<string, unknown>
);
} else {
throw `Failed to get role ${roleIdentifier}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"get",
`Failed to get role ${roleIdentifier}`,
url
);
}
} catch (err) {
throw `Failed to get role: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(`Failed to get role: ${err}`);
}
}
}
async addRole(role: Role): Promise<string> {
const url = `${this.#ROLES_API_PREFIX}`;
try {
const response: ApiResponse = await this.#apiClient.post(
`${this.#ROLES_API_PREFIX}`,
@ -96,48 +113,81 @@ export class RolesApiClient {
if (response.ok) {
return role.name;
} else {
throw `Failed to add role ${role.name}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"post",
`Failed to add role ${role.name}`,
url
);
}
} catch (err) {
throw `Failed to add role ${role.name}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to add role ${role.name}: ${err}`
);
}
}
}
async updateRole(roleIdentifier: string, role: Role): Promise<void> {
const url = `${this.#ROLES_API_PREFIX}/${buildIdentifierParam(
roleIdentifier
)}`;
try {
const response: ApiResponse = await this.#apiClient.put(
`${this.#ROLES_API_PREFIX}/${buildIdentifierParam(
roleIdentifier
)}`,
url,
JSON.stringify(role)
);
if (response.ok) {
return;
} else {
throw `Failed to update role ${roleIdentifier}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"put",
`Failed to update role ${roleIdentifier}`,
url
);
}
} catch (err) {
throw `Failed to update role ${roleIdentifier}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to update role ${roleIdentifier}: ${err}`
);
}
}
}
async deleteRole(roleIdentifier: string): Promise<void> {
try {
const response: ApiResponse = await this.#apiClient.delete(
`${this.#ROLES_API_PREFIX}/${buildIdentifierParam(
const url = `${this.#ROLES_API_PREFIX}/${buildIdentifierParam(
roleIdentifier
)}`
);
)}`;
try {
const response: ApiResponse = await this.#apiClient.delete(url);
if (response.ok) {
return;
} else {
throw `Failed to delete role ${name}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"delete",
`Failed to delete role ${name}`,
url
);
}
} catch (err) {
throw `Failed to delete role ${name}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to delete role ${name}: ${err}`
);
}
}
}
@ -146,16 +196,14 @@ export class RolesApiClient {
limit: number,
offset: number
): Promise<ListView<RolePartyMembership>> {
const url = `${this.#ROLES_API_PREFIX}/${buildIdentifierParam(
roleIdentifier
)}/members`;
try {
const roleParam: string = buildIdentifierParam(roleIdentifier);
const response: ApiResponse = await this.#apiClient.get(
`${this.#ROLES_API_PREFIX}/${roleParam}/members`,
{
const response: ApiResponse = await this.#apiClient.get(url, {
limit: limit.toString(),
offset: offset.toString(),
}
);
});
if (response.ok) {
const result: Record<
@ -177,11 +225,22 @@ export class RolesApiClient {
offset: result.offset as number,
};
} else {
throw `Failed to get members of role ${roleIdentifier}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"get",
`Failed to get members of role ${roleIdentifier}`,
url
);
}
} catch (err) {
throw `Failed to get members of role ${roleIdentifier}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to get members of role ${roleIdentifier}: ${err}`
);
}
}
}
@ -189,22 +248,32 @@ export class RolesApiClient {
roleIdentifier: string | number,
userIdentifier: string | number
): Promise<void> {
try {
const roleParam = buildIdentifierParam(roleIdentifier);
const memberParam = buildIdentifierParam(userIdentifier);
const response: ApiResponse = await this.#apiClient.put(
`${this.#ROLES_API_PREFIX}/${roleParam}/members/${memberParam}`
);
const url = `${
this.#ROLES_API_PREFIX
}/${roleParam}/members/${memberParam}`;
try {
const response: ApiResponse = await this.#apiClient.put(url);
if (response.ok) {
return;
} else {
throw `Failed to add user ${userIdentifier} to
role ${roleIdentifier}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"put",
`Failed to add user ${userIdentifier} to role ${roleIdentifier}`,
url
);
}
} catch (err) {
throw `Failed to add user ${userIdentifier} to
role ${roleIdentifier}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to add user ${userIdentifier} to role ${roleIdentifier}: ${err}`
);
}
}
}
@ -212,22 +281,32 @@ export class RolesApiClient {
roleIdentifier: string | number,
userIdentifier: string | number
): Promise<void> {
try {
const roleParam = buildIdentifierParam(roleIdentifier);
const memberParam = buildIdentifierParam(userIdentifier);
const response: ApiResponse = await this.#apiClient.delete(
`${this.#ROLES_API_PREFIX}/${roleParam}/members/${memberParam}`
);
const url = `${
this.#ROLES_API_PREFIX
}/${roleParam}/members/${memberParam}`;
try {
const response: ApiResponse = await this.#apiClient.delete(url);
if (response.ok) {
return;
} else {
throw `Failed to remove user ${userIdentifier} from
role ${roleIdentifier}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"delete",
`Failed to remove user ${userIdentifier} from role ${roleIdentifier}`,
url
);
}
} catch (err) {
throw `Failed to remote user ${userIdentifier} from
role ${roleIdentifier}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to remote user ${userIdentifier} from role ${roleIdentifier}: ${err}`
);
}
}
}
@ -236,16 +315,14 @@ export class RolesApiClient {
limit: number,
offset: number
): Promise<ListView<RolePermission>> {
try {
const roleParam: string = buildIdentifierParam(roleIdentifier);
const response: ApiResponse = await this.#apiClient.get(
`${this.#ROLES_API_PREFIX}/${roleParam}/permissions`,
{
const roleParam: string = buildIdentifierParam(roleIdentifier);
const url = `${this.#ROLES_API_PREFIX}/${roleParam}/permissions`;
try {
const response: ApiResponse = await this.#apiClient.get(url, {
limit: limit.toString(),
offset: offset.toString(),
}
);
});
if (response.ok) {
const result: Record<
string,
@ -266,11 +343,22 @@ export class RolesApiClient {
offset: result.offset as number,
};
} else {
throw `Failed to get permissions of role ${roleIdentifier}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"get",
`Failed to get permissions of role ${roleIdentifier}`,
url
);
}
} catch (err) {
throw `Failed to get permissions of role ${roleIdentifier}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to get permissions of role ${roleIdentifier}: ${err}`
);
}
}
}
@ -278,21 +366,32 @@ export class RolesApiClient {
roleIdentifier: string,
permission: RolePermission
): Promise<void> {
try {
const roleParam: string = buildIdentifierParam(roleIdentifier);
const url = `${this.#ROLES_API_PREFIX}/${roleParam}/permissions`;
try {
const response: ApiResponse = await this.#apiClient.post(
`${this.#ROLES_API_PREFIX}/${roleParam}/permissions`,
url,
JSON.stringify(permission)
);
if (response.ok) {
return;
} else {
throw `Failed to add permission to role ${roleIdentifier}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"post",
`Failed to add permission to role ${roleIdentifier}`,
url
);
}
} catch (err) {
throw `Failed to add permission to role ${roleIdentifier}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to add permission to role ${roleIdentifier}: ${err}`
);
}
}
}
@ -300,9 +399,12 @@ export class RolesApiClient {
roleIdentifier: string | number,
permissionIdentifier: string | number
): Promise<void> {
try {
const roleParam = buildIdentifierParam(roleIdentifier);
const permissionParam = buildIdentifierParam(permissionIdentifier);
const url = `${
this.#ROLES_API_PREFIX
}/${roleParam}/permissions/${permissionParam}`;
try {
const response: ApiResponse = await this.#apiClient.delete(
`${
this.#ROLES_API_PREFIX
@ -311,13 +413,22 @@ export class RolesApiClient {
if (response.ok) {
return;
} else {
throw `Failed to remove permission ${permissionIdentifier} from
role ${roleIdentifier}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"delete",
`Failed to remove permission ${permissionIdentifier} from role ${roleIdentifier}`,
url
);
}
} catch (err) {
throw `Failed to remote permission ${permissionIdentifier} from
role ${roleIdentifier}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to remote permission ${permissionIdentifier} from role ${roleIdentifier}: ${err}`
);
}
}
}
}