Skip to content

Commit

Permalink
Added a RoundTripper to simplehttp that wraps around another RoundTri…
Browse files Browse the repository at this point in the history
…pper and converts 4XX and 5XX series errors to SimpleError with the corresponding code set, as defined in the inverse mapping
  • Loading branch information
Calvin Lobo committed Sep 14, 2023
1 parent bb227b6 commit f37dd84
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
55 changes: 55 additions & 0 deletions ecosystem/http/roundtripper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package simplehttp

import (
"github.com/lobocv/simplerr"
"net/http"
)

type attr int

const (
attrHTTPResponse = attr(1)
)

// roundTripper is a wrapper around the given http.RoundTripper that converts 4XX and 5XX series errors to SimpleErrors
type roundTripper struct {
rt http.RoundTripper
}

func (s roundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
resp, err := s.rt.RoundTrip(request)
if err != nil {
return resp, err
}

if resp.StatusCode >= 400 && resp.StatusCode < 600 {
code, _ := GetCode(resp.StatusCode)
serr := simplerr.New("%s", resp.Status).
Code(code).
Attr(attrHTTPResponse, resp)
return nil, serr
}

return resp, nil
}

// EnableHTTPStatusErrors wraps the http.RoundTripper in middleware that converts 4XX and 5XX series errors to SimpleErrors
// with the code defined in the inverse mapping.
func EnableHTTPStatusErrors(rt http.RoundTripper) http.RoundTripper {
return roundTripper{rt: rt}
}

// GetHTTPResponseAttr gets the *http.Response attached to the error, if it exists.
func GetHTTPResponseAttr(err error) *http.Response {
v, ok := simplerr.GetAttribute(err, attrHTTPResponse)
if !ok {
return nil
}

resp, ok := v.(*http.Response)
if !ok {
return nil
}

return resp
}
79 changes: 79 additions & 0 deletions ecosystem/http/roundtripper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package simplehttp

import (
"fmt"
"github.com/lobocv/simplerr"
"github.com/stretchr/testify/require"
"net/http"
"testing"
)

type dummyTransport struct {
response *http.Response
err error
}

func (d dummyTransport) RoundTrip(_ *http.Request) (*http.Response, error) {
return d.response, d.err
}

func TestRoundTripperConvertCode(t *testing.T) {

for status, expectedCode := range inverseMapping {
originalResponse := &http.Response{}
originalResponse.StatusCode = status

rt := EnableHTTPStatusErrors(dummyTransport{
response: originalResponse,
})
resp, err := rt.RoundTrip(nil)

require.NotNil(t, simplerr.As(err), fmt.Sprintf("'%s' failed: simplerr not returned", http.StatusText(status)))
require.True(t, simplerr.HasErrorCode(err, expectedCode), fmt.Sprintf("'%s' failed: unexpected code", http.StatusText(status)))
require.Nil(t, resp)
}
}

func TestRoundTripperNoConversionFound(t *testing.T) {
originalResponse := &http.Response{}
originalResponse.StatusCode = http.StatusOK

rt := EnableHTTPStatusErrors(dummyTransport{
response: originalResponse,
})
resp, err := rt.RoundTrip(nil)
require.NoError(t, err)
require.Equal(t, originalResponse, resp)
}

func TestRoundTripperAttrResponse(t *testing.T) {

originalResponse := &http.Response{StatusCode: http.StatusTeapot}

rt := EnableHTTPStatusErrors(dummyTransport{
response: originalResponse,
})

resp, err := rt.RoundTrip(nil)
expectedCode := simplerr.CodeUnknown
require.True(t, simplerr.HasErrorCode(err, expectedCode), "failed to convert error code")
require.Nil(t, resp)

gotOriginalResponse := GetHTTPResponseAttr(err)
require.Equal(t, originalResponse, gotOriginalResponse)

require.Nil(t, GetHTTPResponseAttr(nil))
require.Nil(t, GetHTTPResponseAttr(simplerr.New("something").Attr(attrHTTPResponse, "not an *http.Response")))
}

func TestRoundTripperErrorOnUnderlyingRoundTripper(t *testing.T) {

rt := EnableHTTPStatusErrors(dummyTransport{
err: fmt.Errorf("some error"),
})
resp, err := rt.RoundTrip(nil)

require.Nil(t, resp)
require.Errorf(t, err, "some error")

}

0 comments on commit f37dd84

Please sign in to comment.