Use Error subclasses instead of throwing strings
parent
b92356b844
commit
d0a881ec95
|
|
@ -1,4 +1,6 @@
|
||||||
import {
|
import {
|
||||||
|
ApiClientError,
|
||||||
|
ApiError,
|
||||||
ApiResponse,
|
ApiResponse,
|
||||||
LibreCcmApiClient,
|
LibreCcmApiClient,
|
||||||
ListView,
|
ListView,
|
||||||
|
|
@ -27,14 +29,12 @@ export class SitesApiClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
async getSites(limit: number, offset: number): Promise<ListView<Site>> {
|
async getSites(limit: number, offset: number): Promise<ListView<Site>> {
|
||||||
|
const url = `${this.#SITES_API_PREFIX}`;
|
||||||
try {
|
try {
|
||||||
const response: ApiResponse = await this.#apiClient.get(
|
const response: ApiResponse = await this.#apiClient.get(url, {
|
||||||
`${this.#SITES_API_PREFIX}`,
|
limit: limit.toString(),
|
||||||
{
|
offset: offset.toString(),
|
||||||
limit: limit.toString(),
|
});
|
||||||
offset: offset.toString(),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const result: Record<
|
const result: Record<
|
||||||
string,
|
string,
|
||||||
|
|
@ -55,48 +55,79 @@ export class SitesApiClient {
|
||||||
offset: result.offset as number,
|
offset: result.offset as number,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to get sites:
|
throw new ApiError(
|
||||||
${response.status} ${response.statusText}`;
|
response.status,
|
||||||
|
response.statusText,
|
||||||
|
"get",
|
||||||
|
`Failed to get sites`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to get sites: ${err}`;
|
if (err instanceof ApiError) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(`Failed to get sites: ${err}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getSite(siteIdentifier: string | number): Promise<Site> {
|
async getSite(siteIdentifier: string | number): Promise<Site> {
|
||||||
try {
|
try {
|
||||||
const response: ApiResponse = await this.#apiClient.get(
|
const url = `${this.#SITES_API_PREFIX}/${buildIdentifierParam(
|
||||||
`${this.#SITES_API_PREFIX}/${buildIdentifierParam(
|
siteIdentifier
|
||||||
siteIdentifier
|
)}`;
|
||||||
)}`
|
const response: ApiResponse = await this.#apiClient.get(url);
|
||||||
);
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return buildSiteFromRecord(
|
return buildSiteFromRecord(
|
||||||
(await response.json()) as Record<string, unknown>
|
(await response.json()) as Record<string, unknown>
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to get site ${siteIdentifier}:
|
throw new ApiError(
|
||||||
${response.status} ${response.statusText}`;
|
response.status,
|
||||||
|
response.statusText,
|
||||||
|
"get",
|
||||||
|
`Failed to get site ${siteIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to get site ${siteIdentifier}: ${err}`;
|
if (err instanceof ApiError) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to get site ${siteIdentifier}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async addSite(site: Site): Promise<string> {
|
async addSite(site: Site): Promise<string> {
|
||||||
|
const url = `${this.#SITES_API_PREFIX}`;
|
||||||
try {
|
try {
|
||||||
const response: ApiResponse = await this.#apiClient.post(
|
const response: ApiResponse = await this.#apiClient.post(
|
||||||
`${this.#SITES_API_PREFIX}`,
|
url,
|
||||||
JSON.stringify(site)
|
JSON.stringify(site)
|
||||||
);
|
);
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return site.domainOfSite;
|
return site.domainOfSite;
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to add site ${site.domainOfSite}:
|
throw new ApiError(
|
||||||
${response.status} ${response.statusText}`;
|
response.status,
|
||||||
|
response.statusText,
|
||||||
|
"post",
|
||||||
|
`Failed to add site ${site.domainOfSite}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to add site ${site.domainOfSite}: ${err}`;
|
if (err instanceof ApiClientError) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to add site ${site.domainOfSite}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -104,39 +135,61 @@ export class SitesApiClient {
|
||||||
siteIdentifier: string | number,
|
siteIdentifier: string | number,
|
||||||
site: Site
|
site: Site
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
const url = `${this.#SITES_API_PREFIX}/${buildIdentifierParam(
|
||||||
|
siteIdentifier
|
||||||
|
)}`;
|
||||||
try {
|
try {
|
||||||
const response: ApiResponse = await this.#apiClient.put(
|
const response: ApiResponse = await this.#apiClient.put(
|
||||||
`${this.#SITES_API_PREFIX}/${buildIdentifierParam(
|
url,
|
||||||
siteIdentifier
|
|
||||||
)}`,
|
|
||||||
JSON.stringify(site)
|
JSON.stringify(site)
|
||||||
);
|
);
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to update site ${siteIdentifier}:
|
throw new ApiError(
|
||||||
${response.status} ${response.statusText}`;
|
response.status,
|
||||||
|
response.statusText,
|
||||||
|
"put",
|
||||||
|
`Failed to update site ${siteIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to update site ${siteIdentifier}: ${err}`;
|
if (err instanceof ApiError) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to update site ${siteIdentifier}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteSite(siteIdentifier: string | number): Promise<void> {
|
async deleteSite(siteIdentifier: string | number): Promise<void> {
|
||||||
|
const url = `${this.#SITES_API_PREFIX}/${buildIdentifierParam(
|
||||||
|
siteIdentifier
|
||||||
|
)}`;
|
||||||
try {
|
try {
|
||||||
const response: ApiResponse = await this.#apiClient.delete(
|
const response: ApiResponse = await this.#apiClient.delete(url);
|
||||||
`${this.#SITES_API_PREFIX}/${buildIdentifierParam(
|
|
||||||
siteIdentifier
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to delete site ${siteIdentifier}:
|
throw new ApiError(
|
||||||
${response.status} ${response.statusText}`;
|
response.status,
|
||||||
|
response.statusText,
|
||||||
|
"delete",
|
||||||
|
`Failed to delete site ${siteIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to delete site ${siteIdentifier}: ${err}`;
|
if (err instanceof ApiError) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to delete site ${siteIdentifier}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -145,16 +198,13 @@ export class SitesApiClient {
|
||||||
limit: number,
|
limit: number,
|
||||||
offset: number
|
offset: number
|
||||||
): Promise<ListView<CcmApplicationId>> {
|
): Promise<ListView<CcmApplicationId>> {
|
||||||
|
const siteParam = buildIdentifierParam(siteIdentifier);
|
||||||
|
const url = `${this.#SITES_API_PREFIX}/${siteParam}/applications`;
|
||||||
try {
|
try {
|
||||||
const siteParam = buildIdentifierParam(siteIdentifier);
|
const response: ApiResponse = await this.#apiClient.get(url, {
|
||||||
|
limit: limit.toString(),
|
||||||
const response: ApiResponse = await this.#apiClient.get(
|
offset: offset.toString(),
|
||||||
`${this.#SITES_API_PREFIX}/${siteParam}/applications`,
|
});
|
||||||
{
|
|
||||||
limit: limit.toString(),
|
|
||||||
offset: offset.toString(),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const result: Record<
|
const result: Record<
|
||||||
string,
|
string,
|
||||||
|
|
@ -175,13 +225,22 @@ export class SitesApiClient {
|
||||||
offset: result.limit as number,
|
offset: result.limit as number,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to get applications of
|
throw new ApiError(
|
||||||
site ${siteIdentifier}:
|
response.status,
|
||||||
${response.status} ${response.statusText}`;
|
response.statusText,
|
||||||
|
"get",
|
||||||
|
`Failed to get applications of site ${siteIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to get applications of
|
if (err instanceof ApiError) {
|
||||||
site ${siteIdentifier}: ${err}`;
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to get applications of site ${siteIdentifier}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -189,29 +248,34 @@ export class SitesApiClient {
|
||||||
siteIdentifier: string | number,
|
siteIdentifier: string | number,
|
||||||
applicationIdentifier: string | number
|
applicationIdentifier: string | number
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
const siteParam: string = buildIdentifierParam(siteIdentifier);
|
||||||
|
const applicationParam: string = buildIdentifierParam(
|
||||||
|
applicationIdentifier
|
||||||
|
);
|
||||||
|
const url = `${
|
||||||
|
this.#SITES_API_PREFIX
|
||||||
|
}/${siteParam}/groups/${applicationParam}`;
|
||||||
try {
|
try {
|
||||||
const siteParam: string = buildIdentifierParam(siteIdentifier);
|
const response: ApiResponse = await this.#apiClient.put(url);
|
||||||
const applicationParam: string = buildIdentifierParam(
|
|
||||||
applicationIdentifier
|
|
||||||
);
|
|
||||||
|
|
||||||
const response: ApiResponse = await this.#apiClient.put(
|
|
||||||
`${
|
|
||||||
this.#SITES_API_PREFIX
|
|
||||||
}/${siteParam}/groups/${applicationParam}`
|
|
||||||
);
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to add application ${applicationIdentifier}
|
throw new ApiError(
|
||||||
to site
|
response.status,
|
||||||
${siteIdentifier}:
|
response.statusText,
|
||||||
${response.status} ${response.statusText}`;
|
"put",
|
||||||
|
`Failed to add application ${applicationIdentifier} to site ${siteIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to add application ${applicationIdentifier}
|
if (err instanceof ApiError) {
|
||||||
to site
|
throw err;
|
||||||
${siteIdentifier}: ${err}`;
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to add application ${applicationIdentifier} to site ${siteIdentifier}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -219,29 +283,35 @@ export class SitesApiClient {
|
||||||
siteIdentifier: string | number,
|
siteIdentifier: string | number,
|
||||||
applicationIdentifier: string | number
|
applicationIdentifier: string | number
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
const siteParam: string = buildIdentifierParam(siteIdentifier);
|
||||||
const siteParam: string = buildIdentifierParam(siteIdentifier);
|
const applicationParam: string = buildIdentifierParam(
|
||||||
const applicationParam: string = buildIdentifierParam(
|
applicationIdentifier
|
||||||
applicationIdentifier
|
);
|
||||||
);
|
const url = `${
|
||||||
|
this.#SITES_API_PREFIX
|
||||||
|
}/${siteParam}/groups/${applicationParam}`;
|
||||||
|
|
||||||
const response: ApiResponse = await this.#apiClient.delete(
|
try {
|
||||||
`${
|
const response: ApiResponse = await this.#apiClient.delete(url);
|
||||||
this.#SITES_API_PREFIX
|
|
||||||
}/${siteParam}/groups/${applicationParam}`
|
|
||||||
);
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to remove application ${applicationIdentifier}
|
throw new ApiError(
|
||||||
fromsite
|
response.status,
|
||||||
${siteIdentifier}:
|
response.statusText,
|
||||||
${response.status} ${response.statusText}`;
|
"delete",
|
||||||
|
`Failed to remove application ${applicationIdentifier} fromsite ${siteIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to remove application ${applicationIdentifier}
|
if (err instanceof ApiError) {
|
||||||
from site
|
throw err;
|
||||||
${siteIdentifier}: ${err}`;
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to remove application ${applicationIdentifier} from site ${siteIdentifier}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue