Skip to content

Commit 308841a

Browse files
committed
Add error handling to working commands
1 parent 95e2b9b commit 308841a

5 files changed

Lines changed: 50 additions & 30 deletions

File tree

cmd/meshexec/cli_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,23 +95,23 @@ func TestStatus_InvalidSince_IgnoredByParser(t *testing.T) {
9595
}
9696

9797
func TestJoinFlags_ParsingOnly(t *testing.T) {
98-
oldRun := joinCmd.Run
99-
joinCmd.Run = func(cmd *cobra.Command, args []string) {}
100-
defer func() { joinCmd.Run = oldRun }()
98+
oldRun := joinCmd.Run
99+
joinCmd.Run = func(cmd *cobra.Command, args []string) {}
100+
defer func() { joinCmd.Run = oldRun }()
101101

102-
execArgs(t, "join", "--foreground", "--scan-interval", "1500", "--advertise-interval", "2000")
103-
if !joinForeground || joinScanInterval != 1500 || joinAdvertiseInterval != 2000 {
104-
t.Fatalf("unexpected join flags: fg=%v scan=%d adv=%d", joinForeground, joinScanInterval, joinAdvertiseInterval)
105-
}
102+
execArgs(t, "join", "--foreground", "--scan-interval", "1500", "--advertise-interval", "2000")
103+
if !joinForeground || joinScanInterval != 1500 || joinAdvertiseInterval != 2000 {
104+
t.Fatalf("unexpected join flags: fg=%v scan=%d adv=%d", joinForeground, joinScanInterval, joinAdvertiseInterval)
105+
}
106106
}
107107

108108
func TestListFlags_ParsingOnly(t *testing.T) {
109-
oldRun := listCmd.Run
110-
listCmd.Run = func(cmd *cobra.Command, args []string) {}
111-
defer func() { listCmd.Run = oldRun }()
109+
oldRun := listCmd.Run
110+
listCmd.Run = func(cmd *cobra.Command, args []string) {}
111+
defer func() { listCmd.Run = oldRun }()
112112

113-
execArgs(t, "list", "--json", "--timeout", "2500")
114-
if !listJSON || listTimeoutMs != 2500 {
115-
t.Fatalf("unexpected list flags: json=%v timeout=%d", listJSON, listTimeoutMs)
116-
}
113+
execArgs(t, "list", "--json", "--timeout", "2500")
114+
if !listJSON || listTimeoutMs != 2500 {
115+
t.Fatalf("unexpected list flags: json=%v timeout=%d", listJSON, listTimeoutMs)
116+
}
117117
}

cmd/meshexec/config.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66

7+
"github.com/monster0506/meshexec/internal"
78
"github.com/monster0506/meshexec/internal/config"
89
"github.com/spf13/cobra"
910
)
@@ -33,7 +34,8 @@ var configShowCmd = &cobra.Command{
3334

3435
cfg, err := manager.Load()
3536
if err != nil {
36-
fmt.Fprintf(os.Stderr, "Error loading config: %v\n", err)
37+
me := internal.NewConfigError("invalid_config", "failed to load configuration", map[string]interface{}{"error": err.Error()})
38+
fmt.Fprintln(os.Stderr, internal.FormatUserError(me))
3739
os.Exit(1)
3840
}
3941

@@ -67,7 +69,8 @@ var configInitCmd = &cobra.Command{
6769

6870
err := manager.CreateDefaultConfig()
6971
if err != nil {
70-
fmt.Fprintf(os.Stderr, "Error creating config: %v\n", err)
72+
me := internal.NewConfigError("create_failed", "failed to create default configuration", map[string]interface{}{"error": err.Error()})
73+
fmt.Fprintln(os.Stderr, internal.FormatUserError(me))
7174
os.Exit(1)
7275
}
7376

@@ -99,7 +102,8 @@ var configValidateCmd = &cobra.Command{
99102
manager.SetConfigPath(configPath)
100103
}
101104
if _, err := manager.Load(); err != nil {
102-
fmt.Fprintf(os.Stderr, "Configuration invalid: %v\n", err)
105+
me := internal.NewConfigError("invalid_config", "configuration invalid", map[string]interface{}{"error": err.Error()})
106+
fmt.Fprintln(os.Stderr, internal.FormatUserError(me))
103107
os.Exit(1)
104108
}
105109
fmt.Println("Configuration is valid.")

cmd/meshexec/daemon.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package main
22

33
import (
44
"fmt"
5+
"os"
56

7+
"github.com/monster0506/meshexec/internal"
68
"github.com/monster0506/meshexec/internal/config"
79
"github.com/spf13/cobra"
810
)
@@ -40,6 +42,7 @@ var daemonCmd = &cobra.Command{
4042
"error": err,
4143
})
4244
}
45+
fmt.Fprintln(os.Stderr, internal.FormatUserError(internal.NewConfigError("invalid_config", "failed to load configuration", map[string]interface{}{"error": err.Error()})))
4346
}
4447

4548
fmt.Println("Agent daemon is not implemented yet. See tasks in tasks.md (section 8).")

cmd/meshexec/network.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import (
1313
"github.com/spf13/cobra"
1414
)
1515

