Skip to content

Commit

Permalink
Merge pull request #306 from thaJeztah/err_checks
Browse files Browse the repository at this point in the history
move trimming whitespace to error-check helpers
  • Loading branch information
thaJeztah authored Jan 10, 2024
2 parents 8fc3306 + 6a3e64c commit f642c26
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
17 changes: 5 additions & 12 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ func isValidCredsMessage(msg string) error {
if credentials.IsCredentialsMissingServerURLMessage(msg) {
return credentials.NewErrCredentialsMissingServerURL()
}

if credentials.IsCredentialsMissingUsernameMessage(msg) {
return credentials.NewErrCredentialsMissingUsername()
}

return nil
}

Expand All @@ -36,13 +34,10 @@ func Store(program ProgramFunc, creds *credentials.Credentials) error {

out, err := cmd.Output()
if err != nil {
t := strings.TrimSpace(string(out))

if isValidErr := isValidCredsMessage(t); isValidErr != nil {
if isValidErr := isValidCredsMessage(string(out)); isValidErr != nil {
err = isValidErr
}

return fmt.Errorf("error storing credentials - err: %v, out: `%s`", err, t)
return fmt.Errorf("error storing credentials - err: %v, out: `%s`", err, strings.TrimSpace(string(out)))
}

return nil
Expand All @@ -55,17 +50,15 @@ func Get(program ProgramFunc, serverURL string) (*credentials.Credentials, error

out, err := cmd.Output()
if err != nil {
t := strings.TrimSpace(string(out))

if credentials.IsErrCredentialsNotFoundMessage(t) {
if credentials.IsErrCredentialsNotFoundMessage(string(out)) {
return nil, credentials.NewErrCredentialsNotFound()
}

if isValidErr := isValidCredsMessage(t); isValidErr != nil {
if isValidErr := isValidCredsMessage(string(out)); isValidErr != nil {
err = isValidErr
}

return nil, fmt.Errorf("error getting credentials - err: %v, out: `%s`", err, t)
return nil, fmt.Errorf("error getting credentials - err: %v, out: `%s`", err, strings.TrimSpace(string(out)))
}

resp := &credentials.Credentials{
Expand Down
11 changes: 7 additions & 4 deletions credentials/error.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package credentials

import "errors"
import (
"errors"
"strings"
)

const (
// ErrCredentialsNotFound standardizes the not found error, so every helper returns
Expand Down Expand Up @@ -47,7 +50,7 @@ func IsErrCredentialsNotFound(err error) bool {
// This function helps to check messages returned by an
// external program via its standard output.
func IsErrCredentialsNotFoundMessage(err string) bool {
return err == errCredentialsNotFoundMessage
return strings.TrimSpace(err) == errCredentialsNotFoundMessage
}

// errCredentialsMissingServerURL represents an error raised
Expand Down Expand Up @@ -104,7 +107,7 @@ func IsCredentialsMissingServerURL(err error) bool {
// IsCredentialsMissingServerURLMessage checks for an
// errCredentialsMissingServerURL in the error message.
func IsCredentialsMissingServerURLMessage(err string) bool {
return err == errCredentialsMissingServerURLMessage
return strings.TrimSpace(err) == errCredentialsMissingServerURLMessage
}

// IsCredentialsMissingUsername returns true if the error
Expand All @@ -117,5 +120,5 @@ func IsCredentialsMissingUsername(err error) bool {
// IsCredentialsMissingUsernameMessage checks for an
// errCredentialsMissingUsername in the error message.
func IsCredentialsMissingUsernameMessage(err string) bool {
return err == errCredentialsMissingUsernameMessage
return strings.TrimSpace(err) == errCredentialsMissingUsernameMessage
}

0 comments on commit f642c26

Please sign in to comment.