Skip to content

Commit

Permalink
feat(SPV-846): return extended error in every endpoint (#644)
Browse files Browse the repository at this point in the history
* chore(SPV-846): return extended error in every endpoint

* chore(SPV-846): fix linter errors

* trsts(SPV-846): fix tests

* tests(SPV-846): fix xpub test

* chore(SPV-846): fix linter errors
  • Loading branch information
pawellewandowski98 authored Jul 23, 2024
1 parent 184bfa4 commit ce8c73c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 10 deletions.
4 changes: 2 additions & 2 deletions actions/admin/paymail_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (a *Action) paymailGetAddress(c *gin.Context) {
}

if requestBody.Address == "" {
c.JSON(http.StatusBadRequest, "address is required")
spverrors.ErrorResponse(c, spverrors.ErrMissingAddress, a.Services.Logger)
return
}

Expand Down Expand Up @@ -157,7 +157,7 @@ func (a *Action) paymailCreateAddress(c *gin.Context) {
paymailAddress, err := a.Services.SpvWalletEngine.NewPaymailAddress(
c.Request.Context(), requestBody.Key, requestBody.Address, requestBody.PublicName, requestBody.Avatar, opts...)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
spverrors.ErrorResponse(c, err, a.Services.Logger)
return
}

Expand Down
6 changes: 3 additions & 3 deletions actions/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package actions
import (
"net/http"

"github.com/bitcoin-sv/spv-wallet/dictionary"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/gin-gonic/gin"
)

Expand All @@ -14,10 +14,10 @@ func StatusOK(c *gin.Context) {

// NotFound handles all 404 requests
func NotFound(c *gin.Context) {
c.JSON(http.StatusNotFound, dictionary.GetError(dictionary.ErrorRequestNotFound, c.Request.RequestURI))
spverrors.ErrorResponse(c, spverrors.ErrRouteNotFound, nil)
}

// MethodNotAllowed handles all 405 requests
func MethodNotAllowed(c *gin.Context) {
c.JSON(http.StatusMethodNotAllowed, dictionary.GetError(dictionary.ErrorMethodNotAllowed, c.Request.Method, c.Request.RequestURI))
spverrors.ErrorResponse(c, spverrors.ErrRouteMethodNotAllowed, nil)
}
2 changes: 1 addition & 1 deletion actions/utxos/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (a *Action) search(c *gin.Context) {

conditions, err := reqParams.Conditions.ToDbConditions()
if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
spverrors.ErrorResponse(c, spverrors.ErrInvalidConditions, a.Services.Logger)
return
}

Expand Down
6 changes: 4 additions & 2 deletions actions/xpubs/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/bitcoin-sv/spv-wallet/engine"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/bitcoin-sv/spv-wallet/mappings"
"github.com/bitcoin-sv/spv-wallet/server/auth"
"github.com/gin-gonic/gin"
Expand All @@ -28,7 +29,7 @@ func (a *Action) update(c *gin.Context) {

var requestBody engine.Metadata
if err := c.Bind(&requestBody); err != nil {
c.JSON(http.StatusBadRequest, err.Error())
spverrors.ErrorResponse(c, spverrors.ErrCannotBindRequest, a.Services.Logger)
return
}

Expand All @@ -39,7 +40,8 @@ func (a *Action) update(c *gin.Context) {
c.Request.Context(), reqXPubID, requestBody,
)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
spverrors.ErrorResponse(c, err, a.Services.Logger)
return
}

signed := c.GetBool("auth_signed")
Expand Down
12 changes: 11 additions & 1 deletion engine/action_xpub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package engine

import (
"context"
"errors"

"github.com/bitcoin-sv/spv-wallet/engine/datastore"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
)

// NewXpub will parse the xPub and save it into the Datastore
Expand All @@ -14,8 +16,16 @@ func (c *Client) NewXpub(ctx context.Context, xPubKey string, opts ...ModelOps)
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "new_xpub")

// Check if the xpub already exists
xPub, err := getXpubWithCache(ctx, c, xPubKey, "", c.DefaultModelOptions()...)
if err != nil && !errors.Is(err, spverrors.ErrCouldNotFindXpub) {
return nil, err
} else if xPub != nil {
return nil, spverrors.ErrXPubAlreadyExists
}

// Create the model & set the default options (gives options from client->model)
xPub := newXpub(
xPub = newXpub(
xPubKey, c.DefaultModelOptions(append(opts, New())...)...,
)

Expand Down
2 changes: 1 addition & 1 deletion engine/action_xpub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (ts *EmbeddedDBTestSuite) TestClient_NewXpub() {
assert.ErrorIs(t, err, spverrors.ErrXpubInvalidLength)

_, err = tc.client.NewXpub(tc.ctx, "", tc.client.DefaultModelOptions()...)
assert.ErrorIs(t, err, spverrors.ErrXpubInvalidLength)
assert.ErrorIs(t, err, spverrors.ErrMissingFieldXpubID)
})

ts.T().Run(testCase.name+" - duplicate xPub", func(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions engine/spverrors/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ var ErrXpubIDMisMatch = models.SPVError{Message: "xpub_id mismatch", StatusCode:

// ////////////////////////////////// MISSING FIELDS

// ErrXPubAlreadyExists is when xpub already exists
var ErrXPubAlreadyExists = models.SPVError{Message: "xpub already exists", StatusCode: 409, Code: "error-xpub-already-exists"}

// ErrOneOfTheFieldsIsRequired is when all of required fields are missing
var ErrOneOfTheFieldsIsRequired = models.SPVError{Message: "missing all of the fields, one of them is required", StatusCode: 400, Code: "error-missing-field-all-required"}

Expand Down Expand Up @@ -345,3 +348,11 @@ var ErrWebhookSubscriptionNotFound = models.SPVError{Message: "webhook subscript

// ErrNotificationsDisabled happens when the notifications are not enabled in the config
var ErrNotificationsDisabled = models.SPVError{Message: "notifications are disabled", StatusCode: 404, Code: "error-notifications-disabled"}

//////////////////////////////////// ROUTES ERRORS

// ErrRouteNotFound is when route is not found
var ErrRouteNotFound = models.SPVError{Message: "route not found", StatusCode: 404, Code: "error-route-not-found"}

// ErrRouteMethodNotAllowed is when route method is not allowed
var ErrRouteMethodNotAllowed = models.SPVError{Message: "method not allowed", StatusCode: 405, Code: "error-route-method-not-allowed"}

0 comments on commit ce8c73c

Please sign in to comment.