File: response.go

package info (click to toggle)
golang-github-mimuret-golang-iij-dpf 0.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,340 kB
  • sloc: makefile: 55
file content (155 lines) | stat: -rw-r--r-- 3,502 bytes parent folder | download
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
package api

import (
	"encoding/json"
	"strings"
)

type ResponseCommon struct {
	RequestID string `read:"request_id"`
}

type RawResponse struct {
	ResponseCommon `read:",inline"`
	Result         json.RawMessage `read:"result,omitempty"`
	Results        json.RawMessage `read:"results,omitempty"`
}

const (
	ErrorTypeParamaterError  string = "ParameterError"
	ErrorTypeNotFound        string = "NotFound"
	ErrorTypeTooManyRequests string = "TooManyRequests"
	ErrorTypeSystemError     string = "SystemError"
	ErrorTypeGatewayTimeout  string = "GatewayTimeout"
)

type BadResponse struct {
	ResponseCommon `read:",inline"`
	StatusCode     int          `read:"-"`
	ErrorType      string       `read:"error_type"`
	ErrorMessage   string       `read:"error_message"`
	ErrorDetails   ErrorDetails `read:"error_details"`
}

func (r *BadResponse) Error() string {
	if r.ErrorType == ErrorTypeParamaterError {
		if r.IsAuthError() {
			return "Auth error"
		}
	}
	errorDetail := ""
	if len(r.ErrorDetails) > 0 {
		errorDetail = " Detail: " + r.ErrorDetails.Error()
	}
	return "ErrorType: " + r.ErrorType + " Message: " + r.ErrorMessage + errorDetail
}

func (r *BadResponse) IsStatusCode(code int) bool {
	return r.StatusCode == code
}

func (r *BadResponse) IsErrType(name string) bool {
	return r.ErrorType == name
}

func (r *BadResponse) IsErrMsg(msg string) bool {
	return r.ErrorMessage == msg
}

func (r *BadResponse) IsErrorCode(code string) (bool, string) {
	for _, errDetail := range r.ErrorDetails {
		if errDetail.Code == code {
			return true, errDetail.Attribute
		}
	}
	return false, ""
}

func (r *BadResponse) IsErrorCodeAttribute(code string, attribute string) bool {
	for _, errDetail := range r.ErrorDetails {
		if errDetail.Code == code && errDetail.Attribute == attribute {
			return true
		}
	}
	return false
}

func (r *BadResponse) IsAuthError() bool {
	if r.IsStatusCode(400) &&
		r.IsErrType(ErrorTypeParamaterError) &&
		r.IsErrorCodeAttribute("invalid", "access_token") {
		return true
	}
	return false
}

func (r *BadResponse) IsRequestFormatError() bool {
	if r.IsStatusCode(400) &&
		r.IsErrType(ErrorTypeParamaterError) &&
		r.IsErrMsg("JSON parse error occurred.") {
		return true
	}
	return false
}

func (r *BadResponse) IsParameterError() bool {
	if r.IsStatusCode(400) &&
		r.IsErrType(ErrorTypeParamaterError) &&
		!r.IsAuthError() &&
		!r.IsRequestFormatError() {
		return true
	}

	return false
}

func (r *BadResponse) IsNotFound() bool {
	return r.IsStatusCode(404)
}

func (r *BadResponse) IsTooManyRequests() bool {
	return r.IsStatusCode(429)
}

func (r *BadResponse) IsSystemError() bool {
	return r.IsStatusCode(500)
}

func (r *BadResponse) IsGatewayTimeout() bool {
	return r.IsStatusCode(504)
}

func (r *BadResponse) IsInvalidSchema() bool {
	return r.IsParameterError() && r.IsErrorCodeAttribute("invalid", "schema")
}

type ErrorDetails []ErrorDetail

func (e ErrorDetails) Error() string {
	res := []string{}
	for _, detail := range e {
		res = append(res, detail.Error())
	}
	return strings.Join(res, ", ")
}

type ErrorDetail struct {
	Code      string `read:"code"`
	Attribute string `read:"attribute"`
}

func (e ErrorDetail) Error() string {
	return e.Code + "=" + e.Attribute
}

type Count struct {
	Count int32 `read:"count" json:"-"`
}

func (c *Count) SetCount(v int32) { c.Count = v }
func (c *Count) GetCount() int32  { return c.Count }

type AsyncResponse struct {
	ResponseCommon `read:",inline"`
	JobsURL        string `read:"jobs_url"`
}