Handle missing core when getting board user fields

Closes #1142

Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
This commit is contained in:
Akos Kitta 2022-08-16 09:18:50 +02:00 committed by Akos Kitta
parent 5be1f9d7fe
commit d674ab9b73

View File

@ -41,6 +41,7 @@ import {
SupportedUserFieldsResponse, SupportedUserFieldsResponse,
} from './cli-protocol/cc/arduino/cli/commands/v1/upload_pb'; } from './cli-protocol/cc/arduino/cli/commands/v1/upload_pb';
import { ExecuteWithProgress } from './grpc-progressible'; import { ExecuteWithProgress } from './grpc-progressible';
import { ServiceError } from './service-error';
@injectable() @injectable()
export class BoardsServiceImpl export class BoardsServiceImpl
@ -84,19 +85,7 @@ export class BoardsServiceImpl
(resolve, reject) => (resolve, reject) =>
client.boardDetails(detailsReq, (err, resp) => { client.boardDetails(detailsReq, (err, resp) => {
if (err) { if (err) {
// Required cores are not installed manually: https://github.com/arduino/arduino-cli/issues/954 if (isMissingPlatformError(err)) {
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) {
resolve(undefined); resolve(undefined);
return; return;
} }
@ -249,26 +238,38 @@ export class BoardsServiceImpl
const coreClient = await this.coreClient; const coreClient = await this.coreClient;
const { client, instance } = coreClient; const { client, instance } = coreClient;
const supportedUserFieldsReq = new SupportedUserFieldsRequest(); const req = new SupportedUserFieldsRequest();
supportedUserFieldsReq.setInstance(instance); req.setInstance(instance);
supportedUserFieldsReq.setFqbn(options.fqbn); req.setFqbn(options.fqbn);
supportedUserFieldsReq.setProtocol(options.protocol); req.setProtocol(options.protocol);
const supportedUserFieldsResp = const resp = await new Promise<SupportedUserFieldsResponse | undefined>(
await new Promise<SupportedUserFieldsResponse>((resolve, reject) => { (resolve, reject) => {
client.supportedUserFields(supportedUserFieldsReq, (err, resp) => { client.supportedUserFields(req, (err, resp) => {
!!err ? reject(err) : resolve(resp); if (err) {
if (isMissingPlatformError(err)) {
resolve(undefined);
return;
}
reject(err);
return;
}
resolve(resp);
}); });
}); }
return supportedUserFieldsResp.getUserFieldsList().map((e) => { );
return {
toolId: e.getToolId(), if (!resp) {
name: e.getName(), return [];
label: e.getLabel(), }
secret: e.getSecret(),
value: '', return resp.getUserFieldsList().map((e) => ({
}; toolId: e.getToolId(),
}); name: e.getName(),
label: e.getLabel(),
secret: e.getSecret(),
value: '',
}));
} }
async search(options: { query?: string }): Promise<BoardsPackage[]> { async search(options: { query?: string }): Promise<BoardsPackage[]> {
@ -486,3 +487,30 @@ export class BoardsServiceImpl
console.info('<<< Boards package uninstallation done.', item); 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;
}