Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FD-717 external api errors #479

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
42 changes: 34 additions & 8 deletions src/schema/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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', {
Expand Down Expand Up @@ -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 };
},
});
Expand Down Expand Up @@ -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 };
},
});
Expand Down Expand Up @@ -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 };
},
Expand Down
Loading