Example for httpresponse error
package azcore_test
import (
+ "context"
"encoding/json"
+ "errors"
"fmt"
+ "io/ioutil"
+ "net/http"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/log"
+ "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
)
// false positive by linter
@@ -58,3 +63,25 @@ func ExampleNullValue() {
// Output:
// {"count":null}
}
+
+func ExampleHTTPResponse() {
+ pipeline := runtime.NewPipeline(http.DefaultClient)
+ req, err := runtime.NewRequest(context.Background(), "POST", "https://fakecontainerregisty.azurecr.io/acr/v1/nonexisteng/_tags")
+ if err != nil {
+ panic(err)
+ }
+ resp, err := pipeline.Do(req)
+ var httpErr azcore.HTTPResponse
+ if errors.As(err, &httpErr) {
+ // Handle Error
+ if httpErr.RawResponse().StatusCode == http.StatusNotFound {
+ fmt.Printf("Repository could not be found: %v", httpErr.RawResponse())
+ } else if httpErr.RawResponse().StatusCode == http.StatusForbidden {
+ fmt.Printf("You do not have permission to access this repository: %v", httpErr.RawResponse())
+ } else {
+ // ...
+ }
+ }
+ // Do something with response
+ fmt.Println(ioutil.ReadAll(resp.Body))
+}
