diff --git a/pkg/clierrors/common.go b/pkg/clierrors/common.go index 7bf2065..7a2f3a7 100644 --- a/pkg/clierrors/common.go +++ b/pkg/clierrors/common.go @@ -5,11 +5,3 @@ type Interrupted struct{} func (e Interrupted) Error() string { return "interrupted" } - -type UnknownCommand struct { - Command string -} - -func (e UnknownCommand) Error() string { - return "unknown command: " + e.Command -} diff --git a/pkg/clierrors/common_test.go b/pkg/clierrors/common_test.go index 5619829..3ea3979 100644 --- a/pkg/clierrors/common_test.go +++ b/pkg/clierrors/common_test.go @@ -2,15 +2,6 @@ package clierrors import "testing" -func TestUnknownCommand_Error(t *testing.T) { - command := "test" - err := UnknownCommand{Command: command} - expected := "unknown command: " + command - - if err.Error() != expected { - t.Errorf("Expected error message '%s', but got '%s'", expected, err.Error()) - } -} func TestInterrupted_Error(t *testing.T) { err := Interrupted{} expected := "interrupted" diff --git a/pkg/clierrors/network.go b/pkg/clierrors/network.go deleted file mode 100644 index 4ee25cf..0000000 --- a/pkg/clierrors/network.go +++ /dev/null @@ -1,13 +0,0 @@ -package clierrors - -type ConnectionClosed struct{} - -func (e ConnectionClosed) Error() string { - return "connection closed" -} - -type Timeout struct{} - -func (e Timeout) Error() string { - return "timeout" -} diff --git a/pkg/clierrors/network_test.go b/pkg/clierrors/network_test.go deleted file mode 100644 index aaad9ec..0000000 --- a/pkg/clierrors/network_test.go +++ /dev/null @@ -1,21 +0,0 @@ -package clierrors - -import "testing" - -func TestConnectionClosed_Error(t *testing.T) { - err := ConnectionClosed{} - want := "connection closed" - - if got := err.Error(); got != want { - t.Errorf("Error() = %v, want %v", got, want) - } -} - -func TestTimeout_Error(t *testing.T) { - err := Timeout{} - want := "timeout" - - if got := err.Error(); got != want { - t.Errorf("Error() = %v, want %v", got, want) - } -} diff --git a/pkg/command/commands.go b/pkg/command/commands.go index 456cf23..d1f8b77 100644 --- a/pkg/command/commands.go +++ b/pkg/command/commands.go @@ -48,7 +48,7 @@ type Executer interface { // If the command is not recognized, an error is returned. func Factory(raw string, macro *Macro) (Executer, error) { if raw == "" { - return nil, fmt.Errorf("empty command") + return nil, &ErrEmptyCommand{} } parts := strings.SplitN(raw, " ", CommandPartsNumber) @@ -66,7 +66,7 @@ func Factory(raw string, macro *Macro) (Executer, error) { return NewEdit(content), nil case "send": if len(parts) == 1 { - return nil, fmt.Errorf("empty request") + return nil, &ErrEmptyRequest{} } return NewSend(parts[1]), nil @@ -76,7 +76,7 @@ func Factory(raw string, macro *Macro) (Executer, error) { if len(parts) > 1 { sec, err := strconv.Atoi(parts[1]) if err != nil || sec < 0 { - return nil, fmt.Errorf("invalid timeout: %s", err) + return nil, &ErrInvalidTimeout{parts[1]} } timeout = time.Duration(sec) * time.Second @@ -88,7 +88,7 @@ func Factory(raw string, macro *Macro) (Executer, error) { return macro.Get(cmd) } - return nil, fmt.Errorf("unknown command: %s", cmd) + return nil, &ErrUnknownCommand{cmd} } } @@ -131,7 +131,7 @@ func NewSend(request string) *Send { func (c *Send) Execute(exCtx ExecutionContext) (Executer, error) { msg, err := exCtx.Connection().Send(c.request) if err != nil { - return nil, fmt.Errorf("fail to send request: %s", err) + return nil, err } return NewPrintMsg(*msg), nil @@ -153,16 +153,16 @@ func (c *PrintMsg) Execute(exCtx ExecutionContext) (Executer, error) { output, err := exCtx.Formater().FormatMessage(msg) if err != nil { - return nil, fmt.Errorf("fail to format for output file: %s, data: %q", err, msg.Data) + return nil, err } switch msg.Type { case ws.Request: - color.New(color.FgGreen).Fprint(exCtx.Output(), "->\n") + color.New(color.FgGreen).Fprintln(exCtx.Output(), "->") case ws.Response: - color.New(color.FgRed).Fprint(exCtx.Output(), "<-\n") + color.New(color.FgRed).Fprintln(exCtx.Output(), "<-") default: - return nil, fmt.Errorf("unknown message type: %s, data: %q", msg.Type, msg.Data) + return nil, &ErrUnsupportedMessageType{msg.Type.String()} } fmt.Fprintf(exCtx.Output(), "%s\n", output) @@ -170,10 +170,13 @@ func (c *PrintMsg) Execute(exCtx ExecutionContext) (Executer, error) { if exCtx.OutputFile() != nil { output, err := exCtx.Formater().FormatForFile(msg) if err != nil { - return nil, fmt.Errorf("fail to write to output file: %s", err) + return nil, err } - fmt.Fprintln(exCtx.OutputFile(), output) + _, err = fmt.Fprintln(exCtx.OutputFile(), output) + if err != nil { + return nil, err + } } return nil, nil @@ -207,7 +210,7 @@ func (c *WaitForResp) Execute(exCtx ExecutionContext) (Executer, error) { if c.timeout.Seconds() == 0 { msg, ok := <-exCtx.Connection().Messages() if !ok { - return nil, fmt.Errorf("connection closed") + return nil, &ErrConnectionClosed{} } return NewPrintMsg(msg), nil @@ -215,10 +218,10 @@ func (c *WaitForResp) Execute(exCtx ExecutionContext) (Executer, error) { select { case <-time.After(c.timeout): - return nil, fmt.Errorf("timeout") + return nil, &ErrTimeout{} case msg, ok := <-exCtx.Connection().Messages(): if !ok { - return nil, fmt.Errorf("connection closed") + return nil, &ErrConnectionClosed{} } return NewPrintMsg(msg), nil diff --git a/pkg/command/macro.go b/pkg/command/macro.go index 1ff7668..cade9c2 100644 --- a/pkg/command/macro.go +++ b/pkg/command/macro.go @@ -37,7 +37,7 @@ func NewMacro(domains []string) *Macro { // Otherwise, it creates a new Sequence with the commands and adds it to the macro. func (m *Macro) AddCommands(name string, rawCommands []string) error { if _, ok := m.macro[name]; ok { - return fmt.Errorf("macro already exists: %s", name) + return &ErrDuplicateMacro{name} } commands := []Executer{} @@ -53,7 +53,7 @@ func (m *Macro) AddCommands(name string, rawCommands []string) error { switch len(commands) { case 0: - return fmt.Errorf("empty macro: %s", name) + return ErrEmptyMacro{name} case 1: m.macro[name] = commands[0] default: @@ -68,7 +68,7 @@ func (m *Macro) AddCommands(name string, rawCommands []string) error { func (m *Macro) merge(macro *Macro) error { for name, cmd := range macro.macro { if _, ok := m.macro[name]; ok { - return fmt.Errorf("duplicate macro name: %s", name) + return &ErrDuplicateMacro{name} } m.macro[name] = cmd @@ -83,7 +83,7 @@ func (m *Macro) Get(name string) (Executer, error) { return cmd, nil } - return nil, fmt.Errorf("unknown command: %s", name) + return nil, &ErrUnknownCommand{name} } func (m *Macro) GetNames() []string { @@ -110,7 +110,7 @@ func LoadFromFile(path string) (*Macro, error) { } if cfg.Version != "1" { - return nil, fmt.Errorf("unsupported macro file version: %s", path) + return nil, &ErrUnsupportedVersion{cfg.Version} } macroCfg := NewMacro(cfg.Domains) @@ -137,7 +137,6 @@ func LoadMacroForDomain(macroDir, domain string) (*Macro, error) { for _, file := range files { if file.IsDir() || (!strings.HasSuffix(file.Name(), ".yaml") && !strings.HasSuffix(file.Name(), ".yml")) { - fmt.Println("skip file", file.Name()) continue } diff --git a/pkg/command/macro_test.go b/pkg/command/macro_test.go index 40e6ded..4caf4d3 100644 --- a/pkg/command/macro_test.go +++ b/pkg/command/macro_test.go @@ -363,8 +363,8 @@ macro: t.Fatalf("LoadFromFile() error = %v, want non-nil", err) } - if err.Error() != "unsupported macro file version: "+tempFile.Name() { - t.Errorf("LoadFromFile() error = %v, want %v", err.Error(), "unsupported macro file version: "+tempFile.Name()) + if err.Error() != (&ErrUnsupportedVersion{"2"}).Error() { + t.Errorf("LoadFromFile() error = %v, want %v", err.Error(), &ErrUnsupportedVersion{"2"}) } }