Skip to content

Commit

Permalink
feat(SPV-846): refactor numeric error codes into strings (#627)
Browse files Browse the repository at this point in the history
* feat(SPV-846): add spverrors package and use it in spv wallet

* feat(SPV-846): update more errors

* chore(SPV-846): update auth methods

* chore(SPV-846): fix linter error

* chore(SPV-846): update SPVError structure

* chore(SPV-846): refactor ErrorResponse method

* chore(SPV-846): move spverrors to engine

* chore(SPV-846): fix linter errors

* chore(SPV-846): revert config changes

* chore(SPV-846): refactor record transaction errors

* chore(SPV-846): refactor authorization errors

* chore(SPV-846): add more contact errors

* chore(SPV-846): fix linter error

* chore(SPV-846): refactor error codes

* chore(SPV-846): add ExtendedError interface
  • Loading branch information
pawellewandowski98 authored Jul 4, 2024
1 parent dc45064 commit 40c62e9
Show file tree
Hide file tree
Showing 101 changed files with 1,023 additions and 718 deletions.
5 changes: 3 additions & 2 deletions actions/access_keys/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package accesskeys
import (
"net/http"

"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/bitcoin-sv/spv-wallet/mappings"
"github.com/bitcoin-sv/spv-wallet/models/filter"
"github.com/bitcoin-sv/spv-wallet/server/auth"
Expand All @@ -26,7 +27,7 @@ func (a *Action) count(c *gin.Context) {

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

Expand All @@ -37,7 +38,7 @@ func (a *Action) count(c *gin.Context) {
reqParams.Conditions.ToDbConditions(),
)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
spverrors.ErrorResponse(c, err, a.Services.Logger)
return
}

Expand Down
5 changes: 3 additions & 2 deletions actions/access_keys/create.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 @@ -26,7 +27,7 @@ func (a *Action) create(c *gin.Context) {

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

Expand All @@ -37,7 +38,7 @@ func (a *Action) create(c *gin.Context) {
engine.WithMetadatas(requestBody.Metadata),
)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
spverrors.ErrorResponse(c, err, a.Services.Logger)
return
}

Expand Down
8 changes: 4 additions & 4 deletions actions/access_keys/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package accesskeys
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 @@ -27,7 +27,7 @@ func (a *Action) get(c *gin.Context) {

id := c.Query("id")
if id == "" {
c.JSON(http.StatusBadRequest, engine.ErrMissingFieldID)
spverrors.ErrorResponse(c, spverrors.ErrMissingFieldID, a.Services.Logger)
return
}

Expand All @@ -36,12 +36,12 @@ func (a *Action) get(c *gin.Context) {
c.Request.Context(), reqXPubID, id,
)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
spverrors.ErrorResponse(c, err, a.Services.Logger)
return
}

if accessKey.XpubID != reqXPubID {
c.JSON(http.StatusForbidden, "unauthorized")
spverrors.ErrorResponse(c, spverrors.ErrAuthorization, a.Services.Logger)
return
}

Expand Down
6 changes: 3 additions & 3 deletions actions/access_keys/revoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package accesskeys
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 @@ -26,7 +26,7 @@ func (a *Action) revoke(c *gin.Context) {

id := c.Query("id")
if id == "" {
c.JSON(http.StatusBadRequest, engine.ErrMissingFieldID)
spverrors.ErrorResponse(c, spverrors.ErrMissingFieldID, a.Services.Logger)
return
}

Expand All @@ -36,7 +36,7 @@ func (a *Action) revoke(c *gin.Context) {
id,
)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
spverrors.ErrorResponse(c, err, a.Services.Logger)
return
}

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

"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/bitcoin-sv/spv-wallet/mappings"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoin-sv/spv-wallet/models/filter"
Expand All @@ -27,7 +28,7 @@ func (a *Action) search(c *gin.Context) {

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

Expand All @@ -39,7 +40,7 @@ func (a *Action) search(c *gin.Context) {
mappings.MapToQueryParams(reqParams.QueryParams),
)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
spverrors.ErrorResponse(c, err, a.Services.Logger)
return
}

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

"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/bitcoin-sv/spv-wallet/mappings"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoin-sv/spv-wallet/models/filter"
Expand All @@ -24,7 +25,7 @@ import (
func (a *Action) accessKeysSearch(c *gin.Context) {
var reqParams filter.AdminSearchAccessKeys
if err := c.Bind(&reqParams); err != nil {
c.JSON(http.StatusBadRequest, err.Error())
spverrors.ErrorResponse(c, spverrors.ErrCannotBindRequest, a.Services.Logger)
return
}

Expand All @@ -35,7 +36,7 @@ func (a *Action) accessKeysSearch(c *gin.Context) {
mappings.MapToQueryParams(reqParams.QueryParams),
)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
spverrors.ErrorResponse(c, err, a.Services.Logger)
return
}

Expand All @@ -62,7 +63,7 @@ func (a *Action) accessKeysSearch(c *gin.Context) {
func (a *Action) accessKeysCount(c *gin.Context) {
var reqParams filter.AdminCountAccessKeys
if err := c.Bind(&reqParams); err != nil {
c.JSON(http.StatusBadRequest, err.Error())
spverrors.ErrorResponse(c, spverrors.ErrCannotBindRequest, a.Services.Logger)
return
}

Expand All @@ -72,7 +73,7 @@ func (a *Action) accessKeysCount(c *gin.Context) {
reqParams.Conditions.ToDbConditions(),
)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
spverrors.ErrorResponse(c, err, a.Services.Logger)
return
}

Expand Down
31 changes: 10 additions & 21 deletions actions/admin/contact.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package admin

import (
"errors"
"net/http"

"github.com/bitcoin-sv/spv-wallet/actions/common"
"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/models"
"github.com/bitcoin-sv/spv-wallet/models/filter"
Expand All @@ -27,13 +27,13 @@ import (
func (a *Action) contactsSearch(c *gin.Context) {
var reqParams filter.SearchContacts
if err := c.Bind(&reqParams); err != nil {
c.JSON(http.StatusBadRequest, err.Error())
spverrors.ErrorResponse(c, spverrors.ErrCannotBindRequest, a.Services.Logger)
return
}

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

Expand All @@ -46,7 +46,7 @@ func (a *Action) contactsSearch(c *gin.Context) {
mappings.MapToQueryParams(reqParams.QueryParams),
)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
spverrors.ErrorResponse(c, err, a.Services.Logger)
return
}

Expand All @@ -58,7 +58,7 @@ func (a *Action) contactsSearch(c *gin.Context) {
conditions,
)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
spverrors.ErrorResponse(c, err, a.Services.Logger)
return
}

Expand Down Expand Up @@ -88,7 +88,7 @@ func (a *Action) contactsSearch(c *gin.Context) {
func (a *Action) contactsUpdate(c *gin.Context) {
var reqParams UpdateContact
if err := c.Bind(&reqParams); err != nil {
c.JSON(http.StatusBadRequest, err.Error())
spverrors.ErrorResponse(c, spverrors.ErrCannotBindRequest, a.Services.Logger)
return
}

Expand All @@ -101,7 +101,7 @@ func (a *Action) contactsUpdate(c *gin.Context) {
&reqParams.Metadata,
)
if err != nil {
handleErrors(err, c)
spverrors.ErrorResponse(c, err, a.Services.Logger)
return
}

Expand Down Expand Up @@ -133,7 +133,7 @@ func (a *Action) contactsDelete(c *gin.Context) {
id,
)
if err != nil {
handleErrors(err, c)
spverrors.ErrorResponse(c, err, a.Services.Logger)
return
}

Expand Down Expand Up @@ -164,7 +164,7 @@ func (a *Action) contactsReject(c *gin.Context) {
engine.ContactRejected,
)
if err != nil {
handleErrors(err, c)
spverrors.ErrorResponse(c, err, a.Services.Logger)
return
}

Expand Down Expand Up @@ -197,22 +197,11 @@ func (a *Action) contactsAccept(c *gin.Context) {
engine.ContactNotConfirmed,
)
if err != nil {
handleErrors(err, c)
spverrors.ErrorResponse(c, err, a.Services.Logger)
return
}

contract := mappings.MapToContactContract(contact)

c.JSON(http.StatusOK, contract)
}

func handleErrors(err error, c *gin.Context) {
switch {
case errors.Is(err, engine.ErrContactNotFound):
c.JSON(http.StatusNotFound, err.Error())
case errors.Is(err, engine.ErrContactIncorrectStatus):
c.JSON(http.StatusUnprocessableEntity, err.Error())
default:
c.JSON(http.StatusInternalServerError, err.Error())
}
}
9 changes: 5 additions & 4 deletions actions/admin/destinations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package admin
import (
"net/http"

"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/bitcoin-sv/spv-wallet/mappings"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoin-sv/spv-wallet/models/filter"
Expand All @@ -24,7 +25,7 @@ import (
func (a *Action) destinationsSearch(c *gin.Context) {
var reqParams filter.SearchDestinations
if err := c.Bind(&reqParams); err != nil {
c.JSON(http.StatusBadRequest, err.Error())
spverrors.ErrorResponse(c, spverrors.ErrCannotBindRequest, a.Services.Logger)
return
}

Expand All @@ -35,7 +36,7 @@ func (a *Action) destinationsSearch(c *gin.Context) {
mappings.MapToQueryParams(reqParams.QueryParams),
)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
spverrors.ErrorResponse(c, err, a.Services.Logger)
return
}

Expand All @@ -61,7 +62,7 @@ func (a *Action) destinationsSearch(c *gin.Context) {
func (a *Action) destinationsCount(c *gin.Context) {
var reqParams filter.CountDestinations
if err := c.Bind(&reqParams); err != nil {
c.JSON(http.StatusBadRequest, err.Error())
spverrors.ErrorResponse(c, spverrors.ErrCannotBindRequest, a.Services.Logger)
return
}

Expand All @@ -71,7 +72,7 @@ func (a *Action) destinationsCount(c *gin.Context) {
reqParams.Conditions.ToDbConditions(),
)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
spverrors.ErrorResponse(c, err, a.Services.Logger)
return
}

Expand Down
Loading

0 comments on commit 40c62e9

Please sign in to comment.