/
errors.go
78 lines (63 loc) · 1.6 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
package db_repo
import (
"errors"
"fmt"
)
type RecordNotFoundError struct {
id uint
modelId string
err error
}
func NewRecordNotFoundError(id uint, modelId string, err error) RecordNotFoundError {
return RecordNotFoundError{
id: id,
modelId: modelId,
err: err,
}
}
func (e RecordNotFoundError) Error() string {
return fmt.Sprintf("could not find model of type %s with id %d: %s", e.modelId, e.id, e.err)
}
func (e RecordNotFoundError) Unwrap() error {
return e.err
}
func IsRecordNotFoundError(err error) bool {
return errors.As(err, &RecordNotFoundError{})
}
type NoQueryResultsError struct {
modelId string
err error
}
func NewNoQueryResultsError(modelId string, err error) NoQueryResultsError {
return NoQueryResultsError{
modelId: modelId,
err: err,
}
}
func (e NoQueryResultsError) Error() string {
return fmt.Sprintf("could not find any results for model type %s: %s", e.modelId, e.err)
}
func (e NoQueryResultsError) Unwrap() error {
return e.err
}
func IsNoQueryResultsError(err error) bool {
return errors.As(err, &NoQueryResultsError{})
}
type TooManyResultsError struct {
modelId string
found int
expected int
}
func NewTooManyResultsError(found int, expected int, modelId string) TooManyResultsError {
return TooManyResultsError{
modelId: modelId,
found: found,
expected: expected,
}
}
func (e TooManyResultsError) Error() string {
return fmt.Sprintf("found %d result(s), expected %d result(s) for model type %s", e.found, e.expected, e.modelId)
}
func IsTooManyResultsError(err error) bool {
return errors.As(err, &TooManyResultsError{})
}