Skip to content

Commit

Permalink
error source - add unwrap; convenience methods (#767)
Browse files Browse the repository at this point in the history
* add unwrap; convenience methods
  • Loading branch information
scottlepp authored Oct 12, 2023
1 parent c9ccd20 commit ece8a54
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions experimental/errorsource/error_source_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ type Error struct {
Err error
}

// Error implements the interface
func (r Error) Error() string {
return r.Err.Error()
}

// Unwrap implements the interface
func (r Error) Unwrap() error {
return r.Err
}

// Middleware captures error source metric
func Middleware(plugin string) httpclient.Middleware {
return httpclient.NamedMiddlewareFunc(plugin, func(opts httpclient.Options, next http.RoundTripper) http.RoundTripper {
Expand All @@ -35,3 +41,26 @@ func Middleware(plugin string) httpclient.Middleware {
})
})
}

// PluginError will apply the source as plugin
func PluginError(err error, override bool) error {
return SourceError(backend.ErrorSourcePlugin, err, override)
}

// DownstreamError will apply the source as downstream
func DownstreamError(err error, override bool) error {
return SourceError(backend.ErrorSourceDownstream, err, override)
}

// SourceError returns an error with the source
// If source is already defined, it will return it, or you can override
func SourceError(source backend.ErrorSource, err error, override bool) error {
var sourceError Error
if errors.As(err, &sourceError) && !override {
return err // already has a source
}
return Error{
Source: source,
Err: err,
}
}

0 comments on commit ece8a54

Please sign in to comment.