Skip to content

Commit dc0054c

Browse files
author
Luca Bianconi
committed
feat: specialize init errors
1 parent 53a6f25 commit dc0054c

File tree

6 files changed

+954
-762
lines changed

6 files changed

+954
-762
lines changed

Diff for: arduino/errors.go

+39-2
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func (e *MissingProgrammerError) ToRPCStatus() *status.Status {
293293
return status.New(codes.InvalidArgument, e.Error())
294294
}
295295

296-
// ProgrammerRequiredForUploadError is returned then the upload can be done only using a programmer
296+
// ProgrammerRequiredForUploadError is returned when the upload can be done only using a programmer
297297
type ProgrammerRequiredForUploadError struct{}
298298

299299
func (e *ProgrammerRequiredForUploadError) Error() string {
@@ -308,6 +308,41 @@ func (e *ProgrammerRequiredForUploadError) ToRPCStatus() *status.Status {
308308
return st
309309
}
310310

311+
type FailedInstanceInitReason string
312+
313+
const (
314+
// Unspecified the error reason is not specialized
315+
Unspecified FailedInstanceInitReason = "UNSPECIFIED"
316+
// InvalidIndexURL a package index url is malformed
317+
InvalidIndexURL FailedInstanceInitReason = "INVALID_INDEX_URL"
318+
// ErrorIndexLoad failure encountered while loading an index
319+
ErrorIndexLoad FailedInstanceInitReason = "INDEX_LOAD_ERROR"
320+
// ErrorToolLoad failure encountered while loading a tool
321+
ErrorToolLoad FailedInstanceInitReason = "TOOL_LOAD_ERROR"
322+
)
323+
324+
// InitFailedError is returned when the instance initialization fails
325+
type InitFailedError struct {
326+
Code codes.Code
327+
Cause error
328+
Reason FailedInstanceInitReason
329+
}
330+
331+
func (ife *InitFailedError) Error() string {
332+
return ife.Cause.Error()
333+
}
334+
335+
// ToRPCStatus converts the error into a *status.Status
336+
func (ife *InitFailedError) ToRPCStatus() *status.Status {
337+
st, _ := status.
338+
New(ife.Code, ife.Cause.Error()).
339+
WithDetails(&rpc.FailedInstanceInitError{
340+
Reason: string(ife.Reason),
341+
Message: ife.Cause.Error(),
342+
})
343+
return st
344+
}
345+
311346
// ProgrammerNotFoundError is returned when the programmer is not found
312347
type ProgrammerNotFoundError struct {
313348
Programmer string
@@ -405,7 +440,9 @@ func (e *PlatformLoadingError) Error() string {
405440

406441
// ToRPCStatus converts the error into a *status.Status
407442
func (e *PlatformLoadingError) ToRPCStatus() *status.Status {
408-
return status.New(codes.FailedPrecondition, e.Error())
443+
s, _ := status.New(codes.FailedPrecondition, e.Error()).
444+
WithDetails(&rpc.PlatformLoadingError{})
445+
return s
409446
}
410447

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

Diff for: commands/instances.go

+30-10
Original file line numberDiff line numberDiff line change
@@ -303,23 +303,35 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
303303
for _, u := range urls {
304304
URL, err := utils.URLParse(u)
305305
if err != nil {
306-
s := status.Newf(codes.InvalidArgument, tr("Invalid additional URL: %v"), err)
307-
responseError(s)
306+
e := &arduino.InitFailedError{
307+
Code: codes.InvalidArgument,
308+
Cause: fmt.Errorf(tr("Invalid additional URL: %v", err)),
309+
Reason: arduino.InvalidIndexURL,
310+
}
311+
responseError(e.ToRPCStatus())
308312
continue
309313
}
310314

311315
if URL.Scheme == "file" {
312316
_, err := pmb.LoadPackageIndexFromFile(paths.New(URL.Path))
313317
if err != nil {
314-
s := status.Newf(codes.FailedPrecondition, tr("Loading index file: %v"), err)
315-
responseError(s)
318+
e := &arduino.InitFailedError{
319+
Code: codes.FailedPrecondition,
320+
Cause: fmt.Errorf(tr("Loading index file: %v", err)),
321+
Reason: arduino.ErrorIndexLoad,
322+
}
323+
responseError(e.ToRPCStatus())
316324
}
317325
continue
318326
}
319327

320328
if err := pmb.LoadPackageIndex(URL); err != nil {
321-
s := status.Newf(codes.FailedPrecondition, tr("Loading index file: %v"), err)
322-
responseError(s)
329+
e := &arduino.InitFailedError{
330+
Code: codes.FailedPrecondition,
331+
Cause: fmt.Errorf(tr("Loading index file: %v", err)),
332+
Reason: arduino.ErrorIndexLoad,
333+
}
334+
responseError(e.ToRPCStatus())
323335
}
324336
}
325337

@@ -331,8 +343,12 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
331343
for name, tool := range pmb.GetOrCreatePackage("builtin").Tools {
332344
latest := tool.LatestRelease()
333345
if latest == nil {
334-
s := status.Newf(codes.Internal, tr("can't find latest release of tool %s", name))
335-
responseError(s)
346+
e := &arduino.InitFailedError{
347+
Code: codes.Internal,
348+
Cause: fmt.Errorf(tr("can't find latest release of tool %s", name)),
349+
Reason: arduino.ErrorToolLoad,
350+
}
351+
responseError(e.ToRPCStatus())
336352
} else if !latest.IsInstalled() {
337353
builtinToolsToInstall = append(builtinToolsToInstall, latest)
338354
}
@@ -342,8 +358,12 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
342358
if len(builtinToolsToInstall) > 0 {
343359
for _, toolRelease := range builtinToolsToInstall {
344360
if err := installTool(pmb.Build(), toolRelease, downloadCallback, taskCallback); err != nil {
345-
s := status.Newf(codes.Internal, err.Error())
346-
responseError(s)
361+
e := &arduino.InitFailedError{
362+
Code: codes.Internal,
363+
Cause: err,
364+
Reason: arduino.ErrorToolLoad,
365+
}
366+
responseError(e.ToRPCStatus())
347367
}
348368
}
349369

0 commit comments

Comments
 (0)