From 2f53cb4b674cec4a9b75ddbf65d7799aef484ca7 Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Wed, 12 Aug 2020 13:46:49 +0200 Subject: [PATCH] Use Error for throw Former-commit-id: 6dbc59202671ceb880d476254ae68978afaef986 --- .../main/typescript/ccm-apiclient-commons.ts | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/ccm-apiclient-commons/src/main/typescript/ccm-apiclient-commons.ts b/ccm-apiclient-commons/src/main/typescript/ccm-apiclient-commons.ts index 19392cc08..1a89e7689 100644 --- a/ccm-apiclient-commons/src/main/typescript/ccm-apiclient-commons.ts +++ b/ccm-apiclient-commons/src/main/typescript/ccm-apiclient-commons.ts @@ -22,7 +22,9 @@ export { LibreCcmApiClient, ApiResponse, ApiError, buildUrl }; */ export function buildEmbeddedApiClient(): LibreCcmApiClient { if (!isFetchAvailable()) { - throw "Fetch API is not available. Please use buildIsomorpicApiClient."; + throw new Error( + "Fetch API is not available. Please use buildIsomorpicApiClient." + ); } const baseUrl = new URL(document.documentURI); baseUrl.hash = ""; @@ -48,7 +50,9 @@ export function buildRemoteApiClient( jwt: string ): LibreCcmApiClient { if (!isFetchAvailable()) { - throw "Fetch API is not available. Please use buildIsomorpicApiClient."; + throw new Error( + "Fetch API is not available. Please use buildIsomorpicApiClient." + ); } return new ApiClientFetchImpl(baseUrl, { @@ -118,8 +122,16 @@ export function assertProperties( ): void { const missing = findMissingProperties(record, properties); if (missing.length > 0) { - throw `record is missing the following required properties: - ${missing.join(",")}`; + throw new MissingPropertiesError(missing); + } +} + +export class MissingPropertiesError extends Error { + constructor(missingProperties: string[]) { + super(`record is missing the following required properties: + ${missingProperties.join(",")}`); + + this.name = "MissingPropertiesError"; } } @@ -141,11 +153,11 @@ export function findMissingProperties( } /** - * Builds an identifier parameter from the the provided parameter. If the + * Builds an identifier parameter from the the provided parameter. If the * parameter is a number it is converted to a string and prefixed with `ID-`. * If the parameter matches the regex for a UUID, the identifier is prefixed * with `UUID-`. Otherwise the identifier is returned as it is. - * + * * @param identifier The identifier to process. */ export function buildIdentifierParam(identifier: string | number): string {