File: unmarshal_error_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go 1.49.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 312,636 kB
  • sloc: makefile: 120
file content (240 lines) | stat: -rw-r--r-- 5,967 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//go:build go1.7
// +build go1.7

package jsonrpc

import (
	"bytes"
	"encoding/hex"
	"io/ioutil"
	"net/http"
	"reflect"
	"strings"
	"testing"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/aws/request"
	"github.com/aws/aws-sdk-go/private/protocol"
)

const unknownErrJSON = `{"__type":"UnknownError", "message":"error message", "something":123}`
const simpleErrJSON = `{"__type":"SimpleError", "message":"some message", "foo":123}`
const simpleCasedErrJSON = `{"__type":"SimpleError", "Message":"some message", "foo":123}`

type SimpleError struct {
	_ struct{} `type:"structure"`
	error

	Message2 *string `type:"string" locationName:"message"`
	Foo      *int64  `type:"integer" locationName:"foo"`
}

const otherErrJSON = `{"__type":"OtherError", "message":"some message"}`
const complexCodeErrJSON = `{"__type":"foo.bar#OtherError", "message":"some message"}`

type OtherError struct {
	_ struct{} `type:"structure"`
	error

	Message2 *string `type:"string" locationName:"message"`
}

const complexErrJSON = `{"__type":"ComplexError", "message":"some message", "foo": {"bar":"abc123", "baz":123}}`

type ComplexError struct {
	_ struct{} `type:"structure"`
	error

	Message2 *string      `type:"string" locationName:"message"`
	Foo      *ErrorNested `type:"structure" locationName:"foo"`
}
type ErrorNested struct {
	_ struct{} `type:"structure"`

	Bar *string `type:"string" locationName:"bar"`
	Baz *int64  `type:"integer" locationName:"baz"`
}

func TestUnmarshalTypedError(t *testing.T) {

	respMeta := protocol.ResponseMetadata{
		StatusCode: 400,
		RequestID:  "abc123",
	}

	exceptions := map[string]func(protocol.ResponseMetadata) error{
		"SimpleError": func(meta protocol.ResponseMetadata) error {
			return &SimpleError{}
		},
		"OtherError": func(meta protocol.ResponseMetadata) error {
			return &OtherError{}
		},
		"ComplexError": func(meta protocol.ResponseMetadata) error {
			return &ComplexError{}
		},
	}

	cases := map[string]struct {
		Response *http.Response
		Expect   error
		Err      string
	}{
		"simple error": {
			Response: &http.Response{
				Header: http.Header{},
				Body:   ioutil.NopCloser(strings.NewReader(simpleErrJSON)),
			},
			Expect: &SimpleError{
				Message2: aws.String("some message"),
				Foo:      aws.Int64(123),
			},
		},
		"other error": {
			Response: &http.Response{
				Header: http.Header{},
				Body:   ioutil.NopCloser(strings.NewReader(otherErrJSON)),
			},
			Expect: &OtherError{
				Message2: aws.String("some message"),
			},
		},
		"other complex Code error": {
			Response: &http.Response{
				Header: http.Header{},
				Body:   ioutil.NopCloser(strings.NewReader(complexCodeErrJSON)),
			},
			Expect: &OtherError{
				Message2: aws.String("some message"),
			},
		},
		"complex error": {
			Response: &http.Response{
				Header: http.Header{},
				Body:   ioutil.NopCloser(strings.NewReader(complexErrJSON)),
			},
			Expect: &ComplexError{
				Message2: aws.String("some message"),
				Foo: &ErrorNested{
					Bar: aws.String("abc123"),
					Baz: aws.Int64(123),
				},
			},
		},
		"unknown error": {
			Response: &http.Response{
				Header: http.Header{},
				Body:   ioutil.NopCloser(strings.NewReader(unknownErrJSON)),
			},
			Expect: awserr.NewRequestFailure(
				awserr.New("UnknownError", "error message", nil),
				respMeta.StatusCode,
				respMeta.RequestID,
			),
		},
		"invalid error": {
			Response: &http.Response{
				StatusCode: 400,
				Header:     http.Header{},
				Body:       ioutil.NopCloser(strings.NewReader(`{`)),
			},
			Err: "failed decoding",
		},
		"mixed case fields": {
			Response: &http.Response{
				Header: http.Header{},
				Body:   ioutil.NopCloser(strings.NewReader(simpleCasedErrJSON)),
			},
			Expect: &SimpleError{
				Message2: aws.String("some message"),
				Foo:      aws.Int64(123),
			},
		},
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			u := NewUnmarshalTypedError(exceptions)
			v, err := u.UnmarshalError(c.Response, respMeta)

			if len(c.Err) != 0 {
				if err == nil {
					t.Fatalf("expect error, got none")
				}
				if e, a := c.Err, err.Error(); !strings.Contains(a, e) {
					t.Fatalf("expect %v in error, got %v", e, a)
				}
			} else if err != nil {
				t.Fatalf("expect no error, got %v", err)
			}

			if e, a := c.Expect, v; !reflect.DeepEqual(e, a) {
				t.Errorf("expect %+#v, got %#+v", e, a)
			}
		})
	}
}

func TestUnmarshalError_SerializationError(t *testing.T) {
	cases := map[string]struct {
		Request     *request.Request
		ExpectMsg   string
		ExpectBytes []byte
	}{
		"empty body": {
			Request: &request.Request{
				Data: &struct{}{},
				HTTPResponse: &http.Response{
					StatusCode: 400,
					Header: http.Header{
						"X-Amzn-Requestid": []string{"abc123"},
					},
					Body: ioutil.NopCloser(
						bytes.NewReader([]byte{}),
					),
				},
			},
			ExpectMsg: "error message missing",
		},
		"HTML body": {
			Request: &request.Request{
				Data: &struct{}{},
				HTTPResponse: &http.Response{
					StatusCode: 400,
					Header: http.Header{
						"X-Amzn-Requestid": []string{"abc123"},
					},
					Body: ioutil.NopCloser(
						bytes.NewReader([]byte(`<html></html>`)),
					),
				},
			},
			ExpectBytes: []byte(`<html></html>`),
			ExpectMsg:   "failed decoding",
		},
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			req := c.Request

			UnmarshalError(req)
			if req.Error == nil {
				t.Fatal("expect error, got none")
			}

			aerr := req.Error.(awserr.RequestFailure)
			if e, a := request.ErrCodeSerialization, aerr.Code(); e != a {
				t.Errorf("expect %v, got %v", e, a)
			}

			uerr := aerr.OrigErr().(awserr.UnmarshalError)
			if e, a := c.ExpectMsg, uerr.Message(); !strings.Contains(a, e) {
				t.Errorf("Expect %q, in %q", e, a)
			}
			if e, a := c.ExpectBytes, uerr.Bytes(); !bytes.Equal(e, a) {
				t.Errorf("expect:\n%v\nactual:\n%v", hex.Dump(e), hex.Dump(a))
			}
		})
	}
}