Skip to content

Commit

Permalink
feat(sdk): Fix error unit test and preserve sdk.Error on wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt committed Sep 28, 2018
1 parent 8a64c69 commit 0a6b710
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cli/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func ExitOnError(err error, helpFunc ...func() error) {
return
}

var code int
code := 50 // default error code

switch e := err.(type) {
case sdk.Error:
Expand Down
13 changes: 9 additions & 4 deletions sdk/cdsclient/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,19 @@ func (c *client) RequestJSON(method, path string, in interface{}, out interface{
return nil, nil, code, err
}

if code >= 400 {
if err := sdk.DecodeError(res); err != nil {
return res, nil, code, err
}
return res, nil, code, fmt.Errorf("HTTP %d", code)
}

if out != nil {
if err := json.Unmarshal(res, out); err != nil {
return res, nil, code, err
}
}

if code >= 400 {
return res, nil, code, fmt.Errorf("HTTP %d", code)
}
return res, header, code, nil
}

Expand Down Expand Up @@ -149,8 +153,9 @@ func (c *client) Request(method string, path string, body io.Reader, mods ...Req

if code >= 400 {
if err := sdk.DecodeError(bodyBtes); err != nil {
return nil, nil, code, err
return bodyBtes, nil, code, err
}
return bodyBtes, nil, code, fmt.Errorf("HTTP %d", code)
}

return bodyBtes, respHeader, code, nil
Expand Down
18 changes: 18 additions & 0 deletions sdk/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,15 @@ func WrapError(err error, format string, args ...interface{}) error {
return e
}

// if it's a Error wrap it in error with stack
if e, ok := err.(Error); ok {
return errorWithStack{
root: errors.Wrap(err, m),
stack: callers(),
httpError: e,
}
}

return errorWithStack{
root: errors.Wrap(err, m),
stack: callers(),
Expand All @@ -585,6 +594,15 @@ func WithStack(err error) error {
return e
}

// if it's a Error wrap it in error with stack
if e, ok := err.(Error); ok {
return errorWithStack{
root: errors.WithStack(err),
stack: callers(),
httpError: e,
}
}

return errorWithStack{
root: errors.WithStack(err),
stack: callers(),
Expand Down
25 changes: 21 additions & 4 deletions sdk/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package sdk
import (
"fmt"
"testing"

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

func TestErrorIs(t *testing.T) {
Expand All @@ -16,17 +18,25 @@ func TestErrorIs(t *testing.T) {
want bool
}{
{
"Check Error is true",
"Check for wrapped Error is true",
args{
err: fmt.Errorf(ErrNoProject.String()),
err: WithStack(ErrNoProject),
t: ErrNoProject,
},
true,
},
{
"Check Error is false",
"Check for Error is true",
args{
err: fmt.Errorf("FOO"),
err: ErrNoProject,
t: ErrNoProject,
},
true,
},
{
"Check for other error is false",
args{
err: fmt.Errorf("project does not exist"),
t: ErrNoProject,
},
false,
Expand All @@ -43,18 +53,25 @@ func TestErrorIs(t *testing.T) {

func TestNewError(t *testing.T) {
err := NewError(ErrWrongRequest, fmt.Errorf("this is an error generated from vendor"))
assert.Equal(t, "TestNewError: wrong request (caused by: this is an error generated from vendor)", err.Error())

// print the error call stack
fmt.Println(err)
// print the error stack trace
fmt.Printf("%+v\n", err)

httpErr := ExtractHTTPError(err, "fr")

// print the http error
fmt.Println(httpErr)

assert.Equal(t, "la requête est incorrecte", httpErr.Error())
}

func TestWrapError(t *testing.T) {
err := oneForStackTest()
assert.Equal(t, "TestWrapError>oneForStackTest>twoForStackTest>threeForStackTest>fourForStackTest>fiveForStackTest: internal server error (caused by: one: two: three: four: five: this is an error generated from vendor)", err.Error())

// print the error call stack
fmt.Println(err)
// print the error stack trace
Expand Down

0 comments on commit 0a6b710

Please sign in to comment.