-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
103 lines (82 loc) · 2.29 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
95
96
97
98
99
100
101
102
103
/*
Copyright (C) 2017 Jorge Martinez Hernandez
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package database
// DBError is the error type returned when any database operation
// does not return expected result or cannot be carried out.
type DBError interface {
String() string
Code() int
Error() string
}
type (
// Conflict is a DBError returned when a database operation
// cannot be carried out because it conflicts with a previous operation.
Conflict struct {
msg string
}
// NotFound is DBError returned when an object cannot be found in the
// database.
NotFound struct {
msg string
}
// BadRequest is a DBError returned when an operation is malformed.
BadRequest struct {
msg string
}
// Unauthorized is a DBError returned when a client does not have the permissions
// to carry out an operation
Unauthorized struct {
msg string
}
)
func (e Conflict) Error() string {
return e.msg
}
func (e Conflict) String() string {
return "Conflict"
}
// Code returns Conflict's corresponding error code
func (e Conflict) Code() int {
return 409
}
func (e NotFound) Error() string {
return e.msg
}
func (e NotFound) String() string {
return "NotFound"
}
// Code returns NotFound's corresponding error code
func (e NotFound) Code() int {
return 404
}
func (e BadRequest) Error() string {
return e.msg
}
func (e BadRequest) String() string {
return "BadRequest"
}
// Code returns BadRequest's corresponding error code
func (e BadRequest) Code() int {
return 400
}
func (e Unauthorized) Error() string {
return e.msg
}
func (e Unauthorized) String() string {
return "Unauthorized"
}
// Code returns Unauthorized's corresponding error code
func (e Unauthorized) Code() int {
return 401
}