/
status.go
124 lines (96 loc) · 2.61 KB
/
status.go
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
package pphttp
import (
"strconv"
"strings"
"github.com/pinpoint-apm/pinpoint-go-agent"
)
type httpStatusCode interface {
isError(code int) bool
}
type httpStatusInformational struct{}
func newHttpStatusInformational() *httpStatusInformational {
return &httpStatusInformational{}
}
func (h *httpStatusInformational) isError(code int) bool {
return 100 <= code && code <= 199
}
type httpStatusSuccess struct{}
func newHttpStatusSuccess() *httpStatusSuccess {
return &httpStatusSuccess{}
}
func (h *httpStatusSuccess) isError(code int) bool {
return 200 <= code && code <= 299
}
type httpStatusRedirection struct{}
func newHttpStatusRedirection() *httpStatusRedirection {
return &httpStatusRedirection{}
}
func (h *httpStatusRedirection) isError(code int) bool {
return 300 <= code && code <= 399
}
type httpStatusClientError struct{}
func newHttpStatusClientError() *httpStatusClientError {
return &httpStatusClientError{}
}
func (h *httpStatusClientError) isError(code int) bool {
return 400 <= code && code <= 499
}
type httpStatusServerError struct{}
func newHttpStatusServerError() *httpStatusServerError {
return &httpStatusServerError{}
}
func (h *httpStatusServerError) isError(code int) bool {
return 500 <= code && code <= 599
}
type httpStatusDefault struct {
statusCode int
}
func newHttpStatusDefault(code int) *httpStatusDefault {
return &httpStatusDefault{
statusCode: code,
}
}
func (h *httpStatusDefault) isError(code int) bool {
return h.statusCode == code
}
type httpStatusError struct {
errors []httpStatusCode
}
func newHttpStatusError() *httpStatusError {
return &httpStatusError{
errors: setupHttpStatusErrors(),
}
}
func setupHttpStatusErrors() []httpStatusCode {
var errors []httpStatusCode
cfgErrors := pinpoint.GetConfig().StringSlice(CfgHttpServerStatusCodeErrors)
trimStringSlice(cfgErrors)
for _, s := range cfgErrors {
if strings.EqualFold(s, "5xx") {
errors = append(errors, newHttpStatusServerError())
} else if strings.EqualFold(s, "4xx") {
errors = append(errors, newHttpStatusClientError())
} else if strings.EqualFold(s, "3xx") {
errors = append(errors, newHttpStatusRedirection())
} else if strings.EqualFold(s, "2xx") {
errors = append(errors, newHttpStatusSuccess())
} else if strings.EqualFold(s, "1xx") {
errors = append(errors, newHttpStatusInformational())
} else {
c, e := strconv.Atoi(s)
if e != nil {
c = -1
}
errors = append(errors, newHttpStatusDefault(c))
}
}
return errors
}
func (h *httpStatusError) isError(code int) bool {
for _, h := range h.errors {
if h.isError(code) {
return true
}
}
return false
}