Skip to content

Commit

Permalink
feat: correctly implement NIP-47 NOT_FOUND error code
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Jul 9, 2024
1 parent 5ad0e93 commit 1b78f43
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
9 changes: 8 additions & 1 deletion nip47/controllers/lookup_invoice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package controllers

import (
"context"
"errors"
"fmt"
"strings"

"github.com/getAlby/hub/logger"
"github.com/getAlby/hub/nip47/models"
"github.com/getAlby/hub/transactions"
"github.com/nbd-wtf/go-nostr"
decodepay "github.com/nbd-wtf/ln-decodepay"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -72,10 +74,15 @@ func (controller *nip47Controller) HandleLookupInvoiceEvent(ctx context.Context,
"payment_hash": paymentHash,
}).Infof("Failed to lookup invoice: %v", err)

code := models.ERROR_INTERNAL
if errors.Is(err, transactions.NewNotFoundError()) {
code = models.ERROR_NOT_FOUND
}

publishResponse(&models.Response{
ResultType: nip47Request.Method,
Error: &models.Error{
Code: models.ERROR_INTERNAL,
Code: code,
Message: err.Error(),
},
}, nostr.Tags{})
Expand Down
1 change: 1 addition & 0 deletions nip47/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
ERROR_EXPIRED = "EXPIRED"
ERROR_RESTRICTED = "RESTRICTED"
ERROR_BAD_REQUEST = "BAD_REQUEST"
ERROR_NOT_FOUND = "NOT_FOUND"
OTHER = "OTHER"
)

Expand Down
13 changes: 12 additions & 1 deletion transactions/transactions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ type TransactionsService interface {

type Transaction = db.Transaction

type notFoundError struct {
}

func NewNotFoundError() error {
return &notFoundError{}
}

func (err *notFoundError) Error() string {
return "Not Found"
}

const (
TRANSACTION_TYPE_INCOMING = "incoming"
TRANSACTION_TYPE_OUTGOING = "outgoing"
Expand Down Expand Up @@ -290,7 +301,7 @@ func (svc *transactionsService) LookupTransaction(ctx context.Context, paymentHa
}

if result.RowsAffected == 0 {
return nil, errors.New("transaction not found")
return nil, NewNotFoundError()
}

if transaction.State == TRANSACTION_STATE_PENDING {
Expand Down

0 comments on commit 1b78f43

Please sign in to comment.