-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
validator_handler.go
34 lines (30 loc) · 984 Bytes
/
validator_handler.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
package requests
import (
"errors"
"fmt"
"net/http"
)
// ValidatorHandler composes a Validator and a Handler.
// If the validation check fails, it triggers the handler.
// Any errors from validator or handler will be joined to the error returned.
// If the handler succeeds, the error will matching ErrInvalidHandled.
func ValidatorHandler(v, h ResponseHandler) ResponseHandler {
return func(res *http.Response) error {
err1 := v(res)
if err1 == nil { // passes validation
return nil
}
err2 := h(res)
if err2 == nil { // successfully handled
return fmt.Errorf("%w: %w", ErrInvalidHandled, err1)
}
return errors.Join(err1, err2)
}
}
var ErrInvalidHandled = errors.New("handled recovery from invalid response")
// ErrorJSON is a ValidatorHandler that applies DefaultValidator
// and decodes the response as a JSON object
// if the DefaultValidator check fails.
func ErrorJSON(v any) ResponseHandler {
return ValidatorHandler(DefaultValidator, ToJSON(v))
}