From c7ada3e86cc643776b7c86459cbf27a4e501069f Mon Sep 17 00:00:00 2001 From: Carl Johnson Date: Tue, 28 Mar 2023 10:45:55 -0400 Subject: [PATCH] ExampleBuilder_ErrorJSON: Fix status code message; breakup code --- builder_example_test.go | 65 +++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/builder_example_test.go b/builder_example_test.go index eab5bcf..309b7a1 100644 --- a/builder_example_test.go +++ b/builder_example_test.go @@ -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