From 841df050228c89dd6b7aa5670ae23701556c7fe1 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Sun, 30 Aug 2015 20:41:47 -0400 Subject: [PATCH] Transaction.Balanced: Return and handle errors --- transactions.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/transactions.go b/transactions.go index 3d50dca..f9427e5 100644 --- a/transactions.go +++ b/transactions.go @@ -98,17 +98,17 @@ func (t *Transaction) Valid() bool { return true } -func (t *Transaction) Balanced() bool { +func (t *Transaction) Balanced() (bool, error) { var zero big.Rat sums := make(map[int64]big.Rat) if !t.Valid() { - return false // TODO Open question: should we report an error here instead? + return false, errors.New("Transaction invalid") } for i := range t.Splits { account, err := GetAccount(t.Splits[i].AccountId, t.UserId) if err != nil { - return false + return false, err } amount, _ := t.Splits[i].GetAmount() sum := sums[account.SecurityId] @@ -117,10 +117,10 @@ func (t *Transaction) Balanced() bool { } for _, security_sum := range sums { if security_sum.Cmp(&zero) != 0 { - return false + return false, nil } } - return true + return true, nil } func GetTransaction(transactionid int64, userid int64) (*Transaction, error) { @@ -418,7 +418,13 @@ func TransactionHandler(w http.ResponseWriter, r *http.Request) { transaction.TransactionId = -1 transaction.UserId = user.UserId - if !transaction.Valid() || !transaction.Balanced() { + balanced, err := transaction.Balanced() + if err != nil { + WriteError(w, 999 /*Internal Error*/) + log.Print(err) + return + } + if !transaction.Valid() || !balanced { WriteError(w, 3 /*Invalid Request*/) return } @@ -498,7 +504,13 @@ func TransactionHandler(w http.ResponseWriter, r *http.Request) { } transaction.UserId = user.UserId - if !transaction.Valid() || !transaction.Balanced() { + balanced, err := transaction.Balanced() + if err != nil { + WriteError(w, 999 /*Internal Error*/) + log.Print(err) + return + } + if !transaction.Valid() || !balanced { WriteError(w, 3 /*Invalid Request*/) return }