Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5e251e8

Browse filesBrowse files
authored
feat: specialize init errors (#2076)
1 parent c660b33 commit 5e251e8
Copy full SHA for 5e251e8

File tree

Expand file treeCollapse file tree

6 files changed

+1045
-773
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+1045
-773
lines changed

‎arduino/errors.go

Copy file name to clipboardExpand all lines: arduino/errors.go
+26-2Lines changed: 26 additions & 2 deletions
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,28 @@ func (e *ProgrammerRequiredForUploadError) ToRPCStatus() *status.Status {
308308
return st
309309
}
310310

311+
// InitFailedError is returned when the instance initialization fails
312+
type InitFailedError struct {
313+
Code codes.Code
314+
Cause error
315+
Reason rpc.FailedInstanceInitReason
316+
}
317+
318+
func (ife *InitFailedError) Error() string {
319+
return ife.Cause.Error()
320+
}
321+
322+
// ToRPCStatus converts the error into a *status.Status
323+
func (ife *InitFailedError) ToRPCStatus() *status.Status {
324+
st, _ := status.
325+
New(ife.Code, ife.Cause.Error()).
326+
WithDetails(&rpc.FailedInstanceInitError{
327+
Reason: ife.Reason,
328+
Message: ife.Cause.Error(),
329+
})
330+
return st
331+
}
332+
311333
// ProgrammerNotFoundError is returned when the programmer is not found
312334
type ProgrammerNotFoundError struct {
313335
Programmer string
@@ -405,7 +427,9 @@ func (e *PlatformLoadingError) Error() string {
405427

406428
// ToRPCStatus converts the error into a *status.Status
407429
func (e *PlatformLoadingError) ToRPCStatus() *status.Status {
408-
return status.New(codes.FailedPrecondition, e.Error())
430+
s, _ := status.New(codes.FailedPrecondition, e.Error()).
431+
WithDetails(&rpc.PlatformLoadingError{})
432+
return s
409433
}
410434

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

‎commands/instances.go

Copy file name to clipboardExpand all lines: commands/instances.go
+30-10Lines changed: 30 additions & 10 deletions
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: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_INVALID_INDEX_URL,
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: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_INDEX_LOAD_ERROR,
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: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_INDEX_LOAD_ERROR,
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: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_TOOL_LOAD_ERROR,
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: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_TOOL_LOAD_ERROR,
365+
}
366+
responseError(e.ToRPCStatus())
347367
}
348368
}
349369

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.