Skip to content

Commit a478b4a

Browse files
authored
[breaking] Refactor errors handling in packagemanager.Load* golang API (#1682)
* Removed `error` return from `discovery.New(...)` The `New` function never fails. * Replaced *status.Status with errors in packagamanager * Apply suggestions from code review
1 parent 12d488f commit a478b4a

File tree

10 files changed

+159
-129
lines changed

10 files changed

+159
-129
lines changed

arduino/cores/packagemanager/loader.go

+69-92
Large diffs are not rendered by default.

arduino/cores/packagemanager/loader_test.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ func TestLoadDiscoveries(t *testing.T) {
140140
"pluggable_discovery.required": "arduino:ble-discovery",
141141
})
142142

143-
errs := packageManager.LoadDiscoveries()
144-
require.Len(t, errs, 2)
145-
require.Equal(t, errs[0].Message(), "discovery not found: builtin:serial-discovery")
146-
require.Equal(t, errs[1].Message(), "discovery not found: builtin:mdns-discovery")
143+
err := packageManager.LoadDiscoveries()
144+
require.Len(t, err, 2)
145+
require.Equal(t, err[0].Error(), "discovery builtin:serial-discovery not found")
146+
require.Equal(t, err[1].Error(), "discovery builtin:mdns-discovery not found")
147147
discoveries := packageManager.DiscoveryManager().IDs()
148148
require.Len(t, discoveries, 1)
149149
require.Contains(t, discoveries, "arduino:ble-discovery")
@@ -155,10 +155,10 @@ func TestLoadDiscoveries(t *testing.T) {
155155
"pluggable_discovery.required.1": "arduino:serial-discovery",
156156
})
157157

158-
errs = packageManager.LoadDiscoveries()
159-
require.Len(t, errs, 2)
160-
require.Equal(t, errs[0].Message(), "discovery not found: builtin:serial-discovery")
161-
require.Equal(t, errs[1].Message(), "discovery not found: builtin:mdns-discovery")
158+
err = packageManager.LoadDiscoveries()
159+
require.Len(t, err, 2)
160+
require.Equal(t, err[0].Error(), "discovery builtin:serial-discovery not found")
161+
require.Equal(t, err[1].Error(), "discovery builtin:mdns-discovery not found")
162162
discoveries = packageManager.DiscoveryManager().IDs()
163163
require.Len(t, discoveries, 2)
164164
require.Contains(t, discoveries, "arduino:ble-discovery")
@@ -172,10 +172,10 @@ func TestLoadDiscoveries(t *testing.T) {
172172
"pluggable_discovery.teensy.pattern": "\"{runtime.tools.teensy_ports.path}/hardware/tools/teensy_ports\" -J2",
173173
})
174174

175-
errs = packageManager.LoadDiscoveries()
176-
require.Len(t, errs, 2)
177-
require.Equal(t, errs[0].Message(), "discovery not found: builtin:serial-discovery")
178-
require.Equal(t, errs[1].Message(), "discovery not found: builtin:mdns-discovery")
175+
err = packageManager.LoadDiscoveries()
176+
require.Len(t, err, 2)
177+
require.Equal(t, err[0].Error(), "discovery builtin:serial-discovery not found")
178+
require.Equal(t, err[1].Error(), "discovery builtin:mdns-discovery not found")
179179
discoveries = packageManager.DiscoveryManager().IDs()
180180
require.Len(t, discoveries, 3)
181181
require.Contains(t, discoveries, "arduino:ble-discovery")
@@ -191,10 +191,10 @@ func TestLoadDiscoveries(t *testing.T) {
191191
"pluggable_discovery.teensy.pattern": "\"{runtime.tools.teensy_ports.path}/hardware/tools/teensy_ports\" -J2",
192192
})
193193

194-
errs = packageManager.LoadDiscoveries()
195-
require.Len(t, errs, 2)
196-
require.Equal(t, errs[0].Message(), "discovery not found: builtin:serial-discovery")
197-
require.Equal(t, errs[1].Message(), "discovery not found: builtin:mdns-discovery")
194+
err = packageManager.LoadDiscoveries()
195+
require.Len(t, err, 2)
196+
require.Equal(t, err[0].Error(), "discovery builtin:serial-discovery not found")
197+
require.Equal(t, err[1].Error(), "discovery builtin:mdns-discovery not found")
198198
discoveries = packageManager.DiscoveryManager().IDs()
199199
require.Len(t, discoveries, 3)
200200
require.Contains(t, discoveries, "arduino:ble-discovery")

