Skip to content

Commit

Permalink
Example for httpresponse error (#15598)
Browse files Browse the repository at this point in the history
  • Loading branch information
seankane-msft authored Sep 20, 2021
1 parent 9c875cb commit 97af53c
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions sdk/azcore/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
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
Expand Down Expand Up @@ -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))
}

0 comments on commit 97af53c

Please sign in to comment.