-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathvalidator_handler_example_test.go
More file actions
44 lines (39 loc) · 964 Bytes
/
validator_handler_example_test.go
File metadata and controls
44 lines (39 loc) · 964 Bytes
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 requests_test
import (
"context"
"errors"
"fmt"
"strings"
"github.com/carlmjohnson/requests"
)
func ExampleValidatorHandler() {
var (
regularBody string
errBody string
)
// If we fail validation because the response is a 404,
// we handle the body with errBody instead of regularBody
// for separate processing.
err := requests.
URL("http://example.com/404").
ToString(®ularBody).
AddValidator(
requests.ValidatorHandler(
requests.DefaultValidator,
requests.ToString(&errBody),
)).
Fetch(context.Background())
switch {
case errors.Is(err, requests.ErrInvalidHandled):
fmt.Println("got errBody:",
strings.Contains(errBody, "Example Domain"))
case err != nil:
fmt.Println("unexpected error", err)
case err == nil:
fmt.Println("unexpected success")
}
fmt.Println("got regularBody:", strings.Contains(regularBody, "Example Domain"))
// Output:
// got errBody: true
// got regularBody: false
}