Skip to content

Commit

Permalink
Add HTTPResponse interface (#11428)
Browse files Browse the repository at this point in the history
* Add HTTPResponse interface

To be used by callers to access the raw HTTP response from an error in
the event of an API call failure.
Updated sdk/internal dependency to latest version.

* rename package alias
  • Loading branch information
jhendrixMSFT authored Jul 9, 2020
1 parent a30826c commit 1fc9c47
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 37 deletions.
50 changes: 16 additions & 34 deletions sdk/azcore/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,29 @@ package azcore

import (
"errors"
"fmt"
"net/http"

sdkruntime "github.com/Azure/azure-sdk-for-go/sdk/internal/runtime"
)

var (
// ErrNoMorePolicies is returned from Request.Next() if there are no more policies in the pipeline.
ErrNoMorePolicies = errors.New("no more policies")
)

// TODO: capture frame info for marshal, unmarshal, and parsing errors
// built in frame in xerror? %w
type frameInfo struct {
file string
line int
}

func (f frameInfo) String() string {
if f.zero() {
return ""
}
return fmt.Sprintf("file: %s, line: %d", f.file, f.line)
}

func (f frameInfo) zero() bool {
return f.file == "" && f.line == 0
}

// RequestError is returned when the service returns an unsuccessful resopnse code (4xx, 5xx).
type RequestError struct {
msg string
resp *Response
}

func newRequestError(message string, response *Response) error {
return RequestError{msg: message, resp: response}
}
var (
// StackFrameCount contains the number of stack frames to include when a trace is being collected.
StackFrameCount = 32
)

func (re RequestError) Error() string {
return re.msg
// HTTPResponse provides access to an HTTP response when available.
// Errors returned from failed API calls will implement this interface.
// Use errors.As() to access this interface in the error chain.
// If there was no HTTP response then this interface will be omitted
// from any error in the chain.
type HTTPResponse interface {
RawResponse() *http.Response
}

// Response returns the underlying response.
func (re RequestError) Response() *Response {
return re.resp
}
// ensure our internal ResponseError type implements HTTPResponse
var _ HTTPResponse = (*sdkruntime.ResponseError)(nil)
2 changes: 1 addition & 1 deletion sdk/azcore/go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/Azure/azure-sdk-for-go/sdk/azcore

require github.com/Azure/azure-sdk-for-go/sdk/internal v0.1.1
require github.com/Azure/azure-sdk-for-go/sdk/internal v0.2.0

go 1.13
4 changes: 2 additions & 2 deletions sdk/azcore/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/Azure/azure-sdk-for-go/sdk/internal v0.1.1 h1:S87cbjhPwbSqbygGH69JpB6txa/y1ce2ewaWBQa8Cws=
github.com/Azure/azure-sdk-for-go/sdk/internal v0.1.1/go.mod h1:Q+TCQnSr+clUU0JU+xrHZ3slYCxw17AOFdvWFpQXjAY=
github.com/Azure/azure-sdk-for-go/sdk/internal v0.2.0 h1:cLpVMIkXC/umSP9DMz9I6FttDWJAsmvhpaB6MlkagGY=
github.com/Azure/azure-sdk-for-go/sdk/internal v0.2.0/go.mod h1:Q+TCQnSr+clUU0JU+xrHZ3slYCxw17AOFdvWFpQXjAY=

0 comments on commit 1fc9c47

Please sign in to comment.