/
result.go
44 lines (37 loc) · 1.07 KB
/
result.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package http
import (
"errors"
"fmt"
"github.com/cloudevents/sdk-go/v2/protocol"
)
// NewResult returns a fully populated http Result that should be used as
// a transport.Result.
func NewResult(statusCode int, messageFmt string, args ...interface{}) protocol.Result {
return &Result{
StatusCode: statusCode,
Format: messageFmt,
Args: args,
}
}
// Result wraps the fields required to make adjustments for http Responses.
type Result struct {
StatusCode int
Format string
Args []interface{}
}
// make sure Result implements error.
var _ error = (*Result)(nil)
// Is returns if the target error is a Result type checking target.
func (e *Result) Is(target error) bool {
if o, ok := target.(*Result); ok {
return e.StatusCode == o.StatusCode
}
// Allow for wrapped errors.
err := fmt.Errorf(e.Format, e.Args...)
return errors.Is(err, target)
}
// Error returns the string that is formed by using the format string with the
// provided args.
func (e *Result) Error() string {
return fmt.Sprintf("%d: %s", e.StatusCode, fmt.Sprintf(e.Format, e.Args...))
}