Skip to content

Commit 90068f4

Browse files
per1234cmaglie
authored andcommitted
Replace custom error with protobuf message
Previously, a custom error was returned when attempting to upgrade a platform that was already at the latest available version. There is dedicated code for handling this specific error. Now that the function has been changed to return a status.Status instead of error, the previous check for the return being this error is no longer possible. The capability is restored by replacing the error with a protocol buffer message.
1 parent 9204eda commit 90068f4

File tree

4 files changed

+184
-131
lines changed

4 files changed

+184
-131
lines changed

Diff for: cli/core/upgrade.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
9494
}
9595

9696
_, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress())
97-
if err.Message() == core.ErrAlreadyLatest.Error() {
97+
if d := err.Details(); len(d) > 0 && d[0].(*rpc.AlreadyAtLatestVersionError) != nil {
9898
feedback.Printf("Platform %s is already at the latest version", platformRef)
9999
} else if err != nil {
100100
feedback.Errorf("Error during upgrade: %v", err)

Diff for: commands/core/upgrade.go

+12-16
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ package core
1717

1818
import (
1919
"context"
20-
"errors"
21-
"fmt"
2220

2321
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2422
"github.com/arduino/arduino-cli/commands"
@@ -27,12 +25,6 @@ import (
2725
"google.golang.org/grpc/status"
2826
)
2927

30-
var (
31-
// ErrAlreadyLatest is returned when an upgrade is not possible because
32-
// already at latest version.
33-
ErrAlreadyLatest = errors.New("platform already at latest version")
34-
)
35-
3628
// PlatformUpgrade FIXMEDOC
3729
func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeRequest,
3830
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) (*rpc.PlatformUpgradeResponse, *status.Status) {
@@ -48,7 +40,7 @@ func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeRequest,
4840
PlatformArchitecture: req.Architecture,
4941
}
5042
if err := upgradePlatform(pm, ref, downloadCB, taskCB, req.GetSkipPostInstall()); err != nil {
51-
return nil, status.Convert(err)
43+
return nil, err
5244
}
5345

5446
status := commands.Init(&rpc.InitRequest{Instance: req.Instance}, nil)
@@ -61,33 +53,37 @@ func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeRequest,
6153

6254
func upgradePlatform(pm *packagemanager.PackageManager, platformRef *packagemanager.PlatformReference,
6355
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB,
64-
skipPostInstall bool) error {
56+
skipPostInstall bool) *status.Status {
6557
if platformRef.PlatformVersion != nil {
66-
return fmt.Errorf("upgrade doesn't accept parameters with version")
58+
return status.New(codes.InvalidArgument, "upgrade doesn't accept parameters with version")
6759
}
6860

6961
// Search the latest version for all specified platforms
7062
platform := pm.FindPlatform(platformRef)
7163
if platform == nil {
72-
return fmt.Errorf("platform %s not found", platformRef)
64+
return status.Newf(codes.InvalidArgument, "platform %s not found", platformRef)
7365
}
7466
installed := pm.GetInstalledPlatformRelease(platform)
7567
if installed == nil {
76-
return fmt.Errorf("platform %s is not installed", platformRef)
68+
return status.Newf(codes.InvalidArgument, "platform %s is not installed", platformRef)
7769
}
7870
latest := platform.GetLatestRelease()
7971
if !latest.Version.GreaterThan(installed.Version) {
80-
return ErrAlreadyLatest
72+
status, e := status.New(codes.AlreadyExists, "platform already at latest version").WithDetails(&rpc.AlreadyAtLatestVersionError{})
73+
if e != nil { // should never happen
74+
panic(e)
75+
}
76+
return status
8177
}
8278
platformRef.PlatformVersion = latest.Version
8379

8480
platformRelease, tools, err := pm.FindPlatformReleaseDependencies(platformRef)
8581
if err != nil {
86-
return fmt.Errorf("platform %s is not installed", platformRef)
82+
return status.Newf(codes.FailedPrecondition, "platform %s is not installed", platformRef)
8783
}
8884
err = installPlatform(pm, platformRelease, tools, downloadCB, taskCB, skipPostInstall)
8985
if err != nil {
90-
return err
86+
return status.Convert(err)
9187
}
9288

9389
return nil

0 commit comments

Comments
 (0)