Skip to content

Commit 4a464aa

Browse files
committed
Developing error handling
1 parent c519cdb commit 4a464aa

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

errors.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/brokenCursor/usb-modem-cli/drivers"
7+
)
8+
9+
type DriverSupportError struct {
10+
Driver drivers.BaseModem
11+
Function string
12+
}
13+
14+
// func (e DriverSupportError) Unwrap() error {
15+
// return e.Err
16+
// }
17+
18+
func (e DriverSupportError) Error() string {
19+
return fmt.Sprintf("driver \"%s\" does not support %s", e.Driver.GetModel(), e.Function)
20+
}

main.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

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

67
"github.com/brokenCursor/usb-modem-cli/drivers"
78

@@ -36,9 +37,15 @@ func init() {
3637
}
3738

3839
func main() {
40+
if err := run(); err != nil {
41+
fmt.Fprintf(os.Stderr, "error: %v\n", err)
42+
}
43+
}
44+
45+
func run() error {
3946
parser := arg.MustParse(&args)
4047
model := "8810FT"
41-
48+
4249
modem, err := drivers.GetModemDriver(model, "192.168.0.1")
4350
if err != nil {
4451
panic("failed to get drivers")
@@ -47,14 +54,26 @@ func main() {
4754
switch {
4855
case args.Connection != nil:
4956
err := validate.Struct(args.Connection)
50-
57+
5158
cell, ok := modem.(drivers.ModemCell)
5259
if !ok {
53-
fmt.Printf("Modem %s does not support cell connection", modem.GetModel())
54-
return
60+
return DriverSupportError{Driver: modem, Function: "cell connection"}
5561
}
5662

57-
err, err := cell.GetCellConnStatus()
63+
switch args.Connection.Action {
64+
case "status":
65+
isConnected, err := cell.GetCellConnStatus()
66+
67+
if err != nil {
68+
fmt.Println("Failed to get connection status")
69+
return
70+
}
71+
if isConnected {
72+
fmt.Println("Status: up")
73+
} else {
74+
fmt.Println("Status: down")
75+
}
76+
}
5877

5978
if err != nil {
6079
parser.FailSubcommand("Unknown action", "conn")
@@ -65,4 +84,6 @@ func main() {
6584
}
6685

6786
fmt.Printf("Modem cmd: %s\n", args.Ip)
87+
88+
return nil
6889
}

0 commit comments

Comments
 (0)