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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
package schema
import (
"errors"
"fmt"
"strings"
"github.com/rs/zerolog"
"github.com/authzed/spicedb/internal/sharederrors"
nspkg "github.com/authzed/spicedb/pkg/namespace"
"github.com/authzed/spicedb/pkg/spiceerrors"
)
// DefinitionNotFoundError occurs when a definition was not found.
type DefinitionNotFoundError struct {
error
definitionName string
}
// NotFoundNamespaceName is the name of the definition not found.
func (err DefinitionNotFoundError) NotFoundNamespaceName() string {
return err.definitionName
}
// MarshalZerologObject implements zerolog object marshalling.
func (err DefinitionNotFoundError) MarshalZerologObject(e *zerolog.Event) {
e.Err(err.error).Str("definition", err.definitionName)
}
// DetailsMetadata returns the metadata for details for this error.
func (err DefinitionNotFoundError) DetailsMetadata() map[string]string {
return map[string]string{
"definition_name": err.definitionName,
}
}
// RelationNotFoundError occurs when a relation was not found under a definition.
type RelationNotFoundError struct {
error
definitionName string
relationName string
}
// NamespaceName returns the name of the definition in which the relation was not found.
func (err RelationNotFoundError) NamespaceName() string {
return err.definitionName
}
// NotFoundRelationName returns the name of the relation not found.
func (err RelationNotFoundError) NotFoundRelationName() string {
return err.relationName
}
// MarshalZerologObject implements zerolog object marshalling.
func (err RelationNotFoundError) MarshalZerologObject(e *zerolog.Event) {
e.Err(err.error).Str("definition", err.definitionName).Str("relation", err.relationName)
}
// DetailsMetadata returns the metadata for details for this error.
func (err RelationNotFoundError) DetailsMetadata() map[string]string {
return map[string]string{
"definition_name": err.definitionName,
"relation_or_permission_name": err.relationName,
}
}
// CaveatNotFoundError occurs when a caveat was not found.
type CaveatNotFoundError struct {
error
caveatName string
}
// CaveatName returns the name of the caveat not found.
func (err CaveatNotFoundError) CaveatName() string {
return err.caveatName
}
// MarshalZerologObject implements zerolog object marshalling.
func (err CaveatNotFoundError) MarshalZerologObject(e *zerolog.Event) {
e.Err(err.error).Str("caveat", err.caveatName)
}
// DetailsMetadata returns the metadata for details for this error.
func (err CaveatNotFoundError) DetailsMetadata() map[string]string {
return map[string]string{
"caveat_name": err.caveatName,
}
}
// DuplicateRelationError occurs when a duplicate relation was found inside a definition.
type DuplicateRelationError struct {
error
definitionName string
relationName string
}
// MarshalZerologObject implements zerolog object marshalling.
func (err DuplicateRelationError) MarshalZerologObject(e *zerolog.Event) {
e.Err(err.error).Str("definition", err.definitionName).Str("relation", err.relationName)
}
// DetailsMetadata returns the metadata for details for this error.
func (err DuplicateRelationError) DetailsMetadata() map[string]string {
return map[string]string{
"definition_name": err.definitionName,
"relation_or_permission_name": err.relationName,
}
}
// PermissionUsedOnLeftOfArrowError occurs when a permission is used on the left side of an arrow
// expression.
type PermissionUsedOnLeftOfArrowError struct {
error
definitionName string
parentPermissionName string
foundPermissionName string
}
// MarshalZerologObject implements zerolog object marshalling.
func (err PermissionUsedOnLeftOfArrowError) MarshalZerologObject(e *zerolog.Event) {
e.Err(err.error).Str("definition", err.definitionName).Str("permission", err.parentPermissionName).Str("usedPermission", err.foundPermissionName)
}
// DetailsMetadata returns the metadata for details for this error.
func (err PermissionUsedOnLeftOfArrowError) DetailsMetadata() map[string]string {
return map[string]string{
"definition_name": err.definitionName,
"permission_name": err.parentPermissionName,
"used_permission_name": err.foundPermissionName,
}
}
// WildcardUsedInArrowError occurs when an arrow operates over a relation that contains a wildcard.
type WildcardUsedInArrowError struct {
error
definitionName string
parentPermissionName string
accessedRelationName string
}
// MarshalZerologObject implements zerolog object marshalling.
func (err WildcardUsedInArrowError) MarshalZerologObject(e *zerolog.Event) {
e.Err(err.error).Str("definition", err.definitionName).Str("parentPermissionName", err.parentPermissionName).Str("accessedRelationName", err.accessedRelationName)
}
// DetailsMetadata returns the metadata for details for this error.
func (err WildcardUsedInArrowError) DetailsMetadata() map[string]string {
return map[string]string{
"definition_name": err.definitionName,
"permission_name": err.parentPermissionName,
"accessed_relation_name": err.accessedRelationName,
}
}
// MissingAllowedRelationsError occurs when a relation is defined without any type information.
type MissingAllowedRelationsError struct {
error
definitionName string
relationName string
}
// MarshalZerologObject implements zerolog object marshalling.
func (err MissingAllowedRelationsError) MarshalZerologObject(e *zerolog.Event) {
e.Err(err.error).Str("definition", err.definitionName).Str("relation", err.relationName)
}
// DetailsMetadata returns the metadata for details for this error.
func (err MissingAllowedRelationsError) DetailsMetadata() map[string]string {
return map[string]string{
"definition_name": err.definitionName,
"relation_name": err.relationName,
}
}
// TransitiveWildcardError occurs when a wildcard relation in turn references another wildcard
// relation.
type TransitiveWildcardError struct {
error
definitionName string
relationName string
}
// MarshalZerologObject implements zerolog object marshalling.
func (err TransitiveWildcardError) MarshalZerologObject(e *zerolog.Event) {
e.Err(err.error).Str("definition", err.definitionName).Str("relation", err.relationName)
}
// DetailsMetadata returns the metadata for details for this error.
func (err TransitiveWildcardError) DetailsMetadata() map[string]string {
return map[string]string{
"definition_name": err.definitionName,
"relation_name": err.relationName,
}
}
// PermissionsCycleError occurs when a cycle exists within permissions.
type PermissionsCycleError struct {
error
definitionName string
permissionNames []string
}
// MarshalZerologObject implements zerolog object marshalling.
func (err PermissionsCycleError) MarshalZerologObject(e *zerolog.Event) {
e.Err(err.error).Str("definition", err.definitionName).Str("permissions", strings.Join(err.permissionNames, ", "))
}
// DetailsMetadata returns the metadata for details for this error.
func (err PermissionsCycleError) DetailsMetadata() map[string]string {
return map[string]string{
"definition_name": err.definitionName,
"permission_names": strings.Join(err.permissionNames, ","),
}
}
// DuplicateAllowedRelationError indicates that an allowed relation was redefined on a relation.
type DuplicateAllowedRelationError struct {
error
definitionName string
relationName string
allowedRelationSource string
}
// MarshalZerologObject implements zerolog object marshalling.
func (err DuplicateAllowedRelationError) MarshalZerologObject(e *zerolog.Event) {
e.Err(err.error).Str("definition", err.definitionName).Str("relation", err.relationName).Str("allowed-relation", err.allowedRelationSource)
}
// DetailsMetadata returns the metadata for details for this error.
func (err DuplicateAllowedRelationError) DetailsMetadata() map[string]string {
return map[string]string{
"definition_name": err.definitionName,
"relation_name": err.relationName,
"allowed_relation": err.allowedRelationSource,
}
}
// UnusedCaveatParameterError indicates that a caveat parameter is unused in the caveat expression.
type UnusedCaveatParameterError struct {
error
caveatName string
paramName string
}
// MarshalZerologObject implements zerolog object marshalling.
func (err UnusedCaveatParameterError) MarshalZerologObject(e *zerolog.Event) {
e.Err(err.error).Str("caveat", err.caveatName).Str("param", err.paramName)
}
// DetailsMetadata returns the metadata for details for this error.
func (err UnusedCaveatParameterError) DetailsMetadata() map[string]string {
return map[string]string{
"caveat_name": err.caveatName,
"parameter_name": err.paramName,
}
}
// NewDefinitionNotFoundErr constructs a new definition not found error.
func NewDefinitionNotFoundErr(nsName string) error {
return DefinitionNotFoundError{
error: fmt.Errorf("object definition `%s` not found", nsName),
definitionName: nsName,
}
}
// NewRelationNotFoundErr constructs a new relation not found error.
func NewRelationNotFoundErr(nsName string, relationName string) error {
return RelationNotFoundError{
error: fmt.Errorf("relation/permission `%s` not found under definition `%s`", relationName, nsName),
definitionName: nsName,
relationName: relationName,
}
}
// NewCaveatNotFoundErr constructs a new caveat not found error.
func NewCaveatNotFoundErr(caveatName string) error {
return CaveatNotFoundError{
error: fmt.Errorf("caveat `%s` not found", caveatName),
caveatName: caveatName,
}
}
// NewDuplicateRelationError constructs an error indicating that a relation was defined more than once in a definition.
func NewDuplicateRelationError(nsName string, relationName string) error {
return DuplicateRelationError{
error: fmt.Errorf("found duplicate relation/permission name `%s` under definition `%s`", relationName, nsName),
definitionName: nsName,
relationName: relationName,
}
}
// NewDuplicateAllowedRelationErr constructs an error indicating that an allowed relation was defined more than once for a relation.
func NewDuplicateAllowedRelationErr(nsName string, relationName string, allowedRelationSource string) error {
return DuplicateAllowedRelationError{
error: fmt.Errorf("found duplicate allowed subject type `%s` on relation `%s` under definition `%s`", allowedRelationSource, relationName, nsName),
definitionName: nsName,
relationName: relationName,
allowedRelationSource: allowedRelationSource,
}
}
// NewPermissionUsedOnLeftOfArrowErr constructs an error indicating that a permission was used on the left side of an arrow.
func NewPermissionUsedOnLeftOfArrowErr(nsName string, parentPermissionName string, foundPermissionName string) error {
return PermissionUsedOnLeftOfArrowError{
error: fmt.Errorf("under permission `%s` under definition `%s`: permissions cannot be used on the left hand side of an arrow (found `%s`)", parentPermissionName, nsName, foundPermissionName),
definitionName: nsName,
parentPermissionName: parentPermissionName,
foundPermissionName: foundPermissionName,
}
}
// NewWildcardUsedInArrowErr constructs an error indicating that an arrow operated over a relation with a wildcard type.
func NewWildcardUsedInArrowErr(nsName string, parentPermissionName string, foundRelationName string, wildcardTypeName string, wildcardRelationName string) error {
return WildcardUsedInArrowError{
error: fmt.Errorf("for arrow under permission `%s`: relation `%s#%s` includes wildcard type `%s` via relation `%s`: wildcard relations cannot be used on the left side of arrows", parentPermissionName, nsName, foundRelationName, wildcardTypeName, wildcardRelationName),
definitionName: nsName,
parentPermissionName: parentPermissionName,
accessedRelationName: foundRelationName,
}
}
// NewMissingAllowedRelationsErr constructs an error indicating that type information is missing for a relation.
func NewMissingAllowedRelationsErr(nsName string, relationName string) error {
return MissingAllowedRelationsError{
error: fmt.Errorf("at least one allowed relation/permission is required to be defined for relation `%s`", relationName),
definitionName: nsName,
relationName: relationName,
}
}
// NewTransitiveWildcardErr constructs an error indicating that a transitive wildcard exists.
func NewTransitiveWildcardErr(nsName string, relationName string, foundRelationNamespace string, foundRelationName string, wildcardTypeName string, wildcardRelationReference string) error {
return TransitiveWildcardError{
error: fmt.Errorf("for relation `%s`: relation/permission `%s#%s` includes wildcard type `%s` via relation `%s`: wildcard relations cannot be transitively included", relationName, foundRelationNamespace, foundRelationName, wildcardTypeName, wildcardRelationReference),
definitionName: nsName,
relationName: relationName,
}
}
// NewPermissionsCycleErr constructs an error indicating that a cycle exists amongst permissions.
func NewPermissionsCycleErr(nsName string, permissionNames []string) error {
return PermissionsCycleError{
error: fmt.Errorf("under definition `%s`, there exists a cycle in permissions: %s", nsName, strings.Join(permissionNames, ", ")),
definitionName: nsName,
permissionNames: permissionNames,
}
}
// NewUnusedCaveatParameterErr constructs indicating that a parameter was unused in a caveat expression.
func NewUnusedCaveatParameterErr(caveatName string, paramName string) error {
return UnusedCaveatParameterError{
error: fmt.Errorf("parameter `%s` for caveat `%s` is unused", paramName, caveatName),
caveatName: caveatName,
paramName: paramName,
}
}
// asTypeError wraps another error in a type error.
func asTypeError(wrapped error) error {
if wrapped == nil {
return nil
}
var te TypeError
if errors.As(wrapped, &te) {
return wrapped
}
return TypeError{wrapped}
}
// TypeError wraps another error as a type error.
type TypeError struct {
error
}
func (err TypeError) Unwrap() error {
return err.error
}
var (
_ sharederrors.UnknownNamespaceError = DefinitionNotFoundError{}
_ sharederrors.UnknownRelationError = RelationNotFoundError{}
)
// NewTypeWithSourceError creates a new type error at the specific position and with source code, wrapping the underlying
// error.
func NewTypeWithSourceError(wrapped error, withSource nspkg.WithSourcePosition, sourceCodeString string) error {
sourcePosition := withSource.GetSourcePosition()
if sourcePosition != nil {
return asTypeError(spiceerrors.NewWithSourceError(
wrapped,
sourceCodeString,
sourcePosition.ZeroIndexedLineNumber+1, // +1 to make 1-indexed
sourcePosition.ZeroIndexedColumnPosition+1, // +1 to make 1-indexed
))
}
return asTypeError(spiceerrors.NewWithSourceError(
wrapped,
sourceCodeString,
0,
0,
))
}
|