Skip to content

Commit

Permalink
feat(SPV-846): adjust error response to new structure
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 committed Jun 21, 2024
1 parent f788b2b commit 608726b
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 113 deletions.
5 changes: 3 additions & 2 deletions authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package walletclient
import (
"encoding/hex"
"fmt"
"github.com/bitcoin-sv/spv-wallet/spverrors"
"net/http"
"time"

Expand All @@ -18,7 +19,7 @@ import (
)

// SetSignature will set the signature on the header for the request
func setSignature(header *http.Header, xPriv *bip32.ExtendedKey, bodyString string) ResponseError {
func setSignature(header *http.Header, xPriv *bip32.ExtendedKey, bodyString string) *spverrors.SPVError {
// Create the signature
authData, err := createSignature(xPriv, bodyString)
if err != nil {
Expand Down Expand Up @@ -199,7 +200,7 @@ func getSigningMessage(xPub string, auth *models.AuthPayload) string {
return fmt.Sprintf("%s%s%s%d", xPub, auth.AuthHash, auth.AuthNonce, auth.AuthTime)
}

func setSignatureHeaders(header *http.Header, authData *models.AuthPayload) ResponseError {
func setSignatureHeaders(header *http.Header, authData *models.AuthPayload) *spverrors.SPVError {
// Create the auth header hash
header.Set(models.AuthHeaderHash, authData.AuthHash)

Expand Down
51 changes: 13 additions & 38 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package walletclient
import (
"encoding/json"
"errors"
"fmt"
"io"
"github.com/bitcoin-sv/spv-wallet/spverrors"
"net/http"
)

Expand All @@ -14,59 +13,35 @@ var ErrAdminKey = errors.New("an admin key must be set to be able to create an x
// ErrNoClientSet is when no client is set
var ErrNoClientSet = errors.New("no transport client set")

// ResError is a struct which contain information about error
type ResError struct {
StatusCode int
Message string
}

// ResponseError is an interface for error
type ResponseError interface {
Error() string
GetStatusCode() int
}

// WrapError wraps an error into ResponseError
func WrapError(err error) ResponseError {
// WrapError wraps an error into SPVError
func WrapError(err error) *spverrors.SPVError {
if err == nil {
return nil
}

return &ResError{
return &spverrors.SPVError{
StatusCode: http.StatusInternalServerError,
Message: err.Error(),
Code: spverrors.UnknownErrorCode,
}
}

// WrapResponseError wraps a http response into ResponseError
func WrapResponseError(res *http.Response) ResponseError {
// WrapResponseError wraps a http response into SPVError
func WrapResponseError(res *http.Response) *spverrors.SPVError {
if res == nil {
return nil
}

var errorMsg string
var resError *spverrors.ResponseError

err := json.NewDecoder(res.Body).Decode(&errorMsg)
err := json.NewDecoder(res.Body).Decode(&resError)
if err != nil {
// if EOF, then body is empty and we return response status as error message
if !errors.Is(err, io.EOF) {
errorMsg = fmt.Sprintf("spv-wallet error message can't be decoded. Reason: %s", err.Error())
}
errorMsg = res.Status
return WrapError(err)
}

return &ResError{
return &spverrors.SPVError{
StatusCode: res.StatusCode,
Message: errorMsg,
Code: resError.Code,
Message: resError.Message,
}
}

// Error returns the error message
func (e *ResError) Error() string {
return e.Message
}

// GetStatusCode returns the status code of error
func (e *ResError) GetStatusCode() int {
return e.StatusCode
}
34 changes: 33 additions & 1 deletion go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 608726b

Please sign in to comment.