Skip to content

Commit

Permalink
chore: remove case used in investigating http.Error and add test of h…
Browse files Browse the repository at this point in the history
…ttp.Error.Error() response.
  • Loading branch information
karel-rehor committed Aug 8, 2024
1 parent af74d7a commit 20a74f5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
2 changes: 0 additions & 2 deletions api/http/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ func (e *Error) Error() string {
return e.Err.Error()
case e.Code != "" && e.Message != "":
return fmt.Sprintf("%s: %s", e.Code, e.Message)
case e.Message != "":
return e.Message
default:
return "Unexpected status code " + strconv.Itoa(e.StatusCode)
}
Expand Down
28 changes: 28 additions & 0 deletions api/http/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package http

import (
"fmt"
"github.com/stretchr/testify/assert"
ihttp "net/http"

Expand Down Expand Up @@ -51,3 +52,30 @@ func TestWriteErrorHeaderToString(t *testing.T) {
assert.NotContains(t, filterString, "Content-Type: application/json")
assert.NotContains(t, filterString, "Retry-After: 2044")
}

func TestErrorIfaceError(t *testing.T) {
tests := []struct {
statusCode int
err error
code string
message string
expected string
}{
{statusCode: 418, err: fmt.Errorf("original test message"), code: "", message: "", expected: "original test message"},
{statusCode: 418, err: fmt.Errorf("original test message"), code: "bad request", message: "is this a teapot?", expected: "original test message"},
{statusCode: 418, err: nil, code: "bad request", message: "is this a teapot?", expected: "bad request: is this a teapot?"},
{statusCode: 418, err: nil, code: "I'm a teapot", message: "", expected: "Unexpected status code 418"},
}

for _, test := range tests {
err := Error{
StatusCode: test.statusCode,
Code: test.code,
Message: test.message,
Err: test.err,
RetryAfter: 0,
Header: ihttp.Header{},
}
assert.Equal(t, test.expected, err.Error())
}
}

0 comments on commit 20a74f5

Please sign in to comment.