Skip to content

Commit

Permalink
fix(SPV-846): handle exeptions example (#248)
Browse files Browse the repository at this point in the history
Co-authored-by: Paweł Lewandowski <[email protected]>
  • Loading branch information
chris-4chain and pawellewandowski98 authored Jul 4, 2024
1 parent ae50a5c commit b5b1a5f
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 10 deletions.
6 changes: 3 additions & 3 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var ErrMissingXpriv = models.SPVError{Message: "xpriv missing", StatusCode: 401,
var ErrCouldNotFindDraftTransaction = models.SPVError{Message: "could not find draft transaction", StatusCode: 404, Code: "error-draft-transaction-not-found"}

// WrapError wraps an error into SPVError
func WrapError(err error) *models.SPVError {
func WrapError(err error) error {
if err == nil {
return nil
}
Expand All @@ -29,7 +29,7 @@ func WrapError(err error) *models.SPVError {
}

// WrapResponseError wraps a http response into SPVError
func WrapResponseError(res *http.Response) *models.SPVError {
func WrapResponseError(res *http.Response) error {
if res == nil {
return nil
}
Expand All @@ -48,7 +48,7 @@ func WrapResponseError(res *http.Response) *models.SPVError {
}
}

func CreateErrorResponse(code string, message string) *models.SPVError {
func CreateErrorResponse(code string, message string) error {
return &models.SPVError{
StatusCode: http.StatusInternalServerError,
Code: code,
Expand Down
2 changes: 1 addition & 1 deletion examples/go.mod

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

2 changes: 2 additions & 0 deletions examples/go.sum

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

10 changes: 8 additions & 2 deletions examples/handle_exceptions/handle_exceptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ package main

import (
"context"
"errors"
"fmt"
"os"

walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
"github.com/bitcoin-sv/spv-wallet-go-client/examples"
"github.com/bitcoin-sv/spv-wallet/models"
)

func main() {
Expand All @@ -24,8 +26,12 @@ func main() {

status, err := client.AdminGetStatus(ctx)
if err != nil {
fmt.Println("Response status: ", err.GetStatusCode())
fmt.Println("Content: ", err.Error())
var extendedErr models.ExtendedError
if errors.As(err, &extendedErr) {
fmt.Printf("Extended error: [%d] '%s': %s\n", extendedErr.GetStatusCode(), extendedErr.GetCode(), extendedErr.GetMessage())
} else {
fmt.Println("Error: ", err.Error())
}

os.Exit(1)
}
Expand Down
4 changes: 1 addition & 3 deletions go.mod

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

2 changes: 2 additions & 0 deletions go.sum

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

5 changes: 4 additions & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/bitcoinschema/go-bitcoin/v2"
"net/http"
"strconv"

"github.com/bitcoin-sv/spv-wallet-go-client/utils"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoin-sv/spv-wallet/models/filter"
"github.com/bitcoinschema/go-bitcoin/v2"
"github.com/libsv/go-bk/bec"
"github.com/libsv/go-bk/bip32"
)
Expand Down Expand Up @@ -565,6 +565,9 @@ func (wc *WalletClient) authenticateWithXpriv(sign bool, req *http.Request, xPri
}

func (wc *WalletClient) authenticateWithAccessKey(req *http.Request, rawJSON []byte) error {
if wc.accessKey == nil {
return WrapError(errors.New("access key is missing"))
}
return SetSignatureFromAccessKey(&req.Header, hex.EncodeToString(wc.accessKey.Serialise()), string(rawJSON))
}

Expand Down

0 comments on commit b5b1a5f

Please sign in to comment.