...
1 package lderrors
2
3 import (
4 "fmt"
5 "sort"
6 "strings"
7
8 "golang.org/x/exp/maps"
9 )
10
11 const (
12 msgContextUninitialized = "tried to use uninitialized Context"
13 msgContextKeyEmpty = "context key must not be empty"
14 msgContextKeyNull = "context key must not be null"
15 msgContextKeyMissing = `"key" property not found in JSON context object`
16 msgContextKindEmpty = "context kind cannot be empty"
17 msgContextKindCannotBeKind = `"kind" is not a valid context kind`
18 msgContextKindMultiForSingleKind = `single context cannot have the kind "multi"`
19 msgContextKindMultiWithNoKinds = "multi-context must contain at least one kind"
20 msgContextKindMultiDuplicates = "multi-context cannot have same kind more than once"
21 msgContextKindInvalidChars = "context kind contains disallowed characters"
22 )
23
24
25 type ErrContextUninitialized struct{}
26
27
28 type ErrContextKeyEmpty struct{}
29
30
31
32
33 type ErrContextKeyNull struct{}
34
35
36
37 type ErrContextKeyMissing struct{}
38
39
40
41
42
43 type ErrContextKindEmpty struct{}
44
45
46
47 type ErrContextKindCannotBeKind struct{}
48
49
50
51 type ErrContextKindMultiForSingleKind struct{}
52
53
54
55 type ErrContextKindMultiWithNoKinds struct{}
56
57
58
59
60 type ErrContextKindMultiDuplicates struct{}
61
62
63
64 type ErrContextKindInvalidChars struct{}
65
66
67
68 type ErrContextPerKindErrors struct {
69
70
71 Errors map[string]error
72 }
73
74 func (e ErrContextUninitialized) Error() string { return msgContextUninitialized }
75 func (e ErrContextKeyEmpty) Error() string { return msgContextKeyEmpty }
76 func (e ErrContextKeyNull) Error() string { return msgContextKeyNull }
77 func (e ErrContextKeyMissing) Error() string { return msgContextKeyMissing }
78 func (e ErrContextKindEmpty) Error() string { return msgContextKindEmpty }
79 func (e ErrContextKindCannotBeKind) Error() string { return msgContextKindCannotBeKind }
80 func (e ErrContextKindMultiForSingleKind) Error() string { return msgContextKindMultiForSingleKind }
81 func (e ErrContextKindMultiWithNoKinds) Error() string { return msgContextKindMultiWithNoKinds }
82 func (e ErrContextKindMultiDuplicates) Error() string { return msgContextKindMultiDuplicates }
83 func (e ErrContextKindInvalidChars) Error() string { return msgContextKindInvalidChars }
84
85 func (e ErrContextPerKindErrors) Error() string {
86 sortedKeys := maps.Keys(e.Errors)
87 sort.Strings(sortedKeys)
88 messages := make([]string, 0, len(e.Errors))
89 for _, kind := range sortedKeys {
90 messages = append(messages, fmt.Sprintf("(%s) %s", kind, e.Errors[kind]))
91 }
92 return strings.Join(messages, ", ")
93 }
94
View as plain text