-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
errors.go
46 lines (38 loc) · 982 Bytes
/
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
// Package errors wraps driver and changeset error as a grimoire's error.
package errors
type Kind int
const (
Unexpected Kind = iota
Changeset
NotFound
UniqueConstraint
ForeignKeyConstraint
CheckConstraint
)
// Error defines information about grimoire's error.
type Error struct {
Message string `json:"message"`
Field string `json:"field,omitempty"`
Code int `json:"code,omitempty"`
kind Kind
}
// Error prints error message.
func (e Error) Error() string {
return e.Message
}
// Kind of error.
func (e Error) Kind() Kind {
return e.kind
}
// New creates an error.
func New(message string, field string, kind Kind) Error {
return Error{message, field, 0, kind}
}
// NewWithCode creates an error with code.
func NewWithCode(message string, field string, code int, kind Kind) Error {
return Error{message, field, code, kind}
}
// NewUnexpected creates an error.
func NewUnexpected(message string) Error {
return Error{message, "", 0, Unexpected}
}