-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest_internal.go
More file actions
152 lines (137 loc) · 3.52 KB
/
request_internal.go
File metadata and controls
152 lines (137 loc) · 3.52 KB
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
package httpsuite
import (
"errors"
"net/http"
"reflect"
)
var (
errNilHTTPRequest = errors.New("nil http request")
errNilRequestBody = errors.New("nil request body")
errNilParamExtractor = errors.New("nil param extractor")
errInvalidRequestType = errors.New("invalid request target")
errValidationFailed = errors.New("validation error")
)
func normalizeParseOptions(opts *ParseOptions) ParseOptions {
normalized := ParseOptions{
MaxBodyBytes: defaultMaxBodyBytes,
Problems: nil,
Validator: DefaultValidator(),
}
if opts != nil {
if opts.MaxBodyBytes > 0 {
normalized.MaxBodyBytes = opts.MaxBodyBytes
}
if opts.Problems != nil {
problems := mergeProblemConfig(opts.Problems)
normalized.Problems = &problems
}
if opts.Validator != nil {
normalized.Validator = opts.Validator
}
normalized.SkipValidation = opts.SkipValidation
}
if normalized.Problems == nil {
problems := DefaultProblemConfig()
normalized.Problems = &problems
}
return normalized
}
func problemFromDecodeError(err error, problems *ProblemConfig) (*ProblemDetails, int) {
status := http.StatusBadRequest
var decodeErr *BodyDecodeError
if errors.As(err, &decodeErr) {
switch decodeErr.Kind {
case BodyDecodeErrorBodyTooLarge:
status = http.StatusRequestEntityTooLarge
return NewProblemDetails(
status,
problems.TypeURL("bad_request_error"),
"Payload Too Large",
decodeErr.Error(),
), status
case BodyDecodeErrorMultipleDocuments:
return NewProblemDetails(
status,
problems.TypeURL("bad_request_error"),
"Invalid Request",
"Request body must contain a single JSON document",
), status
default:
return NewProblemDetails(
status,
problems.TypeURL("bad_request_error"),
"Invalid Request",
decodeErr.Error(),
), status
}
}
return NewProblemDetails(
status,
problems.TypeURL("bad_request_error"),
"Invalid Request",
err.Error(),
), status
}
func problemFromPathParamError(err error, problems *ProblemConfig) (*ProblemDetails, int) {
status := http.StatusBadRequest
var pathErr *PathParamError
if errors.As(err, &pathErr) {
if pathErr.Missing {
return NewProblemDetails(
status,
problems.TypeURL("bad_request_error"),
"Missing Parameter",
"Parameter "+pathErr.Param+" not found in request",
), status
}
problem := NewProblemDetails(
status,
problems.TypeURL("bad_request_error"),
"Invalid Parameter",
"Failed to bind parameter "+pathErr.Param,
)
if pathErr.Err != nil {
problem.Extensions = map[string]interface{}{"error": pathErr.Err.Error()}
}
return problem, status
}
return NewProblemDetails(
status,
problems.TypeURL("bad_request_error"),
"Invalid Parameter",
err.Error(),
), status
}
func isRequestNil(i interface{}) bool {
if i == nil {
return true
}
value := reflect.ValueOf(i)
return value.Kind() == reflect.Ptr && value.IsNil()
}
func ensureRequestInitialized[T any](request T) (T, error) {
if !isRequestNil(request) {
return request, nil
}
value := reflect.ValueOf(request)
if !value.IsValid() || value.Kind() != reflect.Ptr {
var empty T
return empty, errInvalidRequestType
}
elem := value.Type().Elem()
if elem == nil {
var empty T
return empty, errInvalidRequestType
}
request = reflect.New(elem).Interface().(T)
return request, nil
}
func validationProblemStatus(problem *ProblemDetails) int {
if problem == nil {
return http.StatusBadRequest
}
if problem.Status >= 400 && problem.Status <= 599 {
return problem.Status
}
return http.StatusBadRequest
}