Skip to content

Commit cbf5187

Browse files
committed
feat: added details to all init errors
1 parent 736d161 commit cbf5187

File tree

4 files changed

+345
-318
lines changed

4 files changed

+345
-318
lines changed

Diff for: arduino/errors.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,11 @@ func (e *PlatformLoadingError) Error() string {
427427

428428
// ToRPCStatus converts the error into a *status.Status
429429
func (e *PlatformLoadingError) ToRPCStatus() *status.Status {
430-
s, _ := status.New(codes.FailedPrecondition, e.Error()).
431-
WithDetails(&rpc.PlatformLoadingError{})
432-
return s
430+
return (&InitFailedError{
431+
Code: codes.FailedPrecondition,
432+
Cause: e.Cause,
433+
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_PLATFORM_LOAD_ERROR,
434+
}).ToRPCStatus()
433435
}
434436

435437
func (e *PlatformLoadingError) Unwrap() error {

Diff for: commands/instances.go

+45-42
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ func Create(req *rpc.CreateRequest, extraUserAgent ...string) (*rpc.CreateRespon
192192
}, nil
193193
}
194194

195+
type rpcStatusConverter interface {
196+
ToRPCStatus() *status.Status
197+
}
198+
195199
// Init loads installed libraries and Platforms in CoreInstance with specified ID,
196200
// a gRPC status error is returned if the CoreInstance doesn't exist.
197201
// All responses are sent through responseCallback, can be nil to ignore all responses.
@@ -214,10 +218,11 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
214218
if responseCallback == nil {
215219
responseCallback = func(r *rpc.InitResponse) {}
216220
}
217-
responseError := func(st *status.Status) {
221+
222+
responseError := func(e rpcStatusConverter) {
218223
responseCallback(&rpc.InitResponse{
219224
Message: &rpc.InitResponse_Error{
220-
Error: st.Proto(),
225+
Error: e.ToRPCStatus().Proto(),
221226
},
222227
})
223228
}
@@ -278,17 +283,19 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
278283
// Load Platforms
279284
if profile == nil {
280285
for _, err := range pmb.LoadHardware() {
281-
s := &arduino.PlatformLoadingError{Cause: err}
282-
responseError(s.ToRPCStatus())
286+
responseError(&arduino.InitFailedError{
287+
Code: codes.Internal,
288+
Cause: err,
289+
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_TOOL_LOAD_ERROR,
290+
})
283291
}
284292
} else {
285293
// Load platforms from profile
286294
errs := pmb.LoadHardwareForProfile(
287295
profile, true, downloadCallback, taskCallback,
288296
)
289297
for _, err := range errs {
290-
s := &arduino.PlatformLoadingError{Cause: err}
291-
responseError(s.ToRPCStatus())
298+
responseError(&arduino.PlatformLoadingError{Cause: err})
292299
}
293300

294301
// Load "builtin" tools
@@ -303,35 +310,28 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
303310
for _, u := range urls {
304311
URL, err := utils.URLParse(u)
305312
if err != nil {
306-
e := &arduino.InitFailedError{
313+
responseError(&arduino.InitFailedError{
307314
Code: codes.InvalidArgument,
308315
Cause: fmt.Errorf(tr("Invalid additional URL: %v", err)),
309316
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_INVALID_INDEX_URL,
310-
}
311-
responseError(e.ToRPCStatus())
317+
})
312318
continue
313319
}
314320

321+
var loadFunc func(*url.URL) error = pmb.LoadPackageIndex
315322
if URL.Scheme == "file" {
316-
_, err := pmb.LoadPackageIndexFromFile(paths.New(URL.Path))
317-
if err != nil {
318-
e := &arduino.InitFailedError{
319-
Code: codes.FailedPrecondition,
320-
Cause: fmt.Errorf(tr("Loading index file: %v", err)),
321-
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_INDEX_LOAD_ERROR,
322-
}
323-
responseError(e.ToRPCStatus())
323+
loadFunc = func(u *url.URL) error {
324+
_, err := pmb.LoadPackageIndexFromFile(paths.New(URL.Path))
325+
return err
324326
}
325-
continue
326327
}
327328

328-
if err := pmb.LoadPackageIndex(URL); err != nil {
329-
e := &arduino.InitFailedError{
329+
if err := loadFunc(URL); err != nil {
330+
responseError(&arduino.InitFailedError{
330331
Code: codes.FailedPrecondition,
331332
Cause: fmt.Errorf(tr("Loading index file: %v", err)),
332333
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_INDEX_LOAD_ERROR,
333-
}
334-
responseError(e.ToRPCStatus())
334+
})
335335
}
336336
}
337337

@@ -343,12 +343,11 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
343343
for name, tool := range pmb.GetOrCreatePackage("builtin").Tools {
344344
latest := tool.LatestRelease()
345345
if latest == nil {
346-
e := &arduino.InitFailedError{
346+
responseError(&arduino.InitFailedError{
347347
Code: codes.Internal,
348348
Cause: fmt.Errorf(tr("can't find latest release of tool %s", name)),
349349
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_TOOL_LOAD_ERROR,
350-
}
351-
responseError(e.ToRPCStatus())
350+
})
352351
} else if !latest.IsInstalled() {
353352
builtinToolsToInstall = append(builtinToolsToInstall, latest)
354353
}
@@ -358,20 +357,22 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
358357
if len(builtinToolsToInstall) > 0 {
359358
for _, toolRelease := range builtinToolsToInstall {
360359
if err := installTool(pmb.Build(), toolRelease, downloadCallback, taskCallback); err != nil {
361-
e := &arduino.InitFailedError{
360+
responseError(&arduino.InitFailedError{
362361
Code: codes.Internal,
363362
Cause: err,
364363
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_TOOL_LOAD_ERROR,
365-
}
366-
responseError(e.ToRPCStatus())
364+
})
367365
}
368366
}
369367

370368
// We installed at least one builtin tool after loading hardware
371369
// so we must reload again otherwise we would never found them.
372370
for _, err := range loadBuiltinTools() {
373-
s := &arduino.PlatformLoadingError{Cause: err}
374-
responseError(s.ToRPCStatus())
371+
responseError(&arduino.InitFailedError{
372+
Code: codes.Internal,
373+
Cause: err,
374+
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_TOOL_LOAD_ERROR,
375+
})
375376
}
376377
}
377378

@@ -382,8 +383,7 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
382383
defer release()
383384

384385
for _, err := range pme.LoadDiscoveries() {
385-
s := &arduino.PlatformLoadingError{Cause: err}
386-
responseError(s.ToRPCStatus())
386+
responseError(&arduino.PlatformLoadingError{Cause: err})
387387
}
388388

389389
// Create library manager and add libraries directories
@@ -403,8 +403,11 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
403403
}
404404

405405
if err := lm.LoadIndex(); err != nil {
406-
s := status.Newf(codes.FailedPrecondition, tr("Loading index file: %v"), err)
407-
responseError(s)
406+
responseError(&arduino.InitFailedError{
407+
Code: codes.FailedPrecondition,
408+
Cause: fmt.Errorf(tr("Loading index file: %v", err)),
409+
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_INDEX_LOAD_ERROR,
410+
})
408411
}
409412

410413
if profile == nil {
@@ -431,14 +434,12 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
431434
})
432435
if libRelease == nil {
433436
taskCallback(&rpc.TaskProgress{Name: tr("Library %s not found", libraryRef)})
434-
err := &arduino.LibraryNotFoundError{Library: libraryRef.Library}
435-
responseError(err.ToRPCStatus())
437+
responseError(&arduino.LibraryNotFoundError{Library: libraryRef.Library})
436438
continue
437439
}
438440
if err := libRelease.Resource.Download(lm.DownloadsDir, nil, libRelease.String(), downloadCallback, ""); err != nil {
439441
taskCallback(&rpc.TaskProgress{Name: tr("Error downloading library %s", libraryRef)})
440-
e := &arduino.FailedLibraryInstallError{Cause: err}
441-
responseError(e.ToRPCStatus())
442+
responseError(&arduino.FailedLibraryInstallError{Cause: err})
442443
continue
443444
}
444445
taskCallback(&rpc.TaskProgress{Completed: true})
@@ -447,8 +448,7 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
447448
taskCallback(&rpc.TaskProgress{Name: tr("Installing library %s", libraryRef)})
448449
if err := libRelease.Resource.Install(lm.DownloadsDir, libRoot, libDir); err != nil {
449450
taskCallback(&rpc.TaskProgress{Name: tr("Error installing library %s", libraryRef)})
450-
e := &arduino.FailedLibraryInstallError{Cause: err}
451-
responseError(e.ToRPCStatus())
451+
responseError(&arduino.FailedLibraryInstallError{Cause: err})
452452
continue
453453
}
454454
taskCallback(&rpc.TaskProgress{Completed: true})
@@ -459,8 +459,11 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
459459
}
460460

461461
for _, err := range lm.RescanLibraries() {
462-
s := status.Newf(codes.FailedPrecondition, tr("Loading libraries: %v"), err)
463-
responseError(s)
462+
responseError(&arduino.InitFailedError{
463+
Code: codes.FailedPrecondition,
464+
Cause: fmt.Errorf(tr("Loading libraries: %v"), err),
465+
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_LIBRARY_LOAD_ERROR,
466+
})
464467
}
465468

466469
// Refreshes the locale used, this will change the

0 commit comments

Comments
 (0)