Skip to content

Commit

Permalink
Stop silencing Request error: No online device(s) found errors for …
Browse files Browse the repository at this point in the history
…`reboot` and `restartApplication` methods

Resolves: #577
Resolves: #649
Change-type: major
  • Loading branch information
myarmolinsky committed Nov 27, 2024
1 parent db42bdb commit 43f7f18
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 98 deletions.
89 changes: 34 additions & 55 deletions src/models/device.supervisor-api.partial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ import type {
} from '..';
import type { Device } from '../types/models';

import {
isNotFoundResponse,
treatAsMissingDevice,
withSupervisorLockedError,
} from '../util';
import { withSupervisorLockedError } from '../util';

import { ensureVersionCompatibility } from '../util/device-os-version';

Expand Down Expand Up @@ -68,9 +64,6 @@ export const getSupervisorApiHelper = function (
} = deps;
const { apiUrl } = opts;

const getId = (uuidOrId: string | number) =>
sdkInstance.models.device._getId(uuidOrId);

const exports = {
/**
* @summary Ping a device
Expand Down Expand Up @@ -166,37 +159,30 @@ export const getSupervisorApiHelper = function (
*/
restartApplication: (uuidOrId: string | number): Promise<void> =>
withSupervisorLockedError(async () => {
try {
const deviceOptions = {
$select: ['id', 'supervisor_version'],
$expand: { belongs_to__application: { $select: 'id' } },
} satisfies PineOptions<Device>;
const device = (await sdkInstance.models.device.get(
uuidOrId,
deviceOptions,
)) as PineTypedResult<Device, typeof deviceOptions>;
const deviceOptions = {
$select: ['id', 'supervisor_version'],
$expand: { belongs_to__application: { $select: 'id' } },
} satisfies PineOptions<Device>;
const device = (await sdkInstance.models.device.get(
uuidOrId,
deviceOptions,
)) as PineTypedResult<Device, typeof deviceOptions>;

const appId = device.belongs_to__application[0].id;
const { body } = await request.send({
method: 'POST',
url: `/supervisor/v1/restart`,
baseUrl: apiUrl,
body: {
deviceId: device.id,
const appId = device.belongs_to__application[0].id;
const { body } = await request.send({
method: 'POST',
url: `/supervisor/v1/restart`,
baseUrl: apiUrl,
body: {
deviceId: device.id,
appId,
data: {
appId,
data: {
appId,
},
},
timeout: CONTAINER_ACTION_ENDPOINT_TIMEOUT,
});
return body;
} catch (err) {
if (isNotFoundResponse(err)) {
treatAsMissingDevice(uuidOrId, err);
}
throw err;
}
},
timeout: CONTAINER_ACTION_ENDPOINT_TIMEOUT,
});
return body;
}),

/**
Expand Down Expand Up @@ -226,26 +212,19 @@ export const getSupervisorApiHelper = function (
options = {};
}

try {
const deviceId = await getId(uuidOrId);
const { body } = await request.send({
method: 'POST',
url: '/supervisor/v1/reboot',
baseUrl: apiUrl,
body: {
deviceId,
data: {
force: Boolean(options?.force),
},
const deviceId = (await sdkInstance.models.device.get(uuidOrId)).id;
const { body } = await request.send({
method: 'POST',
url: '/supervisor/v1/reboot',
baseUrl: apiUrl,
body: {
deviceId,
data: {
force: Boolean(options?.force),
},
});
return body;
} catch (err) {
if (isNotFoundResponse(err)) {
treatAsMissingDevice(uuidOrId, err);
}
throw err;
}
},
});
return body;
}),

/**
Expand Down
45 changes: 12 additions & 33 deletions src/models/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ import memoizee from 'memoizee';

import {
isId,
isNoDeviceForKeyResponse,
isFullUuid,
mergePineOptions,
treatAsMissingDevice,
limitedMap,
groupByMap,
} from '../util';
Expand Down Expand Up @@ -222,17 +220,6 @@ const getDeviceModel = function (
return (await sdkInstance.models.config.getAll()).deviceUrlsBase;
});

// Internal method for uuid/id disambiguation
// Note that this throws an exception for missing uuids, but not missing ids
const getId = async (uuidOrId: string | number) => {
if (isId(uuidOrId)) {
return uuidOrId;
} else {
const { id } = await exports.get(uuidOrId, { $select: 'id' });
return id;
}
};

const getAppliedConfigVariableValue = async (
uuidOrId: string | number,
name: string,
Expand Down Expand Up @@ -433,7 +420,6 @@ const getDeviceModel = function (
}

const exports = {
_getId: getId,
OverallStatus,
/**
* @summary Get Dashboard URL for a specific device
Expand Down Expand Up @@ -1456,25 +1442,18 @@ const getDeviceModel = function (
keyDescription?: string,
keyExpiryDate?: string,
): Promise<string> => {
try {
const deviceId = await getId(uuidOrId);
const { body } = await request.send({
method: 'POST',
url: `/api-key/device/${deviceId}/device-key`,
baseUrl: apiUrl,
body: {
name: keyName,
description: keyDescription,
expiryDate: keyExpiryDate,
},
});
return body;
} catch (err) {
if (isNoDeviceForKeyResponse(err)) {
treatAsMissingDevice(uuidOrId, err);
}
throw err;
}
const deviceId = await sdkInstance.models.device.get(uuidOrId);
const { body } = await request.send({
method: 'POST',
url: `/api-key/device/${deviceId}/device-key`,
baseUrl: apiUrl,
body: {
name: keyName,
description: keyDescription,
expiryDate: keyExpiryDate,
},
});
return body;
},

/**
Expand Down
10 changes: 0 additions & 10 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ export const isUnauthorizedResponse = (err: Error) =>
export const isNotFoundResponse = (err: Error) =>
isBalenaRequestErrorResponseWithCode(err, 404);

export const isNoDeviceForKeyResponse = (err: Error) =>
isBalenaRequestErrorResponseWithCode(err, 500) &&
err.body === 'No device found to associate with the api key';

export const isNoApplicationForKeyResponse = (err: Error) =>
isBalenaRequestErrorResponseWithCode(err, 500) &&
err.body === 'No application found to associate with the api key';
Expand All @@ -78,12 +74,6 @@ export const treatAsMissingApplication = (
throw replacementErr;
};

export const treatAsMissingDevice = (uuidOrId: string | number, err: Error) => {
const replacementErr = new errors.BalenaDeviceNotFound(uuidOrId);
replacementErr.stack = err.stack ?? '';
throw replacementErr;
};

// TODO: Make it so that it also infers the extras param
export function mergePineOptionsTyped<
R extends object,
Expand Down

0 comments on commit 43f7f18

Please sign in to comment.