Skip to content

Commit

Permalink
Refactor command package errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Dec 2, 2023
1 parent 46ff7e1 commit 748f584
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 73 deletions.
8 changes: 0 additions & 8 deletions pkg/clierrors/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 0 additions & 9 deletions pkg/clierrors/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 0 additions & 13 deletions pkg/clierrors/network.go

This file was deleted.

21 changes: 0 additions & 21 deletions pkg/clierrors/network_test.go

This file was deleted.

31 changes: 17 additions & 14 deletions pkg/command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Check failure on line 51 in pkg/command/commands.go

View workflow job for this annotation

GitHub Actions / tests

undefined: ErrEmptyCommand
}

parts := strings.SplitN(raw, " ", CommandPartsNumber)
Expand All @@ -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{}

Check failure on line 69 in pkg/command/commands.go

View workflow job for this annotation

GitHub Actions / tests

undefined: ErrEmptyRequest
}

return NewSend(parts[1]), nil
Expand All @@ -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]}

Check failure on line 79 in pkg/command/commands.go

View workflow job for this annotation

GitHub Actions / tests

undefined: ErrInvalidTimeout
}

timeout = time.Duration(sec) * time.Second
Expand All @@ -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}

Check failure on line 91 in pkg/command/commands.go

View workflow job for this annotation

GitHub Actions / tests

undefined: ErrUnknownCommand
}
}

Expand Down Expand Up @@ -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
Expand All @@ -153,27 +153,30 @@ 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()}

Check failure on line 165 in pkg/command/commands.go

View workflow job for this annotation

GitHub Actions / tests

undefined: ErrUnsupportedMessageType
}

fmt.Fprintf(exCtx.Output(), "%s\n", output)

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
Expand Down Expand Up @@ -207,18 +210,18 @@ 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{}

Check failure on line 213 in pkg/command/commands.go

View workflow job for this annotation

GitHub Actions / tests

undefined: ErrConnectionClosed
}

return NewPrintMsg(msg), nil
}

select {
case <-time.After(c.timeout):
return nil, fmt.Errorf("timeout")
return nil, &ErrTimeout{}

Check failure on line 221 in pkg/command/commands.go

View workflow job for this annotation

GitHub Actions / tests

undefined: ErrTimeout
case msg, ok := <-exCtx.Connection().Messages():
if !ok {
return nil, fmt.Errorf("connection closed")
return nil, &ErrConnectionClosed{}

Check failure on line 224 in pkg/command/commands.go

View workflow job for this annotation

GitHub Actions / tests

undefined: ErrConnectionClosed
}

return NewPrintMsg(msg), nil
Expand Down
11 changes: 5 additions & 6 deletions pkg/command/macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Check failure on line 40 in pkg/command/macro.go

View workflow job for this annotation

GitHub Actions / tests

undefined: ErrDuplicateMacro
}

commands := []Executer{}
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/command/macro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
}
}

Expand Down

0 comments on commit 748f584

Please sign in to comment.