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 ed41a252..40065816 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 3dba97f1..13714111 100644 --- a/src/schema/device.ts +++ b/src/schema/device.ts @@ -43,6 +43,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', @@ -116,9 +117,14 @@ export const Device = objectType({ t.nonNull.boolean('isInstalled', { resolve: async (root, _, { prisma }) => { const { uniconfigZoneId } = root; - const uniconfigURL = await getUniconfigURL(prisma, uniconfigZoneId); - const isInstalled = await getCachedDeviceInstallStatus(uniconfigURL, root.name); - return isInstalled; + try { + const uniconfigURL = await getUniconfigURL(prisma, uniconfigZoneId); + const isInstalled = await getCachedDeviceInstallStatus(uniconfigURL, root.name); + return isInstalled; + } catch { + // FD-683 supress isInstalled error when something is wrong with uniconfig + return false; + } }, }); t.nonNull.field('zone', { @@ -587,7 +593,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 }; }, }); @@ -628,7 +642,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 }; }, }); @@ -771,9 +791,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 }; },