/
detail.go
212 lines (184 loc) · 6.66 KB
/
detail.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright 2015-present, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package problem
import (
"net/url"
"strconv"
"strings"
"unicode/utf8"
"github.com/corestoreio/errors"
)
// Generated code has been changed ;-) to transform a balanced slice into a JSON map.
// go_generate easyjson -snake_case -omit_empty detail.go
const (
// MediaType specifies the default media type for a Detail response
MediaType = "application/problem+json"
// MediaTypeXML specifies the XML variant on the Detail Media type
MediaTypeXML = "application/problem+xml"
// DefaultURL is the default url to use for problem types
DefaultURL = "about:blank"
)
// Detail is a default problem implementation. Problem details are not a
// debugging tool for the underlying implementation; rather, they are a way to
// expose greater detail about the HTTP interface itself. Designers of new
// problem types need to carefully consider the Security Considerations, in
// particular, the risk of exposing attack vectors by exposing implementation
// internals through error messages.
//easyjson:json
type Detail struct {
// Type - A URI reference [RFC3986] that identifies the problem type. This
// specification encourages that, when dereferenced, it provide
// human-readable documentation for the problem type (e.g., using HTML
// [W3C.REC-html5-20141028]). When this member is not present, its value is
// assumed to be "about:blank".
Type string `json:"type" xml:"type"`
// Title - A short, human-readable summary of the problem type. It SHOULD
// NOT change from occurrence to occurrence of the problem, except for
// purposes of localization.
Title string `json:"title" xml:"title"`
// Status specifies the HTTP status code generated by the origin server for
// this occurrence of the problem.
Status int `json:"status,omitempty" xml:"status,omitempty"`
// Detail states a human-readable explanation specific to this occurrence of the
// problem.
Detail string `json:"detail,omitempty" xml:"detail,omitempty"`
// Instance states an URI that identifies the specific occurrence of the
// problem. This URI may or may not yield further information if
// dereferenced.
Instance string `json:"instance,omitempty" xml:"instance,omitempty"`
// Cause can reference to the underlying real cause of a problem detail.
Cause *Detail `json:"cause,omitempty" xml:"cause,omitempty"`
// Extension defines additional custom key/value pairs in a balanced slice.
// i=key and i+1=value. They will be transformed into a key/value JSON type.
Extension []string
}
// NewDetail returns a new instance of a Detail problem.
func NewDetail(title string, opts ...Option) (*Detail, error) {
d := &Detail{Type: DefaultURL, Title: title}
if err := d.Options(opts...); err != nil {
return nil, errors.Wrap(err, "[problem] NewDetail failed")
}
if err := d.Validate(); err != nil {
return nil, errors.Wrap(err, "[problem] NewDetail validation")
}
return d, nil
}
// MustNewDetail same as NewDetail but panics on error.
func MustNewDetail(title string, opts ...Option) *Detail {
d, err := NewDetail(title, opts...)
if err != nil {
panic(err)
}
return d
}
// Options applies options to the details object.
func (d *Detail) Options(opts ...Option) error {
for i, o := range opts {
if err := o(d); err != nil {
return errors.Wrapf(err, "[problem] Failed to apply option in index %d", i)
}
}
return nil
}
// Validate returns an error if the title is empty, or type is not an URI or
// Extension field is imbalanced.
func (d *Detail) Validate() error {
switch {
case d.Title == "":
return errors.NewEmptyf("[problem] Title cannot be empty")
case !isURL(d.Type): // might be wrong validation
return errors.NewNotValidf("[problem] Title cannot be empty")
case len(d.Extension)%2 == 1:
return errors.NewNotValidf("[problem] While creating a new detail the extensions are imbalanced: %v", d.Extension)
}
return nil
}
func isURL(str string) bool {
const maxURLRuneCount = 2083
const minURLRuneCount = 3
if str == "" || utf8.RuneCountInString(str) >= maxURLRuneCount || len(str) <= minURLRuneCount || strings.HasPrefix(str, ".") {
return false
}
u, err := url.Parse(str)
if err != nil {
return false
}
if strings.HasPrefix(u.Host, ".") {
return false
}
if u.Host == "" && (u.Path != "" && !strings.Contains(u.Path, ".")) {
return false
}
return true
}
// Option applies an option to the Detail object
type Option func(*Detail) error
// WithCause adds a detail as a new cause.
func WithCause(title string, opts ...Option) Option {
return func(d *Detail) error {
c, err := NewDetail(title, opts...)
if err != nil {
return errors.Wrap(err, "[problem] WithCause")
}
d.Cause = c
return nil
}
}
// WithExtensionString adds one or more key/value pairs to the Extension field.
func WithExtensionString(keyValues ...string) Option {
return func(d *Detail) error {
if len(keyValues)%2 == 1 {
return errors.NewNotValidf("[problem] Imbalanced: %v", keyValues)
}
d.Extension = append(d.Extension, keyValues...)
return nil
}
}
// More functional options can be added later ...
// WithExtensionInt adds an int to the Extension field.
func WithExtensionInt(key string, value int) Option {
return func(d *Detail) error {
d.Extension = append(d.Extension, key, strconv.Itoa(value))
return nil
}
}
// WithExtensionUint adds an uint to the Extension field.
func WithExtensionUint(key string, value uint64) Option {
return func(d *Detail) error {
d.Extension = append(d.Extension, key, strconv.FormatUint(value, 10))
return nil
}
}
// WithExtensionMapString adds a string only map to the Extension field.
func WithExtensionMapString(m map[string]string) Option {
return func(d *Detail) error {
for k, v := range m {
d.Extension = append(d.Extension, k, v)
}
return nil
}
}
// WithExtensionMapStringSlice adds a special map to the Extension field. This
// kind of map gets implemented by url.Values, http.Header, mail.Header and
// textproto.MIMEHeader.
func WithExtensionMapStringSlice(m map[string][]string) Option {
return func(d *Detail) error {
for k, sl := range m {
for _, s := range sl {
d.Extension = append(d.Extension, k, s)
}
}
return nil
}
}