@@ -41,6 +41,7 @@ import {
41
41
SupportedUserFieldsResponse ,
42
42
} from './cli-protocol/cc/arduino/cli/commands/v1/upload_pb' ;
43
43
import { ExecuteWithProgress } from './grpc-progressible' ;
44
+ import { ServiceError } from './service-error' ;
44
45
45
46
@injectable ( )
46
47
export class BoardsServiceImpl
@@ -84,19 +85,7 @@ export class BoardsServiceImpl
84
85
( resolve , reject ) =>
85
86
client . boardDetails ( detailsReq , ( err , resp ) => {
86
87
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 ) ) {
100
89
resolve ( undefined ) ;
101
90
return ;
102
91
}
@@ -249,26 +238,38 @@ export class BoardsServiceImpl
249
238
const coreClient = await this . coreClient ;
250
239
const { client, instance } = coreClient ;
251
240
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 ) ;
256
245
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 ) ;
261
258
} ) ;
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
+ } ) ) ;
272
273
}
273
274
274
275
async search ( options : { query ?: string } ) : Promise < BoardsPackage [ ] > {
@@ -486,3 +487,30 @@ export class BoardsServiceImpl
486
487
console . info ( '<<< Boards package uninstallation done.' , item ) ;
487
488
}
488
489
}
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