Skip to content

Commit

Permalink
Merge branch 'main' into feature/num-288-remove-response-wrapper-n-le…
Browse files Browse the repository at this point in the history
…dger-api
  • Loading branch information
flemzord authored Jan 19, 2022
2 parents 46ddcc6 + 9c03790 commit f600702
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
34 changes: 23 additions & 11 deletions pkg/api/controllers/transaction_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,16 @@ func (ctl *TransactionController) PostTransaction(c *gin.Context) {
switch eerr.Code {
case storage.ConstraintFailed:
ctl.responseError(c, http.StatusConflict, err)
return
default:
ctl.responseError(c, http.StatusInternalServerError, err)
}
case *ledger.InsufficientFundError:
ctl.responseError(c, http.StatusBadRequest, err)
case *ledger.ValidationError:
ctl.responseError(c, http.StatusBadRequest, err)
default:
ctl.responseError(c, http.StatusInternalServerError, err)
}
ctl.responseError(
c,
http.StatusInternalServerError,
err,
)
return
}
ctl.response(
Expand Down Expand Up @@ -150,11 +152,21 @@ func (ctl *TransactionController) RevertTransaction(c *gin.Context) {
l, _ := c.Get("ledger")
err := l.(*ledger.Ledger).RevertTransaction(c.Request.Context(), c.Param("txid"))
if err != nil {
ctl.responseError(
c,
http.StatusInternalServerError,
err,
)
switch eerr := err.(type) {
case *storage.Error:
switch eerr.Code {
case storage.ConstraintFailed:
ctl.responseError(c, http.StatusConflict, err)
default:
ctl.responseError(c, http.StatusInternalServerError, err)
}
case *ledger.InsufficientFundError:
ctl.responseError(c, http.StatusBadRequest, err)
case *ledger.ValidationError:
ctl.responseError(c, http.StatusBadRequest, err)
default:
ctl.responseError(c, http.StatusInternalServerError, err)
}
return
}
ctl.response(
Expand Down
29 changes: 29 additions & 0 deletions pkg/ledger/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ledger

import "fmt"

type InsufficientFundError struct {
Asset string
}

func (e InsufficientFundError) Error() string {
return fmt.Sprintf("balance.insufficient.%s", e.Asset)
}

func NewInsufficientFundError(asset string) *InsufficientFundError {
return &InsufficientFundError{Asset: asset}
}

type ValidationError struct {
Msg string
}

func (v ValidationError) Error() string {
return v.Msg
}

func NewValidationError(msg string) *ValidationError {
return &ValidationError{
Msg: msg,
}
}
17 changes: 8 additions & 9 deletions pkg/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (l *Ledger) Commit(ctx context.Context, ts []core.Transaction) ([]core.Tran
for i := range ts {

if len(ts[i].Postings) == 0 {
return ts, errors.New("transaction has no postings")
return ts, NewValidationError("transaction has no postings")
}

ts[i].ID = count + int64(i)
Expand All @@ -67,6 +67,9 @@ func (l *Ledger) Commit(ctx context.Context, ts []core.Transaction) ([]core.Tran
last = &ts[i]

for _, p := range ts[i].Postings {
if p.Amount < 0 {
return ts, NewValidationError("negative amount")
}
if _, ok := rf[p.Source]; !ok {
rf[p.Source] = map[string]int64{}
}
Expand Down Expand Up @@ -101,7 +104,6 @@ func (l *Ledger) Commit(ctx context.Context, ts []core.Transaction) ([]core.Tran
}

balances, err := l.store.AggregateBalances(ctx, addr)

if err != nil {
return ts, err
}
Expand All @@ -110,10 +112,7 @@ func (l *Ledger) Commit(ctx context.Context, ts []core.Transaction) ([]core.Tran
balance, ok := balances[asset]

if !ok || balance < checks[asset] {
return ts, fmt.Errorf(
"balance.insufficient.%s",
asset,
)
return ts, NewInsufficientFundError(asset)
}
}
}
Expand Down Expand Up @@ -228,13 +227,13 @@ func (l *Ledger) SaveMeta(ctx context.Context, targetType string, targetID strin
defer unlock()

if targetType == "" {
return errors.New("empty target type")
return NewValidationError("empty target type")
}
if targetType != targetTypeTransaction && targetType != targetTypeAccount {
return fmt.Errorf("unknown target type '%s'", targetType)
return NewValidationError(fmt.Sprintf("unknown target type '%s'", targetType))
}
if targetID == "" {
return errors.New("empty target id")
return NewValidationError("empty target id")
}

lastMetaID, err := l.store.LastMetaID(ctx)
Expand Down

0 comments on commit f600702

Please sign in to comment.