Skip to content

Commit 0878cda

Browse files
authored
refactor: define all errors as variables (#837)
1 parent 414e71d commit 0878cda

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

pkg/controllers/month_config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (suite *TestSuiteStandard) TestMonthConfigsOptions() {
162162
errMsg string
163163
}{
164164
{"Bad Envelope ID", "Definitely-Not-A-UUID", "1984-03", http.StatusBadRequest, "not a valid UUID"},
165-
{"Invalid Month", envelope.Data.ID.String(), "2000-00", http.StatusBadRequest, "Could not parse the specified month"},
165+
{"Invalid Month", envelope.Data.ID.String(), "2000-00", http.StatusBadRequest, "could not parse the specified month"},
166166
{"No envelope", uuid.New().String(), "1984-03", http.StatusNoContent, ""},
167167
{"No MonthConfig", envelope.Data.ID.String(), "1984-03", http.StatusNoContent, ""},
168168
{"Existing", envelope.Data.ID.String(), "2014-05", http.StatusNoContent, ""},

pkg/httperrors/errors.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@ import (
1818
)
1919

2020
var (
21-
ErrInvalidQueryString = errors.New("the query string contains unparseable data. Please check the values")
22-
ErrInvalidUUID = errors.New("the specified resource ID is not a valid UUID")
23-
ErrNoResource = errors.New("there is no resource for the ID you specified")
24-
ErrDatabaseClosed = errors.New("there is a problem with the database connection, please try again later")
25-
ErrRequestBodyEmpty = errors.New("the request body must not be empty")
21+
ErrInvalidQueryString = errors.New("the query string contains unparseable data. Please check the values")
22+
ErrInvalidUUID = errors.New("the specified resource ID is not a valid UUID")
23+
ErrNoResource = errors.New("there is no resource for the ID you specified")
24+
ErrDatabaseClosed = errors.New("there is a problem with the database connection, please try again later")
25+
ErrDatabaseReadOnly = errors.New("the database is currently in read-only mode, please try again later")
26+
ErrRequestBodyEmpty = errors.New("the request body must not be empty")
27+
ErrInvalidMonth = errors.New("could not parse the specified month, did you use YYYY-MM format?")
28+
ErrAccountNameNotUnique = errors.New("the account name must be unique for the budget")
29+
ErrCategoryNameNotUnique = errors.New("the category name must be unique for the budget")
30+
ErrEnvelopeNameNotUniqe = errors.New("the envelope name must be unique for the category")
31+
ErrMultipleAllocations = errors.New("you can not create multiple allocations for the same month")
32+
ErrSourceEqualsDestination = errors.New("source and destination accounts for a transaction must be different")
33+
ErrReferenceResourceDoesNotExist = errors.New("a resource you are referencing in another resource does not exist")
2634
)
2735

2836
// Generate a struct containing the HTTP error on the fly.
@@ -61,7 +69,7 @@ func InvalidQueryString(c *gin.Context) {
6169
}
6270

6371
func InvalidMonth(c *gin.Context) {
64-
New(c, http.StatusBadRequest, "Could not parse the specified month, did you use YYYY-MM format?")
72+
New(c, http.StatusBadRequest, ErrInvalidMonth.Error())
6573
}
6674

6775
// GenericDBError wraps DBError with a more specific error message for not found errors.
@@ -91,38 +99,38 @@ func DBError(c *gin.Context, err error) Error {
9199

92100
// Account name must be unique per Budget
93101
if strings.Contains(err.Error(), "UNIQUE constraint failed: accounts.name, accounts.budget_id") {
94-
return Error{Status: http.StatusBadRequest, Err: errors.New("the account name must be unique for the budget")}
102+
return Error{Status: http.StatusBadRequest, Err: ErrAccountNameNotUnique}
95103
}
96104

97105
// Category names need to be unique per budget
98106
if strings.Contains(err.Error(), "UNIQUE constraint failed: categories.name, categories.budget_id") {
99-
return Error{Status: http.StatusBadRequest, Err: errors.New("the category name must be unique for the budget")}
107+
return Error{Status: http.StatusBadRequest, Err: ErrCategoryNameNotUnique}
100108
}
101109

102110
// Unique envelope names per category
103111
if strings.Contains(err.Error(), "UNIQUE constraint failed: envelopes.name, envelopes.category_id") {
104-
return Error{Status: http.StatusBadRequest, Err: errors.New("the envelope name must be unique for the category")}
112+
return Error{Status: http.StatusBadRequest, Err: ErrEnvelopeNameNotUniqe}
105113
}
106114

107115
// Only one allocation per envelope per month
108116
if strings.Contains(err.Error(), "UNIQUE constraint failed: allocations.month, allocations.envelope_id") {
109-
return Error{Status: http.StatusBadRequest, Err: errors.New("you can not create multiple allocations for the same month")}
117+
return Error{Status: http.StatusBadRequest, Err: ErrMultipleAllocations}
110118
}
111119

112120
// Source and destination accounts need to be different
113121
if strings.Contains(err.Error(), "CHECK constraint failed: source_destination_different") {
114-
return Error{Status: http.StatusBadRequest, Err: errors.New("source and destination accounts for a transaction must be different")}
122+
return Error{Status: http.StatusBadRequest, Err: ErrSourceEqualsDestination}
115123
}
116124

117125
// General message when a field references a non-existing resource
118126
if strings.Contains(err.Error(), "constraint failed: FOREIGN KEY constraint failed") {
119-
return Error{Status: http.StatusBadRequest, Err: errors.New("there is no resource for the ID you specificed in the reference to another resource")}
127+
return Error{Status: http.StatusBadRequest, Err: ErrReferenceResourceDoesNotExist}
120128
}
121129

122130
// Database is read only or file has been deleted
123131
if strings.Contains(err.Error(), "attempt to write a readonly database (1032)") {
124-
log.Error().Msgf("Database is in read-only mode. This might be due to the file being deleted: %#v", err)
125-
return Error{Status: http.StatusInternalServerError, Err: errors.New("the database is currently in read-only mode, please try again later")}
132+
log.Error().Msgf("Database is in read-only mode. This might be due to the file having been deleted: %#v", err)
133+
return Error{Status: http.StatusInternalServerError, Err: ErrDatabaseReadOnly}
126134
}
127135

128136
// A general error we do not know more about

pkg/httperrors/errors_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func TestDatabaseErrorMessages(t *testing.T) {
166166
{http.StatusBadRequest, "UNIQUE constraint failed: categories.name, categories.budget_id", "the category name must be unique for the budget"},
167167
{http.StatusBadRequest, "UNIQUE constraint failed: envelopes.name, envelopes.category_id", "the envelope name must be unique for the category"},
168168
{http.StatusBadRequest, "UNIQUE constraint failed: allocations.month, allocations.envelope_id", "you can not create multiple allocations for the same month"},
169-
{http.StatusBadRequest, "constraint failed: FOREIGN KEY constraint failed", "there is no resource for the ID you specificed in the reference to another resource"},
169+
{http.StatusBadRequest, "constraint failed: FOREIGN KEY constraint failed", "a resource you are referencing in another resource does not exist"},
170170
{http.StatusInternalServerError, "This is a very weird error", "an error occurred on the server during your request, please contact your server administrator. The request id is '', send this to your server administrator to help them finding the problem"},
171171
{http.StatusInternalServerError, "attempt to write a readonly database (1032)", "the database is currently in read-only mode, please try again later"},
172172
}
@@ -221,7 +221,7 @@ func TestDatabaseNo(t *testing.T) {
221221
{http.StatusBadRequest, "UNIQUE constraint failed: categories.name, categories.budget_id", "the category name must be unique for the budget"},
222222
{http.StatusBadRequest, "UNIQUE constraint failed: envelopes.name, envelopes.category_id", "the envelope name must be unique for the category"},
223223
{http.StatusBadRequest, "UNIQUE constraint failed: allocations.month, allocations.envelope_id", "you can not create multiple allocations for the same month"},
224-
{http.StatusBadRequest, "constraint failed: FOREIGN KEY constraint failed", "there is no resource for the ID you specificed in the reference to another resource"},
224+
{http.StatusBadRequest, "constraint failed: FOREIGN KEY constraint failed", "a resource you are referencing in another resource does not exist"},
225225
{http.StatusInternalServerError, "This is a very weird error", "an error occurred on the server during your request, please contact your server administrator. The request id is '', send this to your server administrator to help them finding the problem"},
226226
{http.StatusInternalServerError, "attempt to write a readonly database (1032)", "the database is currently in read-only mode, please try again later"},
227227
}

0 commit comments

Comments
 (0)