forked from elimity-com/scim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
218 lines (194 loc) · 5.63 KB
/
error.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
213
214
215
216
217
218
package scim
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/elimity-com/scim/errors"
)
type scimType string
const (
// One or more of the attribute values are already in use or are reserved.
scimTypeUniqueness = "uniqueness"
// The attempted modification is not compatible with the target attribute's mutability or current state (e.g.,
// modification of an "immutable" attribute with an existing value).
scimTypeMutability = "mutability"
// The request body message structure was invalid or did not conform to the request schema.
scimTypeInvalidSyntax = "invalidSyntax"
// A required value was missing or the value specified was not compatible with the operation, attribute type
// or resource schema.
scimTypeInvalidValue = "invalidValue"
// Endpoint not implemented
scimTypeNotImplemented = "notImplemented"
)
func scimErrorResourceNotFound(id string) scimError {
return scimError{
detail: fmt.Sprintf("Resource %s not found.", id),
status: http.StatusNotFound,
}
}
func scimErrorBadParams(invalidParams []string) scimError {
var suffix string
if len(invalidParams) > 1 {
suffix = "s"
}
return scimErrorBadRequest(fmt.Sprintf(
"Bad Request. Invalid parameter%s provided in request: %s.",
suffix,
strings.Join(invalidParams, ", "),
))
}
func scimErrorBadRequest(msg string) scimError {
return scimError{
detail: msg,
status: http.StatusBadRequest,
}
}
var (
scimErrorUniqueness = scimError{
scimType: scimTypeUniqueness,
detail: "One or more of the attribute values are already in use or are reserved.",
status: http.StatusConflict,
}
scimErrorMutability = scimError{
scimType: scimTypeMutability,
detail: "The attempted modification is not compatible with the target attribute's mutability or current state.",
status: http.StatusBadRequest,
}
scimErrorInvalidSyntax = scimError{
scimType: scimTypeInvalidSyntax,
detail: "The request body message structure was invalid or did not conform to the request schema.",
status: http.StatusBadRequest,
}
scimErrorInvalidValue = scimError{
scimType: scimTypeInvalidValue,
detail: "A required value was missing, or the value specified was not compatible with the operation or attribute type, or resource schema.",
status: http.StatusBadRequest,
}
scimErrorInternalServer = scimError{
status: http.StatusInternalServerError,
}
scimErrorNotImplemented = scimError{
scimType: scimTypeNotImplemented,
status: http.StatusNotImplemented,
}
)
type scimError struct {
// scimType is a SCIM detail error keyword.
scimType scimType
// detail is a detailed human-readable message.
detail string
// status is the HTTP status code expressed as a JSON string. REQUIRED.
status int
}
func (e scimError) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Schemas []string `json:"schemas"`
ScimType scimType `json:"scimType,omitempty"`
Detail string `json:"detail,omitempty"`
Status string `json:"status"`
}{
Schemas: []string{"urn:ietf:params:scim:api:messages:2.0:Error"},
ScimType: e.scimType,
Detail: e.detail,
Status: strconv.Itoa(e.status),
})
}
func (e *scimError) UnmarshalJSON(data []byte) error {
var tmpScimError struct {
ScimType scimType
Detail string
Status string
}
err := json.Unmarshal(data, &tmpScimError)
if err != nil {
return err
}
status, err := strconv.Atoi(tmpScimError.Status)
if err != nil {
return err
}
*e = scimError{
scimType: tmpScimError.ScimType,
detail: tmpScimError.Detail,
status: status,
}
return nil
}
func scimGetError(getError errors.GetError, id string) scimError {
switch getError {
case errors.GetErrorNotImplemented:
return scimErrorNotImplemented
case errors.GetErrorResourceNotFound:
return scimErrorResourceNotFound(id)
default:
return scimErrorInternalServer
}
}
func scimGetAllError(getError errors.GetError) scimError {
switch getError {
case errors.GetErrorNotImplemented:
return scimErrorNotImplemented
default:
return scimErrorInternalServer
}
}
func scimPatchError(patchError errors.PatchError, id string) scimError {
switch patchError {
case errors.PatchErrorNotImplemented:
return scimErrorNotImplemented
case errors.PatchErrorUniqueness:
return scimErrorUniqueness
case errors.PatchErrorMutability:
return scimErrorMutability
case errors.PatchErrorResourceNotFound:
return scimErrorResourceNotFound(id)
default:
return scimErrorInternalServer
}
}
func scimPostError(postError errors.PostError) scimError {
switch postError {
case errors.PostErrorNotImplemented:
return scimErrorNotImplemented
case errors.PostErrorUniqueness:
return scimErrorUniqueness
default:
return scimErrorInternalServer
}
}
func scimPutError(putError errors.PutError, id string) scimError {
switch putError {
case errors.PutErrorNotImplemented:
return scimErrorNotImplemented
case errors.PutErrorUniqueness:
return scimErrorUniqueness
case errors.PutErrorMutability:
return scimErrorMutability
case errors.PutErrorResourceNotFound:
return scimErrorResourceNotFound(id)
default:
return scimErrorInternalServer
}
}
func scimDeleteError(deleteError errors.DeleteError, id string) scimError {
switch deleteError {
case errors.DeleteErrorNotImplemented:
return scimErrorNotImplemented
case errors.DeleteErrorResourceNotFound:
return scimErrorResourceNotFound(id)
default:
return scimErrorInternalServer
}
}
func scimValidationError(validationError errors.ValidationError) scimError {
switch validationError {
case errors.ValidationErrorInvalidSyntax:
return scimErrorInvalidSyntax
case errors.ValidationErrorInvalidValue:
return scimErrorInvalidValue
default:
return scimErrorInternalServer
}
}