Skip to content

Commit

Permalink
Fix: Correct Error Code for expired invoice (#120)
Browse files Browse the repository at this point in the history
* fix code

* enable spv

* fixed

* standardised some error codes
  • Loading branch information
Tíghearnán Carroll authored Feb 21, 2022
1 parent 7d1b237 commit e7fce3d
Show file tree
Hide file tree
Showing 38 changed files with 2,334 additions and 1,142 deletions.
4 changes: 2 additions & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ func main() {
c := client.New(client.WithMaxMessageSize(10000), client.WithPongTimeout(360*time.Second))
defer c.Close()

g := e.Group("/")
rDeps := internal.SetupRestDeps(cfg, log, db, c)
g.Use(middleware.AuthUser(log, rDeps.UserService))
e.Use(middleware.AuthUser(log, rDeps.UserService))

g := e.Group("/")
// setup transports
internal.SetupHTTPEndpoints(*cfg, rDeps, g)

Expand Down
7 changes: 4 additions & 3 deletions data/sqlite/destinations.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
lathos "github.com/theflyingcodr/lathos/errs"

"github.com/libsv/payd"
"github.com/libsv/payd/errcodes"
"github.com/libsv/payd/internal"
)

Expand Down Expand Up @@ -61,7 +62,7 @@ func (s *sqliteStore) DestinationsCreate(ctx context.Context, args payd.Destinat
rows, err := tx.Query(query, sqlArgs...)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, lathos.NewErrNotFound("N0004", "destinations not found, did the create fail?")
return nil, lathos.NewErrNotFound(errcodes.ErrDestinationsFailedCreate, "destinations not found, did the create fail?")
}
}
defer func() {
Expand Down Expand Up @@ -133,12 +134,12 @@ func (s *sqliteStore) Destinations(ctx context.Context, args payd.DestinationsAr
var oo []dbOutput
if err := s.db.SelectContext(ctx, &oo, sqlDestinationsByInvoiceID, args.InvoiceID); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, lathos.NewErrNotFound("N0002", fmt.Sprintf("destinations with invoiceID %s not found", args.InvoiceID))
return nil, lathos.NewErrNotFound(errcodes.ErrDestinationsNotFound, fmt.Sprintf("destinations with invoiceID %s not found", args.InvoiceID))
}
return nil, errors.Wrapf(err, "failed to get destinations with invoiceID %s", args.InvoiceID)
}
if len(oo) == 0 {
return nil, lathos.NewErrNotFound("N0002", fmt.Sprintf("destinations with invoiceID %s not found", args.InvoiceID))
return nil, lathos.NewErrNotFound(errcodes.ErrDestinationsNotFound, fmt.Sprintf("destinations with invoiceID %s not found", args.InvoiceID))
}
outs := make([]payd.Output, len(oo))
for i := 0; i < len(oo); i++ {
Expand Down
8 changes: 5 additions & 3 deletions data/sqlite/invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/libsv/payd"
"github.com/libsv/payd/errcodes"
"github.com/pkg/errors"
lathos "github.com/theflyingcodr/lathos/errs"
)
Expand All @@ -21,6 +22,7 @@ const (
SELECT invoice_id, satoshis, description, spv_required, payment_reference, payment_received_at, expires_at, state, refund_to, refunded_at, created_at, updated_at, deleted_at
FROM invoices
WHERE invoice_id = :invoice_id
AND state != 'deleted'
`

sqlInvoices = `
Expand Down Expand Up @@ -48,7 +50,7 @@ func (s *sqliteStore) Invoice(ctx context.Context, args payd.InvoiceArgs) (*payd
var resp payd.Invoice
if err := s.db.GetContext(ctx, &resp, sqlInvoiceByID, args.InvoiceID); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, lathos.NewErrNotFound("N0001", fmt.Sprintf("invoice with invoiceID %s not found", args.InvoiceID))
return nil, lathos.NewErrNotFound(errcodes.ErrInvoiceNotFound, fmt.Sprintf("invoice with invoiceID %s not found", args.InvoiceID))
}
return nil, errors.Wrapf(err, "failed to get invoice with invoiceID %s", args.InvoiceID)
}
Expand All @@ -60,7 +62,7 @@ func (s *sqliteStore) Invoices(ctx context.Context) ([]payd.Invoice, error) {
var resp []payd.Invoice
if err := s.db.SelectContext(ctx, &resp, sqlInvoices); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, lathos.NewErrNotFound("N0002", "no invoices found")
return nil, lathos.NewErrNotFound(errcodes.ErrInvoicesNotFound, "no invoices found")
}
return nil, errors.Wrapf(err, "failed to get invoices")
}
Expand Down Expand Up @@ -130,7 +132,7 @@ func (s *sqliteStore) InvoiceDelete(ctx context.Context, args payd.InvoiceArgs)
}
if err := handleNamedExec(tx, sqlInvoiceDelete, delInv); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return lathos.NewErrNotFound("N0003", fmt.Sprintf("invoice with ID %s not found", args.InvoiceID))
return lathos.NewErrNotFound(errcodes.ErrInvoiceNotFound, fmt.Sprintf("invoice with ID %s not found", args.InvoiceID))
}
return errors.Wrapf(err, "failed to delete invoice for invoiceID %s", args.InvoiceID)
}
Expand Down
5 changes: 3 additions & 2 deletions data/sqlite/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/jmoiron/sqlx"
"github.com/libsv/go-bt/v2"
"github.com/libsv/payd"
"github.com/libsv/payd/errcodes"
"github.com/mattn/go-sqlite3"
"github.com/pkg/errors"
lathos "github.com/theflyingcodr/lathos/errs"
Expand Down Expand Up @@ -102,7 +103,7 @@ func (s *sqliteStore) TransactionCreate(ctx context.Context, req payd.Transactio

if err = handleNamedExec(tx, sqlInvoiceSetPaid, invUpdate); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return lathos.NewErrNotFound("N0007", fmt.Sprintf("invoiceID '%s' not found when updating payment received info", req.InvoiceID))
return lathos.NewErrNotFound(errcodes.ErrInvoiceNotFound, fmt.Sprintf("invoiceID '%s' not found when updating payment received info", req.InvoiceID))
}
}

