File: custom_error_deser.go

package info (click to toggle)
golang-github-aws-aws-sdk-go-v2 1.30.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 662,428 kB
  • sloc: java: 16,875; makefile: 432; sh: 175
file content (94 lines) | stat: -rw-r--r-- 2,742 bytes parent folder | download | duplicates (5)
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
package customizations

import (
	"bytes"
	"context"
	"encoding/xml"
	"fmt"
	"io"
	"io/ioutil"
	"strings"

	"github.com/aws/smithy-go"
	smithyxml "github.com/aws/smithy-go/encoding/xml"
	"github.com/aws/smithy-go/middleware"
	"github.com/aws/smithy-go/ptr"
	smithyhttp "github.com/aws/smithy-go/transport/http"

	awsmiddle "github.com/aws/aws-sdk-go-v2/aws/middleware"
	"github.com/aws/aws-sdk-go-v2/service/route53/types"
)

// HandleCustomErrorDeserialization check if Route53 response is an error and needs
// custom error deserialization.
func HandleCustomErrorDeserialization(stack *middleware.Stack) error {
	return stack.Deserialize.Insert(&processResponse{}, "OperationDeserializer", middleware.After)
}

// middleware to process raw response and look for error response with InvalidChangeBatch error tag
type processResponse struct{}

// ID returns the middleware ID.
func (*processResponse) ID() string {
	return "Route53:ProcessResponseForCustomErrorResponse"
}

func (m *processResponse) HandleDeserialize(
	ctx context.Context, in middleware.DeserializeInput, next middleware.DeserializeHandler) (
	out middleware.DeserializeOutput, metadata middleware.Metadata, err error,
) {
	out, metadata, err = next.HandleDeserialize(ctx, in)
	if err != nil {
		return out, metadata, err
	}

	response, ok := out.RawResponse.(*smithyhttp.Response)
	if !ok {
		return out, metadata, &smithy.DeserializationError{Err: fmt.Errorf("unknown transport type %T", out.RawResponse)}
	}

	// check if success response
	if response.StatusCode >= 200 && response.StatusCode < 300 {
		return
	}

	var readBuff bytes.Buffer
	body := io.TeeReader(response.Body, &readBuff)

	rootDecoder := xml.NewDecoder(body)
	t, err := smithyxml.FetchRootElement(rootDecoder)
	if err == io.EOF {
		return out, metadata, nil
	}

	// rewind response body
	response.Body = ioutil.NopCloser(io.MultiReader(&readBuff, response.Body))

	// if start tag is "InvalidChangeBatch", the error response needs custom unmarshaling.
	if strings.EqualFold(t.Name.Local, "InvalidChangeBatch") {
		return out, metadata, route53CustomErrorDeser(&metadata, response)
	}

	return out, metadata, err
}

// error type for invalidChangeBatchError
type invalidChangeBatchError struct {
	Messages  []string `xml:"Messages>Message"`
	RequestID string   `xml:"RequestId"`
}

func route53CustomErrorDeser(metadata *middleware.Metadata, response *smithyhttp.Response) error {
	err := invalidChangeBatchError{}
	xml.NewDecoder(response.Body).Decode(&err)

	// set request id in metadata
	if len(err.RequestID) != 0 {
		awsmiddle.SetRequestIDMetadata(metadata, err.RequestID)
	}

	return &types.InvalidChangeBatch{
		Message:  ptr.String("ChangeBatch errors occurred"),
		Messages: err.Messages,
	}
}