arduino/discovery/discovery.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,13 @@ type Event struct {
126126
}
127127

128128
// New create and connect to the given pluggable discovery
129-
func New(id string, args ...string) (*PluggableDiscovery, error) {
130-
disc := &PluggableDiscovery{
129+
func New(id string, args ...string) *PluggableDiscovery {
130+
return &PluggableDiscovery{
131131
id: id,
132132
processArgs: args,
133133
state: Dead,
134134
cachedPorts: map[string]*Port{},
135135
}
136-
return disc, nil
137136
}
138137

139138
// GetID returns the identifier for this discovery

arduino/discovery/discovery_client/main.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ func main() {
3232
discoveries := []*discovery.PluggableDiscovery{}
3333
discEvent := make(chan *discovery.Event)
3434
for _, discCmd := range os.Args[1:] {
35-
disc, err := discovery.New("", discCmd)
36-
if err != nil {
37-
log.Fatal("Error initializing discovery:", err)
38-
}
39-
35+
disc := discovery.New("", discCmd)
4036
if err := disc.Run(); err != nil {
4137
log.Fatal("Error starting discovery:", err)
4238
}

arduino/discovery/discovery_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ func TestDiscoveryStdioHandling(t *testing.T) {
3232
require.NoError(t, builder.Run())
3333

3434
// Run cat and test if streaming json works as expected
35-
disc, err := New("test", "testdata/cat/cat") // copy stdin to stdout
36-
require.NoError(t, err)
35+
disc := New("test", "testdata/cat/cat") // copy stdin to stdout
3736

3837
err = disc.runProcess()
3938
require.NoError(t, err)

arduino/errors.go

+18
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,24 @@ func (e *PlatformNotFoundError) Unwrap() error {
338338
return e.Cause
339339
}
340340

341+
// PlatformLoadingError is returned when a platform has fatal errors that prevents loading
342+
type PlatformLoadingError struct {
343+
Cause error
344+
}
345+
346+
func (e *PlatformLoadingError) Error() string {
347+
return composeErrorMsg(tr("Error loading hardware platform"), e.Cause)
348+
}
349+
350+
// ToRPCStatus converts the error into a *status.Status
351+
func (e *PlatformLoadingError) ToRPCStatus() *status.Status {
352+
return status.New(codes.FailedPrecondition, e.Error())
353+
}
354+
355+
func (e *PlatformLoadingError) Unwrap() error {
356+
return e.Cause
357+
}
358+
341359
// LibraryNotFoundError is returned when a platform is not found
342360
type LibraryNotFoundError struct {
343361
Library string

commands/instances.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,10 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
233233
// otherwise we wouldn't find them and reinstall them each time
234234
// and they would never get reloaded.
235235
for _, err := range instance.PackageManager.LoadHardware() {
236+
s := &arduino.PlatformLoadingError{Cause: err}
236237
responseCallback(&rpc.InitResponse{
237238
Message: &rpc.InitResponse_Error{
238-
Error: err.Proto(),
239+
Error: s.ToRPCStatus().Proto(),
239240
},
240241
})
241242
}
@@ -296,18 +297,20 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
296297
// We installed at least one new tool after loading hardware
297298
// so we must reload again otherwise we would never found them.
298299
for _, err := range instance.PackageManager.LoadHardware() {
300+
s := &arduino.PlatformLoadingError{Cause: err}
299301
responseCallback(&rpc.InitResponse{
300302
Message: &rpc.InitResponse_Error{
301-
Error: err.Proto(),
303+
Error: s.ToRPCStatus().Proto(),
302304
},
303305
})
304306
}
305307
}
306308

307309
for _, err := range instance.PackageManager.LoadDiscoveries() {
310+
s := &arduino.PlatformLoadingError{Cause: err}
308311
responseCallback(&rpc.InitResponse{
309312
Message: &rpc.InitResponse_Error{
310-
Error: err.Proto(),
313+
Error: s.ToRPCStatus().Proto(),
311314
},
312315
})
313316
}

docs/UPGRADING.md

+40
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,46 @@
22

33
Here you can find a list of migration guides to handle breaking changes between releases of the CLI.
44

5+
## 0.22.0
6+
7+
### `packagemanager.Load*` functions now returns `error` instead of `*status.Status`
8+
9+
The following functions signature:
10+
11+
```go
12+
func (pm *PackageManager) LoadHardware() []*status.Status { ... }
13+
func (pm *PackageManager) LoadHardwareFromDirectories(hardwarePaths paths.PathList) []*status.Status { ... }
14+
func (pm *PackageManager) LoadHardwareFromDirectory(path *paths.Path) []*status.Status { ... }
15+
func (pm *PackageManager) LoadToolsFromBundleDirectories(dirs paths.PathList) []*status.Status { ... }
16+
func (pm *PackageManager) LoadDiscoveries() []*status.Status { ... }
17+
```
18+
19+
have been changed to:
20+
21+
```go
22+
func (pm *PackageManager) LoadHardware() []error { ... }
23+
func (pm *PackageManager) LoadHardwareFromDirectories(hardwarePaths paths.PathList) []error { ... }
24+
func (pm *PackageManager) LoadHardwareFromDirectory(path *paths.Path) []error { ... }
25+
func (pm *PackageManager) LoadToolsFromBundleDirectories(dirs paths.PathList) []error { ... }
26+
func (pm *PackageManager) LoadDiscoveries() []error { ... }
27+
```
28+
29+
These function no longer returns a gRPC status, so the errors can be handled as any other `error`.
30+
31+
### Removed `error` return from `discovery.New(...)` function
32+
33+
The `discovery.New(...)` function never fails, so the error has been removed, the old signature:
34+
35+
```go
36+
func New(id string, args ...string) (*PluggableDiscovery, error) { ... }
37+
```
38+
39+
is now:
40+
41+
```go
42+
func New(id string, args ...string) *PluggableDiscovery { ... }
43+
```
44+
545
## 0.21.0
646

747
### `packagemanager.NewPackageManager` function change

legacy/builder/hardware_loader.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,16 @@ func (s *HardwareLoader) Run(ctx *types.Context) error {
2727
// This should happen only on legacy arduino-builder.
2828
// Hopefully this piece will be removed once the legacy package will be cleanedup.
2929
pm := packagemanager.NewPackageManager(nil, nil, nil, nil, "arduino-builder")
30-
if errs := pm.LoadHardwareFromDirectories(ctx.HardwareDirs); len(errs) > 0 {
30+
errs := pm.LoadHardwareFromDirectories(ctx.HardwareDirs)
31+
if ctx.Verbose {
3132
// With the refactoring of the initialization step of the CLI we changed how
3233
// errors are returned when loading platforms and libraries, that meant returning a list of
3334
// errors instead of a single one to enhance the experience for the user.
3435
// I have no intention right now to start a refactoring of the legacy package too, so
3536
// here's this shitty solution for now.
3637
// When we're gonna refactor the legacy package this will be gone.
37-
38-
if ctx.Verbose {
39-
for _, err := range errs {
40-
ctx.Info(tr("Error loading hardware platform: %[1]s", err.Message()))
41-
}
38+
for _, err := range errs {
39+
ctx.Info(tr("Error loading hardware platform: %[1]s", err.Error()))
4240
}
4341
}
4442
ctx.PackageManager = pm

test/test_core.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ def test_core_with_wrong_custom_board_options_is_loaded(run_command, data_dir):
759759
assert "arduino-beta-dev:platform_with_wrong_custom_board_options:altra" in boards
760760
# Verify warning is shown to user
761761
assert (
762-
"Error initializing instance: "
762+
"Error initializing instance: Error loading hardware platform: "
763763
+ "loading platform release arduino-beta-dev:[email protected]: "
764764
+ "loading boards: "
765765
+ "skipping loading of boards arduino-beta-dev:platform_with_wrong_custom_board_options:nessuno: "
@@ -797,7 +797,7 @@ def test_core_with_missing_custom_board_options_is_loaded(run_command, data_dir)
797797
assert "arduino-beta-dev:platform_with_missing_custom_board_options:altra" in boards
798798
# Verify warning is shown to user
799799
assert (
800-
"Error initializing instance: "
800+
"Error initializing instance: Error loading hardware platform: "
801801
+ "loading platform release arduino-beta-dev:[email protected]: "
802802
+ "loading boards: "
803803
+ "skipping loading of boards arduino-beta-dev:platform_with_missing_custom_board_options:nessuno: "

0 commit comments

Comments
 (0)