Skip to content

Commit

Permalink
handle install/uninstall uniconfig errors (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
soson committed Sep 24, 2024
1 parent adbbb36 commit e94aee6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/external-api/errors.ts
Original file line number Diff line number Diff line change
@@ -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} }`;
}
}
2 changes: 1 addition & 1 deletion src/external-api/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async function apiFetch(path: APIPath, options: RequestInit): Promise<unknown> {
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) {
Expand Down
7 changes: 6 additions & 1 deletion src/external-api/uniconfig-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
uninstallDevice,
uninstallMultipleDevices,
} from './uniconfig';
import { ExternalApiError } from './errors';

export class UniconfigCache {
private static instance: UniconfigCache;
Expand Down Expand Up @@ -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');
}

Expand Down
31 changes: 26 additions & 5 deletions src/schema/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 };
},
});
Expand Down Expand Up @@ -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 };
},
});
Expand Down Expand Up @@ -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 };
},
Expand Down

0 comments on commit e94aee6

Please sign in to comment.