Skip to content

Commit

Permalink
feat: replace errors with custom errors
Browse files Browse the repository at this point in the history
  • Loading branch information
d-strobel committed Nov 14, 2023
1 parent f4935f3 commit 076099c
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 20 deletions.
10 changes: 5 additions & 5 deletions connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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)
}
}

Expand Down
4 changes: 2 additions & 2 deletions connection/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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'")
})
}
6 changes: 3 additions & 3 deletions connection/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package connection

import (
"context"
"errors"
"fmt"
"io"

"github.com/d-strobel/gowindows/winerror"
"golang.org/x/crypto/ssh"
)

Expand All @@ -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
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions connection/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package connection

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewSSHClient(t *testing.T) {
Expand All @@ -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")
})
}
4 changes: 2 additions & 2 deletions connection/winrm.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package connection

import (
"errors"
"time"

"github.com/d-strobel/gowindows/winerror"
"github.com/masterzen/winrm"
)

Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions connection/winrm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package connection

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewWinRMClient(t *testing.T) {
Expand Down Expand Up @@ -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")
})
}

Expand Down

0 comments on commit 076099c

Please sign in to comment.