-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.go
More file actions
110 lines (93 loc) · 2.55 KB
/
Copy patherrors.go
File metadata and controls
110 lines (93 loc) · 2.55 KB
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
package picard
import (
"errors"
"fmt"
"strings"
multierror "github.com/hashicorp/go-multierror"
)
// ModelNotFoundError is returned when functions that expect to return an
// existing model cannot find one
const ModelNotFoundError Error = "Model Not Found"
// Error is a type of error that picard will return
type Error string
func (err Error) Error() string {
return string(err)
}
func multiErrorOutputter(errs []error) string {
errorStrings := []string{}
for _, err := range errs {
errorStrings = append(errorStrings, err.Error())
}
return strings.Join(errorStrings, " - ")
}
// SquashErrors turns a slice of errors into a single error
func SquashErrors(errs []error) error {
var squashedError *multierror.Error
errorMap := map[string]bool{}
for _, err := range errs {
errorString := err.Error()
// Make sure our keys are unique
if _, ok := errorMap[errorString]; !ok {
errorMap[errorString] = true
squashedError = multierror.Append(squashedError, err)
}
}
squashedError.ErrorFormat = multiErrorOutputter
return squashedError
}
// ForeignKeyError has extra information about which lookup failed
type ForeignKeyError struct {
Err error
Table string
Key string
KeyColumn string
FieldName string
}
/*
NewForeignKeyError returns a new ForeignKeyError object, populated with
extra information about which lookup failed
*/
func NewForeignKeyError(reason, table, key, keyColumn string, fieldName string) *ForeignKeyError {
return &ForeignKeyError{
Err: errors.New(reason),
Table: table,
Key: key,
KeyColumn: keyColumn,
FieldName: fieldName,
}
}
func (e *ForeignKeyError) Error() string {
return fmt.Sprintf("%s: Table '%s', Foreign Key '%s', Key '%s'", e.Err, e.Table, e.KeyColumn, e.Key)
}
func (e *ForeignKeyError) Unwrap() error {
return e.Err
}
// GetFieldName returns the Related Field Name property
func (e *ForeignKeyError) GetFieldName() string {
return e.FieldName
}
// SplitKey splits the key value into field parts
func (e *ForeignKeyError) SplitKey() []string {
return strings.Split(e.Key, separator)
}
// QueryError holds additional information about an SQL query failure
type QueryError struct {
Err error
Query string
}
/*
NewQueryError returns a new QueryError object, populated with
extra information about which query failed
*/
func NewQueryError(err error, query string) *QueryError {
return &QueryError{
Err: err,
Query: query,
}
}
func (e *QueryError) Error() string {
return fmt.Sprintf("%s: Query: %s", e.Err, e.Query)
}
func (e *QueryError) Unwrap() error {
return e.Err
}