Repository navigation
Expand file tree
/
Copy pathdb_errors.go
More file actions
191 lines (143 loc) · 5.06 KB
/
Copy pathdb_errors.go
File metadata and controls
191 lines (143 loc) · 5.06 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package errors
import (
"errors"
"github.com/lib/pq"
)
type UniqueViolationError struct {
message string
}
var _ error = (*UniqueViolationError)(nil)
func newUniqueViolationError(msg string) UniqueViolationError {
return UniqueViolationError{message: msg}
}
func (s UniqueViolationError) Error() string {
return s.message
}
var (
ErrDuplicateEmail = newUniqueViolationError("email already exists")
ErrDuplicateUsername = newUniqueViolationError("username already exists")
)
// -----------------------------------------------
type ForeignKeyViolationError struct {
message string
}
var _ error = (*ForeignKeyViolationError)(nil)
func newForeignKeyViolationError(msg string) ForeignKeyViolationError {
return ForeignKeyViolationError{message: msg}
}
func (s ForeignKeyViolationError) Error() string {
return s.message
}
var (
ErrComments_PostParentIdFkViolation = newForeignKeyViolationError("invalid request: commenting on non-existent post")
ErrComments_CommentParentIdFkViolation = newForeignKeyViolationError("invalid request: replying on non-existent comment")
ErrFollowsM2M_FollowedIdFkViolation = newForeignKeyViolationError("invalid request: following non-existent user")
ErrPostLikesM2M_PostIdFkViolation = newForeignKeyViolationError("invalid request: liking non-existent post")
ErrUsersSavedListsM2M_ListIdFkViolation = newForeignKeyViolationError("invalid request: saving non-existent list")
ErrPostsTagsM2M_PostIdFkViolation = newForeignKeyViolationError("invalid request: tagging non-existent post")
ErrPostsTagsM2M_TagIdFkViolation = newForeignKeyViolationError("invalid request: tagging with non-existent tags")
ErrSavedPostsM2M_ListIdFkViolation = newForeignKeyViolationError("invalid request: saving into non-existent list")
ErrSavedPostsM2M_PostIdFkViolation = newForeignKeyViolationError("invalid request: saving non-existent post")
)
// -----------------------------------------------
type CheckViolationError struct {
message string
}
var _ error = (*CheckViolationError)(nil)
func newCheckViolationError(msg string) CheckViolationError {
return CheckViolationError{message: msg}
}
func (s CheckViolationError) Error() string {
return s.message
}
var ErrSelfFollowRequest = newCheckViolationError("self follow request is invalid")
// -----------------------------------------------
type RecordNotFoundError struct {
message string
}
var _ error = (*RecordNotFoundError)(nil)
func NewRecordNotFoundError(msg string) RecordNotFoundError {
return RecordNotFoundError{message: msg}
}
func (s RecordNotFoundError) Error() string {
return s.message
}
// -----------------------------------------------
type BadInputError struct {
message string
}
var _ error = (*BadInputError)(nil)
func NewBadInputError(msg string) BadInputError {
return BadInputError{message: msg}
}
func (s BadInputError) Error() string {
return s.message
}
// -----------------------------------------------
type NoRowsAffectedOnM2MEntity struct {
message string
}
var _ error = (*NoRowsAffectedOnM2MEntity)(nil)
func NewNoRowsAffectedOnM2MEntity(msg string) NoRowsAffectedOnM2MEntity {
return NoRowsAffectedOnM2MEntity{message: msg}
}
func (s NoRowsAffectedOnM2MEntity) Error() string {
return s.message
}
// -----------------------------------------------
type UnexpectedDBError struct {
err error
message string
}
var _ error = (*UnexpectedDBError)(nil)
func newUnexpectedDBError(err error) UnexpectedDBError {
return UnexpectedDBError{err: err, message: err.Error()}
}
func (s UnexpectedDBError) Error() string {
return s.message
}
// -----------------------------------------------
func GetDBError(err error) error {
var pqErr *pq.Error
if errors.As(err, &pqErr) {
switch pqErr.Code {
case "23505": // unique violations
switch pqErr.Constraint {
// case "tags_name_key": // ON CONFLICT DO NOTHING
// return ErrDuplicateTagName
case "users_email_key":
return ErrDuplicateEmail
case "users_username_key":
return ErrDuplicateUsername
}
// case "23502": // not null violations (handled by validations)
case "23514": // check violations
switch pqErr.Constraint {
case "cant_follow_yourself":
return ErrSelfFollowRequest
}
case "23503": // foreign key violations
switch pqErr.Constraint {
case "comments_postparentid_fkey":
return ErrComments_PostParentIdFkViolation
case "comments_commentparentid_fkey":
return ErrComments_CommentParentIdFkViolation
case "follows_m2m_followed_fkey":
return ErrFollowsM2M_FollowedIdFkViolation
case "post_likes_m2m_post_id_fkey":
return ErrPostLikesM2M_PostIdFkViolation
case "users_saved_lists_m2m_list_id_fkey":
return ErrUsersSavedListsM2M_ListIdFkViolation
case "posts_tags_m2m_post_id_fkey":
return ErrPostsTagsM2M_PostIdFkViolation
case "posts_tags_m2m_tag_id_fkey":
return ErrPostsTagsM2M_TagIdFkViolation
case "saved_posts_m2m_list_id_fkey":
return ErrSavedPostsM2M_ListIdFkViolation
case "saved_posts_m2m_post_id_fkey":
return ErrSavedPostsM2M_PostIdFkViolation
}
}
}
return newUnexpectedDBError(err)
}