16+
var (
17+
bleNewWithLogger = ble.NewWithLogger
18+
configNewManagerWithLevel = config.NewManagerWithLevel
19+
)
20+
1621
var joinCmd = &cobra.Command{
1722
Use: "join",
1823
Short: "Join the mesh network",
@@ -22,7 +27,7 @@ var joinCmd = &cobra.Command{
2227
if verbose {
2328
logLevel = "debug"
2429
}
25-
cfgMgr := config.NewManagerWithLevel(logLevel)
30+
cfgMgr := configNewManagerWithLevel(logLevel)
2631
cfgPath, _ := cmd.Root().PersistentFlags().GetString("config")
2732
if cfgPath != "" {
2833
cfgMgr.SetConfigPath(cfgPath)
@@ -34,9 +39,10 @@ var joinCmd = &cobra.Command{
3439
}
3540

3641
// Initialize BLE
37-
transport, err := ble.NewWithLogger(&cfg.Network, logger)
42+
transport, err := bleNewWithLogger(&cfg.Network, logger)
3843
if err != nil {
39-
fmt.Fprintf(os.Stderr, "BLE init error: %v\n", err)
44+
me := internal.MapNetworkError("scan", err)
45+
fmt.Fprintln(os.Stderr, internal.FormatUserError(me))
4046
os.Exit(1)
4147
}
4248
mgr := ble.NewManager(transport, logger)
@@ -54,7 +60,8 @@ var joinCmd = &cobra.Command{
5460
ctx, cancel := context.WithCancel(context.Background())
5561
defer cancel()
5662
if err := mgr.StartDiscovery(ctx); err != nil {
57-
fmt.Fprintf(os.Stderr, "Discovery error: %v\n", err)
63+
me := internal.MapNetworkError("scan", err)
64+
fmt.Fprintln(os.Stderr, internal.FormatUserError(me))
5865
os.Exit(1)
5966
}
6067

@@ -85,7 +92,7 @@ var listCmd = &cobra.Command{
8592
if verbose {
8693
logLevel = "debug"
8794
}
88-
cfgMgr := config.NewManagerWithLevel(logLevel)
95+
cfgMgr := configNewManagerWithLevel(logLevel)
8996
cfgPath, _ := cmd.Root().PersistentFlags().GetString("config")
9097
if cfgPath != "" {
9198
cfgMgr.SetConfigPath(cfgPath)
@@ -96,17 +103,19 @@ var listCmd = &cobra.Command{
96103
os.Exit(1)
97104
}
98105

99-
transport, err := ble.NewWithLogger(&cfg.Network, logger)
106+
transport, err := bleNewWithLogger(&cfg.Network, logger)
100107
if err != nil {
101-
fmt.Fprintf(os.Stderr, "BLE init error: %v\n", err)
108+
me := internal.MapNetworkError("scan", err)
109+
fmt.Fprintln(os.Stderr, internal.FormatUserError(me))
102110
os.Exit(1)
103111
}
104112
mgr := ble.NewManager(transport, logger)
105113

106114
ctx, cancel := context.WithTimeout(context.Background(), timeout)
107115
defer cancel()
108116
if err := mgr.StartDiscovery(ctx); err != nil {
109-
fmt.Fprintf(os.Stderr, "Discovery error: %v\n", err)
117+
me := internal.MapNetworkError("scan", err)
118+
fmt.Fprintln(os.Stderr, internal.FormatUserError(me))
110119
os.Exit(1)
111120
}
112121
<-ctx.Done()
@@ -158,7 +167,7 @@ var statusCmd = &cobra.Command{
158167
if verbose {
159168
logLevel = "debug"
160169
}
161-
cfgMgr := config.NewManagerWithLevel(logLevel)
170+
cfgMgr := configNewManagerWithLevel(logLevel)
162171
cfgPath, _ := cmd.Root().PersistentFlags().GetString("config")
163172
if cfgPath != "" {
164173
cfgMgr.SetConfigPath(cfgPath)
@@ -170,9 +179,10 @@ var statusCmd = &cobra.Command{
170179
}
171180

172181
// Initialize BLE and manager
173-
transport, err := ble.NewWithLogger(&cfg.Network, logger)
182+
transport, err := bleNewWithLogger(&cfg.Network, logger)
174183
if err != nil {
175-
fmt.Fprintf(os.Stderr, "BLE init error: %v\n", err)
184+
me := internal.MapNetworkError("scan", err)
185+
fmt.Fprintln(os.Stderr, internal.FormatUserError(me))
176186
os.Exit(1)
177187
}
178188
mgr := ble.NewManager(transport, logger)
@@ -181,7 +191,8 @@ var statusCmd = &cobra.Command{
181191
ctx, cancel := context.WithTimeout(context.Background(), timeout)
182192
defer cancel()
183193
if err := mgr.StartDiscovery(ctx); err != nil {
184-
fmt.Fprintf(os.Stderr, "Discovery error: %v\n", err)
194+
me := internal.MapNetworkError("scan", err)
195+
fmt.Fprintln(os.Stderr, internal.FormatUserError(me))
185196
os.Exit(1)
186197
}
187198
<-ctx.Done()

cmd/meshexec/run.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"strings"
77

8+
"github.com/monster0506/meshexec/internal"
89
"github.com/monster0506/meshexec/internal/config"
910
"github.com/monster0506/meshexec/internal/executor"
1011
"github.com/monster0506/meshexec/internal/messages"
@@ -42,7 +43,8 @@ var runCmd = &cobra.Command{
4243
}
4344
cfg, err := cfgMgr.Load()
4445
if err != nil {
45-
fmt.Fprintf(os.Stderr, "Error loading config: %v\n", err)
46+
me := internal.NewConfigError("invalid_config", "failed to load configuration", map[string]interface{}{"error": err.Error()})
47+
fmt.Fprintln(os.Stderr, internal.FormatUserError(me))
4648
os.Exit(1)
4749
}
4850

0 commit comments

Comments
 (0)