Skip to content

Commit 634648d

Browse files
authored
fix: error message for availability month is now passed to the user (#800)
Fixes #795.
1 parent 34034c4 commit 634648d

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

pkg/httperrors/errors.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ func DBError(c *gin.Context, err error) ErrorStatus {
6565
return ErrorStatus{Status: http.StatusNotFound, Err: errors.New("there is no resource for the ID you specified")}
6666
}
6767

68+
// Availability month is set before the month of the transaction
69+
if strings.Contains(err.Error(), "availability month must not be earlier than the month of the transaction") {
70+
return ErrorStatus{Status: http.StatusBadRequest, Err: err}
71+
}
72+
6873
// Account name must be unique per Budget
6974
if strings.Contains(err.Error(), "UNIQUE constraint failed: accounts.name, accounts.budget_id") {
7075
return ErrorStatus{Status: http.StatusBadRequest, Err: errors.New("the account name must be unique for the budget")}

pkg/httperrors/errors_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ func TestDatabaseErrorMessages(t *testing.T) {
171171
err string
172172
msg string
173173
}{
174+
{http.StatusBadRequest, "availability month must not be earlier than the month of the transaction, transaction date: 2023-10-22, available month 2023-09", "availability month must not be earlier than the month of the transaction, transaction date: 2023-10-22, available month 2023-09"},
174175
{http.StatusBadRequest, "CHECK constraint failed: source_destination_different", "source and destination accounts for a transaction must be different"},
175176
{http.StatusBadRequest, "UNIQUE constraint failed: accounts.name, accounts.budget_id", "the account name must be unique for the budget"},
176177
{http.StatusBadRequest, "UNIQUE constraint failed: categories.name, categories.budget_id", "the category name must be unique for the budget"},

pkg/models/transaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (t *Transaction) BeforeSave(tx *gorm.DB) (err error) {
9797
if t.AvailableFrom.IsZero() {
9898
t.AvailableFrom = types.MonthOf(t.Date)
9999
} else if t.AvailableFrom.Before(types.MonthOf(t.Date)) {
100-
return fmt.Errorf("availability month must not be earlier than the month of the transaction, transaction date: %s, available month %s", t.Date, t.AvailableFrom)
100+
return fmt.Errorf("availability month must not be earlier than the month of the transaction, transaction date: %s, available month %s", t.Date.Format("2006-01-02"), t.AvailableFrom)
101101
}
102102

103103
// Enforce ReconciledSource = false when source account is external

0 commit comments

Comments
 (0)