-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error source HTTP client middleware (#1106)
Adds a new default Error source HTTP client middleware that will wrap any HTTP response error in a downstream error if identified as a downstream HTTP error. Moves backend.ErrorSource related types to a new experimental package status while keeping backward compatibility using type aliases in the backend package referencing the new status package.
- Loading branch information
Showing
14 changed files
with
588 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,142 +1,18 @@ | ||
package backend | ||
package backend_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/grafana/grafana-plugin-sdk-go/backend" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func TestErrorSource(t *testing.T) { | ||
var es ErrorSource | ||
require.False(t, es.IsValid()) | ||
require.True(t, ErrorSourceDownstream.IsValid()) | ||
require.True(t, ErrorSourcePlugin.IsValid()) | ||
} | ||
|
||
func TestIsDownstreamError(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
err error | ||
expected bool | ||
}{ | ||
{ | ||
name: "nil", | ||
err: nil, | ||
expected: false, | ||
}, | ||
{ | ||
name: "downstream error", | ||
err: DownstreamError(nil), | ||
expected: true, | ||
}, | ||
{ | ||
name: "timeout network error", | ||
err: newFakeNetworkError(true, false), | ||
expected: true, | ||
}, | ||
{ | ||
name: "wrapped timeout network error", | ||
err: fmt.Errorf("oh no. err %w", newFakeNetworkError(true, false)), | ||
expected: true, | ||
}, | ||
{ | ||
name: "temporary timeout network error", | ||
err: newFakeNetworkError(true, true), | ||
expected: true, | ||
}, | ||
{ | ||
name: "non-timeout network error", | ||
err: newFakeNetworkError(false, false), | ||
expected: false, | ||
}, | ||
{ | ||
name: "os.ErrDeadlineExceeded", | ||
err: os.ErrDeadlineExceeded, | ||
expected: true, | ||
}, | ||
{ | ||
name: "os.ErrDeadlineExceeded", | ||
err: fmt.Errorf("error: %w", os.ErrDeadlineExceeded), | ||
expected: true, | ||
}, | ||
{ | ||
name: "wrapped os.ErrDeadlineExceeded", | ||
err: errors.Join(fmt.Errorf("oh no"), os.ErrDeadlineExceeded), | ||
expected: true, | ||
}, | ||
{ | ||
name: "other error", | ||
err: fmt.Errorf("other error"), | ||
expected: false, | ||
}, | ||
{ | ||
name: "context.Canceled", | ||
err: context.Canceled, | ||
expected: true, | ||
}, | ||
{ | ||
name: "wrapped context.Canceled", | ||
err: fmt.Errorf("error: %w", context.Canceled), | ||
expected: true, | ||
}, | ||
{ | ||
name: "joined context.Canceled", | ||
err: errors.Join(fmt.Errorf("oh no"), context.Canceled), | ||
expected: true, | ||
}, | ||
{ | ||
name: "gRPC canceled error", | ||
err: status.Error(codes.Canceled, "canceled"), | ||
expected: true, | ||
}, | ||
{ | ||
name: "wrapped gRPC canceled error", | ||
err: fmt.Errorf("error: %w", status.Error(codes.Canceled, "canceled")), | ||
expected: true, | ||
}, | ||
{ | ||
name: "joined gRPC canceled error", | ||
err: errors.Join(fmt.Errorf("oh no"), status.Error(codes.Canceled, "canceled")), | ||
expected: true, | ||
}, | ||
} | ||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
assert.Equalf(t, tc.expected, IsDownstreamError(tc.err), "IsDownstreamError(%v)", tc.err) | ||
}) | ||
} | ||
} | ||
|
||
var _ net.Error = &fakeNetworkError{} | ||
|
||
type fakeNetworkError struct { | ||
timeout bool | ||
temporary bool | ||
} | ||
|
||
func newFakeNetworkError(timeout, temporary bool) *fakeNetworkError { | ||
return &fakeNetworkError{ | ||
timeout: timeout, | ||
temporary: temporary, | ||
} | ||
} | ||
|
||
func (d *fakeNetworkError) Error() string { | ||
return "dummy timeout error" | ||
} | ||
|
||
func (d *fakeNetworkError) Timeout() bool { | ||
return d.timeout | ||
} | ||
|
||
func (d *fakeNetworkError) Temporary() bool { | ||
return d.temporary | ||
var s backend.ErrorSource | ||
require.False(t, s.IsValid()) | ||
require.Equal(t, "plugin", s.String()) | ||
require.True(t, backend.ErrorSourceDownstream.IsValid()) | ||
require.Equal(t, "downstream", backend.ErrorSourceDownstream.String()) | ||
require.True(t, backend.ErrorSourcePlugin.IsValid()) | ||
require.Equal(t, "plugin", backend.ErrorSourcePlugin.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package httpclient | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/grafana/grafana-plugin-sdk-go/experimental/status" | ||
) | ||
|
||
// ErrorSourceMiddlewareName is the middleware name used by ErrorSourceMiddleware. | ||
const ErrorSourceMiddlewareName = "ErrorSource" | ||
|
||
// ErrorSourceMiddleware inspect the response error and wraps it in a [status.DownstreamError] if [status.IsDownstreamHTTPError] returns true. | ||
func ErrorSourceMiddleware() Middleware { | ||
return NamedMiddlewareFunc(ErrorSourceMiddlewareName, func(_ Options, next http.RoundTripper) http.RoundTripper { | ||
return RoundTripperFunc(func(req *http.Request) (*http.Response, error) { | ||
res, err := next.RoundTrip(req) | ||
if err != nil && status.IsDownstreamHTTPError(err) { | ||
return res, status.DownstreamError(err) | ||
} | ||
|
||
return res, err | ||
}) | ||
}) | ||
} |
Oops, something went wrong.