Skip to content

Commit 66c0fe0

Browse files
committed
Merge branch 'master' into fix/postinstall-output
2 parents 66778a2 + 5e251e8 commit 66c0fe0

File tree

7 files changed

+1054
-776
lines changed

7 files changed

+1054
-776
lines changed

Diff for: arduino/cores/packagemanager/package_manager_test.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net/url"
2222
"os"
2323
"runtime"
24+
"strings"
2425
"sync"
2526
"testing"
2627

@@ -706,7 +707,12 @@ func TestRunPostInstall(t *testing.T) {
706707
wg.Wait()
707708
os.Stdout = prevStdout
708709
os.Stderr = prevStderr
709-
710-
require.Equal(t, "sent in stdout\n", string(stdout))
711-
require.Equal(t, "sent in stderr\n", string(stderr))
710+
stdoutS := string(stdout)
711+
stderrS := string(stderr)
712+
stdoutS = strings.TrimSuffix(stdoutS, "\n")
713+
stdoutS = strings.TrimSuffix(stdoutS, "\r")
714+
stderrS = strings.TrimSuffix(stderrS, "\n")
715+
stderrS = strings.TrimSuffix(stderrS, "\r")
716+
require.Equal(t, "sent in stdout", stdoutS)
717+
require.Equal(t, "sent in stderr", stderrS)
712718
}

Diff for: arduino/errors.go

+26-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,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 {

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