File: decoder_util.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 (85 lines) | stat: -rw-r--r-- 2,268 bytes parent folder | download | duplicates (7)
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
package restjson

import (
	"encoding/json"
	"io"
	"strings"

	"github.com/aws/smithy-go"
)

// GetErrorInfo util looks for code, __type, and message members in the
// json body. These members are optionally available, and the function
// returns the value of member if it is available. This function is useful to
// identify the error code, msg in a REST JSON error response.
func GetErrorInfo(decoder *json.Decoder) (errorType string, message string, err error) {
	var errInfo struct {
		Code    string
		Type    string `json:"__type"`
		Message string
	}

	err = decoder.Decode(&errInfo)
	if err != nil {
		if err == io.EOF {
			return errorType, message, nil
		}
		return errorType, message, err
	}

	// assign error type
	if len(errInfo.Code) != 0 {
		errorType = errInfo.Code
	} else if len(errInfo.Type) != 0 {
		errorType = errInfo.Type
	}

	// assign error message
	if len(errInfo.Message) != 0 {
		message = errInfo.Message
	}

	// sanitize error
	if len(errorType) != 0 {
		errorType = SanitizeErrorCode(errorType)
	}

	return errorType, message, nil
}

// SanitizeErrorCode sanitizes the errorCode string .
// The rule for sanitizing is if a `:` character is present, then take only the
// contents before the first : character in the value.
// If a # character is present, then take only the contents after the
// first # character in the value.
func SanitizeErrorCode(errorCode string) string {
	if strings.ContainsAny(errorCode, ":") {
		errorCode = strings.SplitN(errorCode, ":", 2)[0]
	}

	if strings.ContainsAny(errorCode, "#") {
		errorCode = strings.SplitN(errorCode, "#", 2)[1]
	}

	return errorCode
}

// GetSmithyGenericAPIError returns smithy generic api error and an error interface.
// Takes in json decoder, and error Code string as args. The function retrieves error message
// and error code from the decoder body. If errorCode of length greater than 0 is passed in as
// an argument, it is used instead.
func GetSmithyGenericAPIError(decoder *json.Decoder, errorCode string) (*smithy.GenericAPIError, error) {
	errorType, message, err := GetErrorInfo(decoder)
	if err != nil {
		return nil, err
	}

	if len(errorCode) == 0 {
		errorCode = errorType
	}

	return &smithy.GenericAPIError{
		Code:    errorCode,
		Message: message,
	}, nil
}