From e94aee62be6daa89fcaadeb3ddf5340eac04fe42 Mon Sep 17 00:00:00 2001 From: Martin Sottnik Date: Tue, 24 Sep 2024 09:48:21 +0200 Subject: [PATCH] handle install/uninstall uniconfig errors (#479) --- src/external-api/errors.ts | 8 +++++++- src/external-api/helpers.ts | 2 +- src/external-api/uniconfig-cache.ts | 7 ++++++- src/schema/device.ts | 31 ++++++++++++++++++++++++----- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/external-api/errors.ts b/src/external-api/errors.ts index 6473d524..80397590 100644 --- a/src/external-api/errors.ts +++ b/src/external-api/errors.ts @@ -1,8 +1,14 @@ export class ExternalApiError extends Error { code: number; + // message: string | null; - constructor(code: number) { + constructor(code: number, message?: string) { super(); this.code = code; + this.message = message ?? ''; + } + + getErrorMessage() { + return `{ "status": ${this.code}, "message": ${this.message} }`; } } diff --git a/src/external-api/helpers.ts b/src/external-api/helpers.ts index bbf6eb20..0484500c 100644 --- a/src/external-api/helpers.ts +++ b/src/external-api/helpers.ts @@ -72,7 +72,7 @@ async function apiFetch(path: APIPath, options: RequestInit): Promise { if (!response.ok) { const message = await response.text(); logError(requestId, response.status, message); - throw new ExternalApiError(response.status); + throw new ExternalApiError(response.status, message); } if (response.status === 201 || response.status === 204) { diff --git a/src/external-api/uniconfig-cache.ts b/src/external-api/uniconfig-cache.ts index e8345d55..1507235a 100644 --- a/src/external-api/uniconfig-cache.ts +++ b/src/external-api/uniconfig-cache.ts @@ -8,6 +8,7 @@ import { uninstallDevice, uninstallMultipleDevices, } from './uniconfig'; +import { ExternalApiError } from './errors'; export class UniconfigCache { private static instance: UniconfigCache; @@ -116,7 +117,11 @@ export async function installMultipleDevicesCache({ try { await installMultipleDevices(url, devicesToInstall); - } catch { + } catch (e) { + if (e instanceof ExternalApiError) { + throw e; + } + throw new Error('could not install device'); } diff --git a/src/schema/device.ts b/src/schema/device.ts index e0c1bdc4..ac74ee4c 100644 --- a/src/schema/device.ts +++ b/src/schema/device.ts @@ -44,6 +44,7 @@ import { LabelConnection } from './label'; import { Location } from './location'; import { Zone } from './zone'; import config from '../config'; +import { ExternalApiError } from '../external-api/errors'; export const DeviceServiceState = enumType({ name: 'DeviceServiceState', @@ -608,7 +609,15 @@ export const InstallDeviceMutation = extendType({ const { mountParameters } = device; const installDeviceParams = prepareInstallParameters(device.name, mountParameters); const uniconfigURL = await getUniconfigURL(prisma, device.uniconfigZoneId); - await installDeviceCache({ uniconfigURL, deviceName: device.name, params: installDeviceParams }); + try { + await installDeviceCache({ uniconfigURL, deviceName: device.name, params: installDeviceParams }); + } catch (e) { + if (e instanceof ExternalApiError) { + throw new Error(e.getErrorMessage()); + } + + throw e; + } return { device }; }, }); @@ -649,7 +658,13 @@ export const UninstallDeviceMutation = extendType({ throw new Error('device not found'); } const uniconfigURL = await getUniconfigURL(prisma, device.uniconfigZoneId); - await uninstallDeviceCache({ uniconfigURL, params: uninstallParams, deviceName: device.name }); + try { + await uninstallDeviceCache({ uniconfigURL, params: uninstallParams, deviceName: device.name }); + } catch (e) { + if (e instanceof ExternalApiError) { + throw new Error(e.getErrorMessage()); + } + } return { device }; }, }); @@ -792,9 +807,15 @@ export const BulkInstallDevicesMutation = extendType({ }), ); - await Promise.all( - devicesToInstallWithParams.map((devicesToInstall) => installMultipleDevicesCache(devicesToInstall)), - ); + try { + await Promise.all( + devicesToInstallWithParams.map((devicesToInstall) => installMultipleDevicesCache(devicesToInstall)), + ); + } catch (e) { + if (e instanceof ExternalApiError) { + throw new Error(e.getErrorMessage()); + } + } return { installedDevices: devices }; },