From 076099cb90505a8741a43352a796c2d78b1491c2 Mon Sep 17 00:00:00 2001 From: Dustin Strobel Date: Tue, 14 Nov 2023 18:42:57 +0100 Subject: [PATCH] feat: replace errors with custom errors --- connection/connection.go | 10 +++++----- connection/connection_test.go | 4 ++-- connection/ssh.go | 6 +++--- connection/ssh_test.go | 7 +++---- connection/winrm.go | 4 ++-- connection/winrm_test.go | 7 +++---- 6 files changed, 18 insertions(+), 20 deletions(-) diff --git a/connection/connection.go b/connection/connection.go index 87f6f13..4c3ee76 100644 --- a/connection/connection.go +++ b/connection/connection.go @@ -30,10 +30,10 @@ func New(conf *Config) (*Connection, error) { // Assert WinRM and SSH configuration if conf.WinRM == nil && conf.SSH == nil { - return nil, winerror.Errorf(winerror.ConfigError, "Connection: object 'WinRMConfig' or 'SSHConfig' must be set") + return nil, winerror.Errorf(winerror.ConfigError, "connection client: Connection object 'WinRMConfig' or 'SSHConfig' must be set") } if conf.WinRM != nil && conf.SSH != nil { - return nil, winerror.Errorf(winerror.ConfigError, "Connection: object must only contain 'WinRMConfig' or 'SSHConfig'") + return nil, winerror.Errorf(winerror.ConfigError, "connection client: Connection object must only contain 'WinRMConfig' or 'SSHConfig'") } // Allocate a new Connection @@ -43,7 +43,7 @@ func New(conf *Config) (*Connection, error) { if conf.WinRM != nil { winRMClient, err := newWinRMClient(conf.WinRM) if err != nil { - return nil, winerror.Errorf(winerror.ConnectionError, "WinRM: %s", err) + return nil, err } c = &Connection{ @@ -55,7 +55,7 @@ func New(conf *Config) (*Connection, error) { if conf.SSH != nil { sshClient, err := newSSHClient(conf.SSH) if err != nil { - return nil, winerror.Errorf(winerror.ConnectionError, "SSH: %s", err) + return nil, err } c = &Connection{ @@ -71,7 +71,7 @@ func (c *Connection) Close() error { if c.SSH != nil { err := c.SSH.Close() if err != nil { - return winerror.Errorf(winerror.ConnectionError, "Connection: %s", err) + return winerror.Errorf(winerror.ConnectionError, "connection client: %s", err) } } diff --git a/connection/connection_test.go b/connection/connection_test.go index a7a5bd3..be5f6f4 100644 --- a/connection/connection_test.go +++ b/connection/connection_test.go @@ -13,7 +13,7 @@ func TestNewConnectionErrorMessages(t *testing.T) { _, err := New(conf) assert.Error(t, err) - assert.Contains(t, err.Error(), "Connection: object 'WinRMConfig' or 'SSHConfig' must be set") + assert.Contains(t, err.Error(), "connection client: Connection object 'WinRMConfig' or 'SSHConfig' must be set") }) t.Run("Error - Both WinRM and SSH", func(t *testing.T) { @@ -37,6 +37,6 @@ func TestNewConnectionErrorMessages(t *testing.T) { _, err := New(conf) assert.Error(t, err) - assert.Contains(t, err.Error(), "Connection: object must only contain 'WinRMConfig' or 'SSHConfig'") + assert.Contains(t, err.Error(), "connection client: Connection object must only contain 'WinRMConfig' or 'SSHConfig'") }) } diff --git a/connection/ssh.go b/connection/ssh.go index e5fb29f..88a4a10 100644 --- a/connection/ssh.go +++ b/connection/ssh.go @@ -2,10 +2,10 @@ package connection import ( "context" - "errors" "fmt" "io" + "github.com/d-strobel/gowindows/winerror" "golang.org/x/crypto/ssh" ) @@ -25,7 +25,7 @@ func newSSHClient(config *SSHConfig) (*ssh.Client, error) { // Assert if config.SSHHost == "" || config.SSHUsername == "" || config.SSHPassword == "" { - return nil, errors.New("SSHHost, SSHUsername, and SSHPassword must be set") + return nil, winerror.Errorf(winerror.ConfigError, "ssh client: SSHConfig parameter 'SSHHost', 'SSHUsername' and 'SSHPassword' must be set") } // Parse ssh host string @@ -44,7 +44,7 @@ func newSSHClient(config *SSHConfig) (*ssh.Client, error) { // Connect to the remote server and perform the SSH handshake. client, err := ssh.Dial("tcp", sshHost, sshConfig) if err != nil { - return nil, err + return nil, winerror.Errorf(winerror.ConnectionError, "ssh client: %s", err) } return client, nil diff --git a/connection/ssh_test.go b/connection/ssh_test.go index 672522c..c069821 100644 --- a/connection/ssh_test.go +++ b/connection/ssh_test.go @@ -2,6 +2,8 @@ package connection import ( "testing" + + "github.com/stretchr/testify/assert" ) func TestNewSSHClient(t *testing.T) { @@ -17,9 +19,6 @@ func TestNewSSHClient(t *testing.T) { t.Error("Expected a nil SSH client, but got non-nil") } - expectedErrorMsg := "SSHHost, SSHUsername, and SSHPassword must be set" - if err.Error() != expectedErrorMsg { - t.Errorf("Expected error message '%s', but got '%s'", expectedErrorMsg, err.Error()) - } + assert.Contains(t, err.Error(), "ssh client: SSHConfig parameter 'SSHHost', 'SSHUsername' and 'SSHPassword' must be set") }) } diff --git a/connection/winrm.go b/connection/winrm.go index d06cb8f..2a64dc3 100644 --- a/connection/winrm.go +++ b/connection/winrm.go @@ -1,9 +1,9 @@ package connection import ( - "errors" "time" + "github.com/d-strobel/gowindows/winerror" "github.com/masterzen/winrm" ) @@ -31,7 +31,7 @@ func newWinRMClient(config *WinRMConfig) (*winrm.Client, error) { // Assert if config.WinRMHost == "" || config.WinRMUsername == "" || config.WinRMPassword == "" { - return nil, errors.New("WinRMHost, WinRMUsername, and WinRMPassword must be set") + return nil, winerror.Errorf(winerror.ConfigError, "winrm client: WinRMConfig parameter 'WinRMHost', 'WinRMUsername', and 'WinRMPassword' must be set") } // Set default values diff --git a/connection/winrm_test.go b/connection/winrm_test.go index 538cec4..4e1634c 100644 --- a/connection/winrm_test.go +++ b/connection/winrm_test.go @@ -2,6 +2,8 @@ package connection import ( "testing" + + "github.com/stretchr/testify/assert" ) func TestNewWinRMClient(t *testing.T) { @@ -41,10 +43,7 @@ func TestNewWinRMClient(t *testing.T) { t.Error("Expected a nil client, but got non-nil") } - expectedErrorMsg := "WinRMHost, WinRMUsername, and WinRMPassword must be set" - if err.Error() != expectedErrorMsg { - t.Errorf("Expected error message '%s', but got '%s'", expectedErrorMsg, err.Error()) - } + assert.Contains(t, err.Error(), "winrm client: WinRMConfig parameter 'WinRMHost', 'WinRMUsername', and 'WinRMPassword' must be set") }) }