Expand Down Expand Up @@ -171,7 +172,7 @@ func (s *sqliteStore) Tx(ctx context.Context, txID string) (*bt.Tx, error) {
}
if err := s.db.GetContext(ctx, &txhex, sqlTransactionGet, txID); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, lathos.NewErrNotFound("T001", fmt.Sprintf("tx '%s' not in store", txID))
return nil, lathos.NewErrNotFound(errcodes.ErrTxNotFound, fmt.Sprintf("tx '%s' not in store", txID))
}
return nil, errors.Wrapf(err, "failed to retrieve transaction for id %s", txID)
}
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
P4_HOST: "ws://p4:8445/ws"
MAPI_CALLBACK_HOST: "http://p4:8445"
PEERCHANNELS_HOST: "peerchannels:25009"
WALLET_SPVREQUIRED: 'true'
healthcheck:
test: [ "CMD", "curl", "-f", "localhost:8443/api/v1/health" ]
interval: 30s
Expand All @@ -34,6 +35,7 @@ services:
P4_HOST: "ws://p4-merchant:28445/ws"
MAPI_CALLBACK_HOST: "http://p4-merchant:28445"
PEERCHANNELS_HOST: "peerchannels:25009"
WALLET_SPVREQUIRED: 'true'
healthcheck:
test: [ "CMD", "curl", "-f", "localhost:28443/api/v1/health" ]
interval: 30s
Expand Down
6 changes: 6 additions & 0 deletions errcodes/errcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ package errcodes
const (
ErrDuplicatePayment = "D1"
ErrExpiredPayment = "E1"

ErrInvoiceNotFound = "N0001"
ErrInvoicesNotFound = "N0002"
ErrDestinationsNotFound = "N0003"
ErrDestinationsFailedCreate = "N0004"
ErrTxNotFound = "N0005"
)
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ require (
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.11.0 // indirect
github.com/prometheus/client_golang v1.12.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand Down
Loading

0 comments on commit e7fce3d

Please sign in to comment.