Use Error subclasses instead of throwing strings

Former-commit-id: 70568a65ff
restapi
Jens Pelzetter 2020-08-14 17:06:42 +02:00
parent 734ca7623d
commit 4452cb071d
1 changed files with 205 additions and 98 deletions

View File

@ -2,6 +2,8 @@ import {
ApiResponse,
LibreCcmApiClient,
ListView,
ApiError,
ApiClientError,
} from "@libreccm/ccm-apiclient-commons";
import { buildIdentifierParam } from "@libreccm/ccm-apiclient-commons";
@ -32,14 +34,12 @@ export class GroupsApiClient {
}
async getGroups(limit: number, offset: number): Promise<ListView<Group>> {
const url = `${this.#GROUPS_API_PREFIX}`;
try {
const response: ApiResponse = await this.#apiClient.get(
`${this.#GROUPS_API_PREFIX}`,
{
const response: ApiResponse = await this.#apiClient.get(url, {
limit: limit.toString(),
offset: offset.toString(),
}
);
});
if (response.ok) {
const result: Record<
string,
@ -60,86 +60,139 @@ export class GroupsApiClient {
offset: result.offset as number,
};
} else {
throw `Failed to get groups:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"get",
`Failed to get groups`,
url
);
}
} catch (err) {
throw `Failed to get groups: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(`Failed to get groups: ${err}`);
}
}
}
async getGroup(groupIdentifier: string | number): Promise<Group> {
try {
const response: ApiResponse = await this.#apiClient.get(
`${this.#GROUPS_API_PREFIX}/${buildIdentifierParam(
const url = `${this.#GROUPS_API_PREFIX}/${buildIdentifierParam(
groupIdentifier
)}`
);
)}`;
try {
const response: ApiResponse = await this.#apiClient.get(url);
if (response.ok) {
return buildGroupFromRecord(
(await response.json()) as Record<string, unknown>
);
} else {
throw `Failed to get group ${groupIdentifier}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"get",
`Failed to get group ${groupIdentifier}`,
url
);
}
} catch (err) {
throw `Failed to get group ${groupIdentifier}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to get group ${groupIdentifier}: ${err}`
);
}
}
}
async addGroup(group: Group): Promise<string> {
const url = `${this.#GROUPS_API_PREFIX}`;
try {
const response: ApiResponse = await this.#apiClient.post(
`${this.#GROUPS_API_PREFIX}`,
url,
JSON.stringify(group)
);
if (response.ok) {
return group.name;
} else {
throw `Failed to add group ${group.name}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"post",
`Failed to add group ${group.name}`,
url
);
}
} catch (err) {
throw `Failed to add group ${group.name}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to add group ${group.name}: ${err}`
);
}
}
}
async updateGroup(groupIdentifier: string, group: Group): Promise<void> {
const url = `${this.#GROUPS_API_PREFIX}/${buildIdentifierParam(
groupIdentifier
)}`;
try {
const response: ApiResponse = await this.#apiClient.put(
`${this.#GROUPS_API_PREFIX}/${buildIdentifierParam(
groupIdentifier
)}`,
url,
JSON.stringify(group)
);
if (response.ok) {
return;
} else {
throw `Failed to update group ${groupIdentifier}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"put",
`Failed to update group ${groupIdentifier}`,
url
);
}
} catch (err) {
throw `Failed to update group ${groupIdentifier}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to update group ${groupIdentifier}: ${err}`
);
}
}
}
async deleteGroup(groupIdentifier: string): Promise<void> {
try {
const response: ApiResponse = await this.#apiClient.delete(
`${this.#GROUPS_API_PREFIX}/${buildIdentifierParam(
const url = `${this.#GROUPS_API_PREFIX}/${buildIdentifierParam(
groupIdentifier
)}`
);
)}`;
try {
const response: ApiResponse = await this.#apiClient.delete(url);
if (response.ok) {
return;
} else {
throw `Failed to delete group ${name}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"delete",
`Failed to delete group ${name}`,
url
);
}
} catch (err) {
throw `Failed to delete group ${name}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to delete group ${name}: ${err}`
);
}
}
}
@ -148,16 +201,13 @@ export class GroupsApiClient {
limit: number,
offset: number
): Promise<ListView<GroupUserMembership>> {
try {
const groupParam: string = buildIdentifierParam(groupIdentifier);
const response: ApiResponse = await this.#apiClient.get(
`${this.#GROUPS_API_PREFIX}/${groupParam}/members`,
{
const url = `${this.#GROUPS_API_PREFIX}/${groupParam}/members`;
try {
const response: ApiResponse = await this.#apiClient.get(url, {
limit: limit.toString(),
offset: offset.toString(),
}
);
});
if (response.ok) {
const result: Record<
string,
@ -178,11 +228,22 @@ export class GroupsApiClient {
offset: result.offset as number,
};
} else {
throw `Failed to get members of group ${groupIdentifier}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"get",
`Failed to get members of group ${groupIdentifier}`,
url
);
}
} catch (err) {
throw `Failed to get members of group ${groupIdentifier}: ${err}`;
if (err instanceof err) {
throw err;
} else {
throw new ApiClientError(
`Failed to get members of group ${groupIdentifier}: ${err}`
);
}
}
}
@ -190,24 +251,32 @@ export class GroupsApiClient {
groupIdentifier: string | number,
userIdentifier: string | number
): Promise<void> {
try {
const groupParam = buildIdentifierParam(groupIdentifier);
const memberParam = buildIdentifierParam(userIdentifier);
const response: ApiResponse = await this.#apiClient.put(
`${
const url = `${
this.#GROUPS_API_PREFIX
}/${groupParam}/members/${memberParam}`
);
}/${groupParam}/members/${memberParam}`;
try {
const response: ApiResponse = await this.#apiClient.put(url);
if (response.ok) {
return;
} else {
throw `Failed to add user ${userIdentifier} to
group ${groupIdentifier}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"put",
`Failed to add user ${userIdentifier} to group ${groupIdentifier}`,
url
);
}
} catch (err) {
throw `Failed to add user ${userIdentifier} to
group ${groupIdentifier}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to add user ${userIdentifier} to group ${groupIdentifier}: ${err}`
);
}
}
}
@ -215,24 +284,32 @@ export class GroupsApiClient {
groupIdentifier: string | number,
userIdentifier: string | number
): Promise<void> {
try {
const groupParam = buildIdentifierParam(groupIdentifier);
const memberParam = buildIdentifierParam(userIdentifier);
const response: ApiResponse = await this.#apiClient.delete(
`${
const url = `${
this.#GROUPS_API_PREFIX
}/${groupParam}/members/${memberParam}`
);
}/${groupParam}/members/${memberParam}`;
try {
const response: ApiResponse = await this.#apiClient.delete(url);
if (response.ok) {
return;
} else {
throw `Failed to remove user ${userIdentifier} from
group ${groupIdentifier}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"delete",
`Failed to remove user ${userIdentifier} from group ${groupIdentifier}`,
url
);
}
} catch (err) {
throw `Failed to remote user ${userIdentifier} from
group ${groupIdentifier}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to remote user ${userIdentifier} from group ${groupIdentifier}: ${err}`
);
}
}
}
@ -240,11 +317,10 @@ export class GroupsApiClient {
groupIdentifier: string
): Promise<ListView<PartyRoleMembership>> {
const groupParam: string = buildIdentifierParam(groupIdentifier);
const url = `${this.#GROUPS_API_PREFIX}/${groupParam}/roles`;
try {
const response: ApiResponse = await this.#apiClient.get(
`${this.#GROUPS_API_PREFIX}/${groupParam}/roles`
);
const response: ApiResponse = await this.#apiClient.get(url);
if (response.ok) {
const result: Record<
string,
@ -265,11 +341,22 @@ export class GroupsApiClient {
offset: result.offset as number,
};
} else {
throw `Failed to get roles of group ${groupIdentifier}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"get",
`Failed to get roles of group ${groupIdentifier}`,
url
);
}
} catch (err) {
throw `Failed to get roles of group ${groupIdentifier}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to get roles of group ${groupIdentifier}: ${err}`
);
}
}
}
@ -277,22 +364,32 @@ export class GroupsApiClient {
groupIdentifier: string | number,
roleIdentifier: string | number
): Promise<void> {
try {
const groupParam = buildIdentifierParam(groupIdentifier);
const roleParam = buildIdentifierParam(roleIdentifier);
const response: ApiResponse = await this.#apiClient.put(
`${this.#GROUPS_API_PREFIX}/${groupParam}/roles/${roleParam}`
);
const url = `${
this.#GROUPS_API_PREFIX
}/${groupParam}/roles/${roleParam}`;
try {
const response: ApiResponse = await this.#apiClient.put(url);
if (response.ok) {
return;
} else {
throw `Failed to add role ${roleIdentifier} to
group ${groupIdentifier}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"put",
`Failed to add role ${roleIdentifier} to group ${groupIdentifier}`,
url
);
}
} catch (err) {
throw `Failed to add role ${roleIdentifier} to
group ${groupIdentifier}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to add role ${roleIdentifier} to group ${groupIdentifier}: ${err}`
);
}
}
}
@ -300,22 +397,32 @@ export class GroupsApiClient {
groupIdentifier: string | number,
roleIdentifier: string | number
): Promise<void> {
try {
const groupParam = buildIdentifierParam(groupIdentifier);
const roleParam = buildIdentifierParam(roleIdentifier);
const response: ApiResponse = await this.#apiClient.delete(
`${this.#GROUPS_API_PREFIX}/${groupParam}/roles/${roleParam}`
);
const url = `${
this.#GROUPS_API_PREFIX
}/${groupParam}/roles/${roleParam}`;
try {
const response: ApiResponse = await this.#apiClient.delete(url);
if (response.ok) {
return;
} else {
throw `Failed to remove role ${roleIdentifier} from
group ${groupIdentifier}:
${response.status} ${response.statusText}`;
throw new ApiError(
response.status,
response.statusText,
"delete",
`Failed to remove role ${roleIdentifier} from group ${groupIdentifier}`,
url
);
}
} catch (err) {
throw `Failed to remote role ${roleIdentifier} from
group ${groupIdentifier}: ${err}`;
if (err instanceof ApiError) {
throw err;
} else {
throw new ApiClientError(
`Failed to remote role ${roleIdentifier} from group ${groupIdentifier}: ${err}`
);
}
}
}
}