Skip to content

Commit 18806b1

Browse files
author
Akos Kitta
committed
Handle missing core when getting board user fields
Closes #1142 Signed-off-by: Akos Kitta <[email protected]>
1 parent 75e00c2 commit 18806b1

File tree

1 file changed

+59
-31
lines changed

1 file changed

+59
-31
lines changed

Diff for: arduino-ide-extension/src/node/boards-service-impl.ts

+59-31
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
SupportedUserFieldsResponse,
4242
} from './cli-protocol/cc/arduino/cli/commands/v1/upload_pb';
4343
import { ExecuteWithProgress } from './grpc-progressible';
44+
import { ServiceError } from './service-error';
4445

4546
@injectable()
4647
export class BoardsServiceImpl
@@ -84,19 +85,7 @@ export class BoardsServiceImpl
8485
(resolve, reject) =>
8586
client.boardDetails(detailsReq, (err, resp) => {
8687
if (err) {
87-
// Required cores are not installed manually: https://github.com/arduino/arduino-cli/issues/954
88-
if (
89-
(err.message.indexOf('missing platform release') !== -1 &&
90-
err.message.indexOf('referenced by board') !== -1) ||
91-
// Platform is not installed.
92-
(err.message.indexOf('platform') !== -1 &&
93-
err.message.indexOf('not installed') !== -1)
94-
) {
95-
resolve(undefined);
96-
return;
97-
}
98-
// It's a hack to handle https://github.com/arduino/arduino-cli/issues/1262 gracefully.
99-
if (err.message.indexOf('unknown package') !== -1) {
88+
if (isMissingPlatformError(err)) {
10089
resolve(undefined);
10190
return;
10291
}
@@ -249,26 +238,38 @@ export class BoardsServiceImpl
249238
const coreClient = await this.coreClient;
250239
const { client, instance } = coreClient;
251240

252-
const supportedUserFieldsReq = new SupportedUserFieldsRequest();
253-
supportedUserFieldsReq.setInstance(instance);
254-
supportedUserFieldsReq.setFqbn(options.fqbn);
255-
supportedUserFieldsReq.setProtocol(options.protocol);
241+
const req = new SupportedUserFieldsRequest();
242+
req.setInstance(instance);
243+
req.setFqbn(options.fqbn);
244+
req.setProtocol(options.protocol);
256245

257-
const supportedUserFieldsResp =
258-
await new Promise<SupportedUserFieldsResponse>((resolve, reject) => {
259-
client.supportedUserFields(supportedUserFieldsReq, (err, resp) => {
260-
!!err ? reject(err) : resolve(resp);
246+
const resp = await new Promise<SupportedUserFieldsResponse | undefined>(
247+
(resolve, reject) => {
248+
client.supportedUserFields(req, (err, resp) => {
249+
if (err) {
250+
if (isMissingPlatformError(err)) {
251+
resolve(undefined);
252+
return;
253+
}
254+
reject(err);
255+
return;
256+
}
257+
resolve(resp);
261258
});
262-
});
263-
return supportedUserFieldsResp.getUserFieldsList().map((e) => {
264-
return {
265-
toolId: e.getToolId(),
266-
name: e.getName(),
267-
label: e.getLabel(),
268-
secret: e.getSecret(),
269-
value: '',
270-
};
271-
});
259+
}
260+
);
261+
262+
if (!resp) {
263+
return [];
264+
}
265+
266+
return resp.getUserFieldsList().map((e) => ({
267+
toolId: e.getToolId(),
268+
name: e.getName(),
269+
label: e.getLabel(),
270+
secret: e.getSecret(),
271+
value: '',
272+
}));
272273
}
273274

274275
async search(options: { query?: string }): Promise<BoardsPackage[]> {
@@ -486,3 +487,30 @@ export class BoardsServiceImpl
486487
console.info('<<< Boards package uninstallation done.', item);
487488
}
488489
}
490+
491+
function isMissingPlatformError(error: unknown): boolean {
492+
if (ServiceError.is(error)) {
493+
const message = error.details;
494+
// TODO: check gRPC status code? `9 FAILED_PRECONDITION` (https://grpc.github.io/grpc/core/md_doc_statuscodes.html)
495+
496+
// When installing a 3rd party core that depends on a missing Arduino core.
497+
// https://github.com/arduino/arduino-cli/issues/954
498+
if (
499+
message.includes('missing platform release') &&
500+
message.includes('referenced by board')
501+
) {
502+
return true;
503+
}
504+
505+
// When the platform is not installed.
506+
if (message.includes('platform') && message.includes('not installed')) {
507+
return true;
508+
}
509+
510+
// It's a hack to handle https://github.com/arduino/arduino-cli/issues/1262 gracefully.
511+
if (message.includes('unknown package')) {
512+
return true;
513+
}
514+
}
515+
return false;
516+
}

0 commit comments

Comments
 (0)