From 18806b1033fad774fdf88cb8afb85866d15a7a9c Mon Sep 17 00:00:00 2001 From: Akos Kitta Date: Tue, 16 Aug 2022 09:18:50 +0200 Subject: [PATCH] Handle missing core when getting board user fields Closes #1142 Signed-off-by: Akos Kitta --- .../src/node/boards-service-impl.ts | 90 ++++++++++++------- 1 file changed, 59 insertions(+), 31 deletions(-) diff --git a/arduino-ide-extension/src/node/boards-service-impl.ts b/arduino-ide-extension/src/node/boards-service-impl.ts index b46a79677..e4f1fad3b 100644 --- a/arduino-ide-extension/src/node/boards-service-impl.ts +++ b/arduino-ide-extension/src/node/boards-service-impl.ts @@ -41,6 +41,7 @@ import { SupportedUserFieldsResponse, } from './cli-protocol/cc/arduino/cli/commands/v1/upload_pb'; import { ExecuteWithProgress } from './grpc-progressible'; +import { ServiceError } from './service-error'; @injectable() export class BoardsServiceImpl @@ -84,19 +85,7 @@ export class BoardsServiceImpl (resolve, reject) => client.boardDetails(detailsReq, (err, resp) => { if (err) { - // Required cores are not installed manually: https://github.com/arduino/arduino-cli/issues/954 - if ( - (err.message.indexOf('missing platform release') !== -1 && - err.message.indexOf('referenced by board') !== -1) || - // Platform is not installed. - (err.message.indexOf('platform') !== -1 && - err.message.indexOf('not installed') !== -1) - ) { - resolve(undefined); - return; - } - // It's a hack to handle https://github.com/arduino/arduino-cli/issues/1262 gracefully. - if (err.message.indexOf('unknown package') !== -1) { + if (isMissingPlatformError(err)) { resolve(undefined); return; } @@ -249,26 +238,38 @@ export class BoardsServiceImpl const coreClient = await this.coreClient; const { client, instance } = coreClient; - const supportedUserFieldsReq = new SupportedUserFieldsRequest(); - supportedUserFieldsReq.setInstance(instance); - supportedUserFieldsReq.setFqbn(options.fqbn); - supportedUserFieldsReq.setProtocol(options.protocol); + const req = new SupportedUserFieldsRequest(); + req.setInstance(instance); + req.setFqbn(options.fqbn); + req.setProtocol(options.protocol); - const supportedUserFieldsResp = - await new Promise((resolve, reject) => { - client.supportedUserFields(supportedUserFieldsReq, (err, resp) => { - !!err ? reject(err) : resolve(resp); + const resp = await new Promise( + (resolve, reject) => { + client.supportedUserFields(req, (err, resp) => { + if (err) { + if (isMissingPlatformError(err)) { + resolve(undefined); + return; + } + reject(err); + return; + } + resolve(resp); }); - }); - return supportedUserFieldsResp.getUserFieldsList().map((e) => { - return { - toolId: e.getToolId(), - name: e.getName(), - label: e.getLabel(), - secret: e.getSecret(), - value: '', - }; - }); + } + ); + + if (!resp) { + return []; + } + + return resp.getUserFieldsList().map((e) => ({ + toolId: e.getToolId(), + name: e.getName(), + label: e.getLabel(), + secret: e.getSecret(), + value: '', + })); } async search(options: { query?: string }): Promise { @@ -486,3 +487,30 @@ export class BoardsServiceImpl console.info('<<< Boards package uninstallation done.', item); } } + +function isMissingPlatformError(error: unknown): boolean { + if (ServiceError.is(error)) { + const message = error.details; + // TODO: check gRPC status code? `9 FAILED_PRECONDITION` (https://grpc.github.io/grpc/core/md_doc_statuscodes.html) + + // When installing a 3rd party core that depends on a missing Arduino core. + // https://github.com/arduino/arduino-cli/issues/954 + if ( + message.includes('missing platform release') && + message.includes('referenced by board') + ) { + return true; + } + + // When the platform is not installed. + if (message.includes('platform') && message.includes('not installed')) { + return true; + } + + // It's a hack to handle https://github.com/arduino/arduino-cli/issues/1262 gracefully. + if (message.includes('unknown package')) { + return true; + } + } + return false; +}