Skip to content

Commit

Permalink
PR feedback updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jasdel committed Jan 9, 2020
1 parent 7537c16 commit 3feee2a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 19 deletions.
35 changes: 21 additions & 14 deletions private/model/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@ func newClient(cfg aws.Config, handlers request.Handlers, partitionID, endpoint,
svc.Handlers.Build.PushBackNamed({{ .ProtocolPackage }}.BuildHandler)
svc.Handlers.Unmarshal.PushBackNamed({{ .ProtocolPackage }}.UnmarshalHandler)
svc.Handlers.UnmarshalMeta.PushBackNamed({{ .ProtocolPackage }}.UnmarshalMetaHandler)
{{- if and $.WithGeneratedTypedErrors (gt (len $.ShapeListErrors) 0) }}
{{- $_ := $.AddSDKImport "private/protocol" }}
svc.Handlers.UnmarshalError.PushBackNamed(
Expand All @@ -615,16 +616,20 @@ func newClient(cfg aws.Config, handlers request.Handlers, partitionID, endpoint,
{{- else }}
svc.Handlers.UnmarshalError.PushBackNamed({{ .ProtocolPackage }}.UnmarshalErrorHandler)
{{- end }}
{{ if .HasEventStream }}
svc.Handlers.BuildStream.PushBackNamed({{ .ProtocolPackage }}.BuildHandler)
svc.Handlers.UnmarshalStream.PushBackNamed({{ .ProtocolPackage }}.UnmarshalHandler)
{{ end }}
{{ if .UseInitMethods }}// Run custom client initialization if present
if initClient != nil {
initClient(svc.Client)
}
{{ end }}
{{- if .HasEventStream }}
svc.Handlers.BuildStream.PushBackNamed({{ .ProtocolPackage }}.BuildHandler)
svc.Handlers.UnmarshalStream.PushBackNamed({{ .ProtocolPackage }}.UnmarshalHandler)
{{- end }}
{{- if .UseInitMethods }}
// Run custom client initialization if present
if initClient != nil {
initClient(svc.Client)
}
{{- end }}
return svc
}
Expand All @@ -634,11 +639,13 @@ func newClient(cfg aws.Config, handlers request.Handlers, partitionID, endpoint,
func (c *{{ .StructName }}) newRequest(op *request.Operation, params, data interface{}) *request.Request {
req := c.NewRequest(op, params, data)
{{ if .UseInitMethods }}// Run custom request initialization if present
if initRequest != nil {
initRequest(req)
}
{{ end }}
{{- if .UseInitMethods }}
// Run custom request initialization if present
if initRequest != nil {
initRequest(req)
}
{{- end }}
return req
}
Expand Down
4 changes: 4 additions & 0 deletions private/model/api/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,11 @@ func (a *API) Setup() error {

a.findEndpointDiscoveryOp()
a.injectUnboundedOutputStreaming()

// Enables generated types for APIs using REST-JSON and JSONRPC protocols.
// Other protocols will be added as supported.
a.enableGeneratedTypedErrors()

if err := a.customizationPasses(); err != nil {
return err
}
Expand Down
13 changes: 9 additions & 4 deletions private/protocol/restjson/unmarshal_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import (
"github.com/aws/aws-sdk-go/private/protocol/rest"
)

const (
errorTypeHeader = "X-Amzn-Errortype"
errorMessageHeader = "X-Amzn-Errormessage"
)

// UnmarshalTypedError provides unmarshaling errors API response errors
// for both typed and untyped errors.
type UnmarshalTypedError struct {
Expand All @@ -36,8 +41,8 @@ func (u *UnmarshalTypedError) UnmarshalError(
respMeta protocol.ResponseMetadata,
) (error, error) {

code := resp.Header.Get("X-Amzn-Errortype")
msg := resp.Header.Get("X-Amzn-Errormessage")
code := resp.Header.Get(errorTypeHeader)
msg := resp.Header.Get(errorMessageHeader)

body := resp.Body
if len(code) == 0 {
Expand Down Expand Up @@ -106,11 +111,11 @@ func UnmarshalError(r *request.Request) {
return
}

code := r.HTTPResponse.Header.Get("X-Amzn-Errortype")
code := r.HTTPResponse.Header.Get(errorTypeHeader)
if code == "" {
code = jsonErr.Code
}
msg := r.HTTPResponse.Header.Get("X-Amzn-Errormessage")
msg := r.HTTPResponse.Header.Get(errorMessageHeader)
if msg == "" {
msg = jsonErr.Message
}
Expand Down
40 changes: 40 additions & 0 deletions private/protocol/restjson/unmarshal_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

const unknownErrJSON = `{"code":"UnknownError", "message":"error message", "something":123}`
const simpleErrJSON = `{"code":"SimpleError", "message":"some message", "foo":123}`
const simpleNoCodeJSON = `{"message":"some message", "foo":123}`

type SimpleError struct {
_ struct{} `type:"structure"`
Expand Down Expand Up @@ -145,6 +146,45 @@ func TestUnmarshalTypedError(t *testing.T) {
},
Err: "failed decoding",
},
"unknown from header": {
Response: &http.Response{
Header: http.Header{
errorTypeHeader: []string{"UnknownError"},
errorMessageHeader: []string{"error message"},
},
Body: ioutil.NopCloser(nil),
},
Expect: awserr.NewRequestFailure(
awserr.New("UnknownError", "error message", nil),
respMeta.StatusCode,
respMeta.RequestID,
),
},
"code from header": {
Response: &http.Response{
Header: http.Header{
errorTypeHeader: []string{"SimpleError"},
},
Body: ioutil.NopCloser(strings.NewReader(simpleNoCodeJSON)),
},
Expect: &SimpleError{
Message2: aws.String("some message"),
Foo: aws.Int64(123),
},
},
"ignore message header": {
Response: &http.Response{
Header: http.Header{
errorTypeHeader: []string{"SimpleError"},
errorMessageHeader: []string{"error message"},
},
Body: ioutil.NopCloser(strings.NewReader(simpleNoCodeJSON)),
},
Expect: &SimpleError{
Message2: aws.String("some message"),
Foo: aws.Int64(123),
},
},
}

for name, c := range cases {
Expand Down
5 changes: 4 additions & 1 deletion private/protocol/unmarshal_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ func NewUnmarshalErrorHandler(unmarshaler ErrorUnmarshaler) *UnmarshalErrorHandl
}
}

// UnmarshalErrorHandlerName is the name of the named handler.
const UnmarshalErrorHandlerName = "awssdk.protocol.UnmarshalError"

// NamedHandler returns a NamedHandler for the unmarshaler using the set of
// errors the unmarshaler was initialized for.
func (u *UnmarshalErrorHandler) NamedHandler() request.NamedHandler {
return request.NamedHandler{
Name: "awssdk.protocol.UnmarshalError",
Name: UnmarshalErrorHandlerName,
Fn: u.UnmarshalError,
}
}
Expand Down

0 comments on commit 3feee2a

Please sign in to comment.