Skip to content

Commit

Permalink
Fix error handling for parsed output (#23)
Browse files Browse the repository at this point in the history
* Fix error handling for parsed output
  • Loading branch information
jonnylangefeld authored Dec 17, 2021
1 parent a3d5745 commit 8b63ba8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
22 changes: 14 additions & 8 deletions pkg/mc/mc.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,17 @@ func do(done chan bool, context string, namespace string, output map[string]json
stdout, err := kubectl(cmd)
if err != nil {
stdout = []byte(err.Error())
}
mutex.Lock()
logger.Debug("kubectl error", zap.Error(err))
} else {
mutex.Lock()

cns := context
if namespace != "" {
cns += ": " + namespace
cns := context
if namespace != "" {
cns += ": " + namespace
}
output[cns] = stdout
mutex.Unlock()
}
output[cns] = stdout
mutex.Unlock()
if writeToStdout {
fmt.Fprint(out, formatContext(context, namespace, stdout))
}
Expand All @@ -259,7 +261,11 @@ func do(done chan bool, context string, namespace string, output map[string]json
func kubectl(cmd Cmd) ([]byte, error) {
out, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf(strings.Replace(strings.Replace(string(out), "error: ", "", -1), "Error: ", "", -1))
errString := err.Error()
if err, ok := err.(*exec.ExitError); ok {
errString = string(err.Stderr)
}
return nil, fmt.Errorf(strings.Replace(strings.Replace(errString, "error: ", "", -1), "Error: ", "", -1))
}
return out, nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/mc/mc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package mc
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"os/exec"
"sync"
"testing"

Expand Down Expand Up @@ -188,8 +188,8 @@ func TestKubectl(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, kubectlReturn, got)

m.EXPECT().Output().Return([]byte(`Error: unknown shorthand flag: 'a' in -abc
See 'kubectl get --help' for usage.`), errors.New(""))
m.EXPECT().Output().Return(nil, &exec.ExitError{Stderr: []byte(`Error: unknown shorthand flag: 'a' in -abc
See 'kubectl get --help' for usage.`)})

got, err = kubectl(m)
assert.Nil(t, got)
Expand Down

0 comments on commit 8b63ba8

Please sign in to comment.