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

Sign up
Appearance settings

Commit 52f3cb2

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

File tree

6 files changed

+955
-762
lines changed

6 files changed

+955
-762
lines changed

‎arduino/errors.go‎

Lines changed: 40 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,42 @@ func (e *ProgrammerRequiredForUploadError) ToRPCStatus() *status.Status {
308308
return st
309309
}
310310

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

406442
// ToRPCStatus converts the error into a *status.Status
407443
func (e *PlatformLoadingError) ToRPCStatus() *status.Status {
408-
return status.New(codes.FailedPrecondition, e.Error())
444+
s, _ := status.New(codes.FailedPrecondition, e.Error()).
445+
WithDetails(&rpc.PlatformLoadingError{})
446+
return s
409447
}
410448

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

‎commands/instances.go‎

Lines 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: 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)

AltStyle によって変換されたページ (->オリジナル) /