This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
/
errors.go
94 lines (81 loc) · 2.47 KB
/
errors.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
// Copyright 2020 Google LLC
//
// 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 database
import (
"errors"
"fmt"
"sort"
)
var (
// ErrNoSigningKeyManager is the error returned when the key manager cannot be
// used as a SigningKeyManager.
ErrNoSigningKeyManager = errors.New("configured key manager cannot be used to manage per-realm keys")
// ErrValidationFailed is the error returned when validation failed. This
// should always be considered user error.
ErrValidationFailed = errors.New("validation failed")
)
// Errorable defines an embeddable struct for managing errors on models.
type Errorable struct {
// errors is the list of errors on the model, usually from validation. The
// string key is the column name (or virtual column name) of the field that
// has errors.
errors map[string][]string
}
// AddError adds a new error to the list.
func (e *Errorable) AddError(key, err string) {
e.init()
e.errors[key] = append(e.errors[key], err)
}
// Errors returns the list of errors.
func (e *Errorable) Errors() map[string][]string {
e.init()
return e.errors
}
// ErrorMessages returns the list of error messages.
func (e *Errorable) ErrorMessages() []string {
e.init()
// Sort keys so the response is in predictable ordering.
keys := make([]string, 0, len(e.errors))
for k := range e.errors {
keys = append(keys, k)
}
sort.Strings(keys)
l := make([]string, 0, len(e.errors))
for _, k := range keys {
v := e.errors[k]
for _, msg := range v {
l = append(l, fmt.Sprintf("%s %s", k, msg))
}
}
return l
}
// ErrorsFor returns the list of errors for the key
func (e *Errorable) ErrorsFor(key string) []string {
e.init()
return e.errors[key]
}
// ErrorOrNil returns ErrValidationFailed if there are any errors, or nil if
// there are none.
func (e *Errorable) ErrorOrNil() error {
e.init()
if len(e.errors) == 0 {
return nil
}
return ErrValidationFailed
}
func (e *Errorable) init() {
if e.errors == nil {
e.errors = make(map[string][]string)
}
}