From 70568a65ffaf2673dd3ef2ace360b219e256a38f Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Fri, 14 Aug 2020 17:06:42 +0200 Subject: [PATCH] Use Error subclasses instead of throwing strings --- .../src/main/typescript/clients/groups-api.ts | 303 ++++++++++++------ 1 file changed, 205 insertions(+), 98 deletions(-) diff --git a/ccm-core-apiclient/src/main/typescript/clients/groups-api.ts b/ccm-core-apiclient/src/main/typescript/clients/groups-api.ts index 26cf5d10e..c3c17fc15 100644 --- a/ccm-core-apiclient/src/main/typescript/clients/groups-api.ts +++ b/ccm-core-apiclient/src/main/typescript/clients/groups-api.ts @@ -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> { + const url = `${this.#GROUPS_API_PREFIX}`; try { - const response: ApiResponse = await this.#apiClient.get( - `${this.#GROUPS_API_PREFIX}`, - { - 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< 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 { + const url = `${this.#GROUPS_API_PREFIX}/${buildIdentifierParam( + groupIdentifier + )}`; try { - const response: ApiResponse = await this.#apiClient.get( - `${this.#GROUPS_API_PREFIX}/${buildIdentifierParam( - groupIdentifier - )}` - ); + const response: ApiResponse = await this.#apiClient.get(url); if (response.ok) { return buildGroupFromRecord( (await response.json()) as Record ); } 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 { + 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 { + 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 { + const url = `${this.#GROUPS_API_PREFIX}/${buildIdentifierParam( + groupIdentifier + )}`; try { - const response: ApiResponse = await this.#apiClient.delete( - `${this.#GROUPS_API_PREFIX}/${buildIdentifierParam( - groupIdentifier - )}` - ); + 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> { + const groupParam: string = buildIdentifierParam(groupIdentifier); + const url = `${this.#GROUPS_API_PREFIX}/${groupParam}/members`; try { - const groupParam: string = buildIdentifierParam(groupIdentifier); - - const response: ApiResponse = await this.#apiClient.get( - `${this.#GROUPS_API_PREFIX}/${groupParam}/members`, - { - 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< 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 { + const groupParam = buildIdentifierParam(groupIdentifier); + const memberParam = buildIdentifierParam(userIdentifier); + const url = `${ + this.#GROUPS_API_PREFIX + }/${groupParam}/members/${memberParam}`; try { - const groupParam = buildIdentifierParam(groupIdentifier); - const memberParam = buildIdentifierParam(userIdentifier); - const response: ApiResponse = await this.#apiClient.put( - `${ - this.#GROUPS_API_PREFIX - }/${groupParam}/members/${memberParam}` - ); + 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 { + const groupParam = buildIdentifierParam(groupIdentifier); + const memberParam = buildIdentifierParam(userIdentifier); + const url = `${ + this.#GROUPS_API_PREFIX + }/${groupParam}/members/${memberParam}`; try { - const groupParam = buildIdentifierParam(groupIdentifier); - const memberParam = buildIdentifierParam(userIdentifier); - const response: ApiResponse = await this.#apiClient.delete( - `${ - this.#GROUPS_API_PREFIX - }/${groupParam}/members/${memberParam}` - ); + 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> { 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 { + const groupParam = buildIdentifierParam(groupIdentifier); + const roleParam = buildIdentifierParam(roleIdentifier); + const url = `${ + this.#GROUPS_API_PREFIX + }/${groupParam}/roles/${roleParam}`; try { - const groupParam = buildIdentifierParam(groupIdentifier); - const roleParam = buildIdentifierParam(roleIdentifier); - const response: ApiResponse = await this.#apiClient.put( - `${this.#GROUPS_API_PREFIX}/${groupParam}/roles/${roleParam}` - ); + 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 { + const groupParam = buildIdentifierParam(groupIdentifier); + const roleParam = buildIdentifierParam(roleIdentifier); + const url = `${ + this.#GROUPS_API_PREFIX + }/${groupParam}/roles/${roleParam}`; try { - const groupParam = buildIdentifierParam(groupIdentifier); - const roleParam = buildIdentifierParam(roleIdentifier); - const response: ApiResponse = await this.#apiClient.delete( - `${this.#GROUPS_API_PREFIX}/${groupParam}/roles/${roleParam}` - ); + 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}` + ); + } } } }