Skip to content

Commit

Permalink
fix: error message for availability month is now passed to the user (#…
Browse files Browse the repository at this point in the history
…800)

Fixes #795.
  • Loading branch information
morremeyer authored Oct 22, 2023
1 parent 34034c4 commit 634648d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pkg/httperrors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func DBError(c *gin.Context, err error) ErrorStatus {
return ErrorStatus{Status: http.StatusNotFound, Err: errors.New("there is no resource for the ID you specified")}
}

// Availability month is set before the month of the transaction
if strings.Contains(err.Error(), "availability month must not be earlier than the month of the transaction") {
return ErrorStatus{Status: http.StatusBadRequest, Err: err}
}

// Account name must be unique per Budget
if strings.Contains(err.Error(), "UNIQUE constraint failed: accounts.name, accounts.budget_id") {
return ErrorStatus{Status: http.StatusBadRequest, Err: errors.New("the account name must be unique for the budget")}
Expand Down
1 change: 1 addition & 0 deletions pkg/httperrors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func TestDatabaseErrorMessages(t *testing.T) {
err string
msg string
}{
{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"},
{http.StatusBadRequest, "CHECK constraint failed: source_destination_different", "source and destination accounts for a transaction must be different"},
{http.StatusBadRequest, "UNIQUE constraint failed: accounts.name, accounts.budget_id", "the account name must be unique for the budget"},
{http.StatusBadRequest, "UNIQUE constraint failed: categories.name, categories.budget_id", "the category name must be unique for the budget"},
Expand Down
2 changes: 1 addition & 1 deletion pkg/models/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (t *Transaction) BeforeSave(tx *gorm.DB) (err error) {
if t.AvailableFrom.IsZero() {
t.AvailableFrom = types.MonthOf(t.Date)
} else if t.AvailableFrom.Before(types.MonthOf(t.Date)) {
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)
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)
}

// Enforce ReconciledSource = false when source account is external
Expand Down

0 comments on commit 634648d

Please sign in to comment.