Skip to content

Commit

Permalink
ExampleBuilder_ErrorJSON: Fix status code message; breakup code
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed Mar 30, 2023
1 parent 6b9d716 commit c7ada3e
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions builder_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,41 +496,44 @@ func ExampleBuilder_Transport() {
}

func ExampleBuilder_ErrorJSON() {
goodRes := requests.ReplayString(`HTTP/1.1 200 OK
{"x": 1}`)

var goodJSON1 struct{ X int }
var errJSON1 struct{ Error string }
err := requests.
URL("http://example.com/").
Transport(goodRes).
ToJSON(&goodJSON1).
ErrorJSON(&errJSON1).
Fetch(context.Background())
if err != nil {
fmt.Println("problem", err)
} else {
fmt.Println("X", goodJSON1.X)
{
trans := requests.ReplayString(`HTTP/1.1 200 OK
{"x": 1}`)

var goodJSON1 struct{ X int }
var errJSON1 struct{ Error string }
err := requests.
URL("http://example.com/").
Transport(trans).
ToJSON(&goodJSON1).
ErrorJSON(&errJSON1).
Fetch(context.Background())
if err != nil {
fmt.Println("Error!", err)
} else {
fmt.Println("X", goodJSON1.X)
}
}

badRes := requests.ReplayString(`HTTP/1.1 418 OK
{
trans := requests.ReplayString(`HTTP/1.1 418 I'm a teapot
{"error": "brewing"}`)

var goodJSON2 struct{ X int }
var errJSON2 struct{ Error string }
err = requests.
URL("http://example.com/").
Transport(badRes).
ToJSON(&goodJSON2).
ErrorJSON(&errJSON2).
Fetch(context.Background())
if err != nil {
fmt.Println(errors.Is(err, requests.ErrInvalidHandled))
fmt.Println(errJSON2.Error)
} else {
fmt.Println("unexpected success")
var goodJSON2 struct{ X int }
var errJSON2 struct{ Error string }
err := requests.
URL("http://example.com/").
Transport(trans).
ToJSON(&goodJSON2).
ErrorJSON(&errJSON2).
Fetch(context.Background())
if err != nil {
fmt.Println(errors.Is(err, requests.ErrInvalidHandled))
fmt.Println(errJSON2.Error)
} else {
fmt.Println("unexpected success")
}
}
// Output:
// X 1
Expand Down

0 comments on commit c7ada3e

Please sign in to comment.