Skip to content

feat: specialize init errors #2076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions arduino/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func (e *MissingProgrammerError) ToRPCStatus() *status.Status {
return status.New(codes.InvalidArgument, e.Error())
}

// ProgrammerRequiredForUploadError is returned then the upload can be done only using a programmer
// ProgrammerRequiredForUploadError is returned when the upload can be done only using a programmer
type ProgrammerRequiredForUploadError struct{}

func (e *ProgrammerRequiredForUploadError) Error() string {
Expand All @@ -308,6 +308,28 @@ func (e *ProgrammerRequiredForUploadError) ToRPCStatus() *status.Status {
return st
}

// InitFailedError is returned when the instance initialization fails
type InitFailedError struct {
Code codes.Code
Cause error
Reason rpc.FailedInstanceInitReason
}

func (ife *InitFailedError) Error() string {
return ife.Cause.Error()
}

// ToRPCStatus converts the error into a *status.Status
func (ife *InitFailedError) ToRPCStatus() *status.Status {
st, _ := status.
New(ife.Code, ife.Cause.Error()).
WithDetails(&rpc.FailedInstanceInitError{
Reason: ife.Reason,
Message: ife.Cause.Error(),
})
return st
}

// ProgrammerNotFoundError is returned when the programmer is not found
type ProgrammerNotFoundError struct {
Programmer string
Expand Down Expand Up @@ -405,7 +427,9 @@ func (e *PlatformLoadingError) Error() string {

// ToRPCStatus converts the error into a *status.Status
func (e *PlatformLoadingError) ToRPCStatus() *status.Status {
return status.New(codes.FailedPrecondition, e.Error())
s, _ := status.New(codes.FailedPrecondition, e.Error()).
WithDetails(&rpc.PlatformLoadingError{})
return s
}

func (e *PlatformLoadingError) Unwrap() error {
Expand Down
40 changes: 30 additions & 10 deletions commands/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,23 +303,35 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
for _, u := range urls {
URL, err := utils.URLParse(u)
if err != nil {
s := status.Newf(codes.InvalidArgument, tr("Invalid additional URL: %v"), err)
responseError(s)
e := &arduino.InitFailedError{
Code: codes.InvalidArgument,
Cause: fmt.Errorf(tr("Invalid additional URL: %v", err)),
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_INVALID_INDEX_URL,
}
responseError(e.ToRPCStatus())
continue
}

if URL.Scheme == "file" {
_, err := pmb.LoadPackageIndexFromFile(paths.New(URL.Path))
if err != nil {
s := status.Newf(codes.FailedPrecondition, tr("Loading index file: %v"), err)
responseError(s)
e := &arduino.InitFailedError{
Code: codes.FailedPrecondition,
Cause: fmt.Errorf(tr("Loading index file: %v", err)),
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_INDEX_LOAD_ERROR,
}
responseError(e.ToRPCStatus())
}
continue
}

if err := pmb.LoadPackageIndex(URL); err != nil {
s := status.Newf(codes.FailedPrecondition, tr("Loading index file: %v"), err)
responseError(s)
e := &arduino.InitFailedError{
Code: codes.FailedPrecondition,
Cause: fmt.Errorf(tr("Loading index file: %v", err)),
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_INDEX_LOAD_ERROR,
}
responseError(e.ToRPCStatus())
}
}

Expand All @@ -331,8 +343,12 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
for name, tool := range pmb.GetOrCreatePackage("builtin").Tools {
latest := tool.LatestRelease()
if latest == nil {
s := status.Newf(codes.Internal, tr("can't find latest release of tool %s", name))
responseError(s)
e := &arduino.InitFailedError{
Code: codes.Internal,
Cause: fmt.Errorf(tr("can't find latest release of tool %s", name)),
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_TOOL_LOAD_ERROR,
}
responseError(e.ToRPCStatus())
} else if !latest.IsInstalled() {
builtinToolsToInstall = append(builtinToolsToInstall, latest)
}
Expand All @@ -342,8 +358,12 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
if len(builtinToolsToInstall) > 0 {
for _, toolRelease := range builtinToolsToInstall {
if err := installTool(pmb.Build(), toolRelease, downloadCallback, taskCallback); err != nil {
s := status.Newf(codes.Internal, err.Error())
responseError(s)
e := &arduino.InitFailedError{
Code: codes.Internal,
Cause: err,
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_TOOL_LOAD_ERROR,
}
responseError(e.ToRPCStatus())
}
}

Expand Down
Loading