Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapping errors while returning #391

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aws/kms/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package kms

import (
"context"
"fmt"

"github.com/Layr-Labs/eigensdk-go/utils"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/kms"
)

func NewKMSClient(ctx context.Context, region string) (*kms.Client, error) {
config, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
if err != nil {
return nil, fmt.Errorf("failed to load AWS config: %w", err)
return nil, utils.WrapError("failed to load AWS config", err)
}

c := kms.NewFromConfig(config)
Expand Down
10 changes: 7 additions & 3 deletions aws/kms/get_public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/asn1"
"fmt"

"github.com/Layr-Labs/eigensdk-go/utils"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/kms"
"github.com/ethereum/go-ethereum/crypto"
Expand All @@ -28,18 +29,21 @@ func GetECDSAPublicKey(ctx context.Context, svc *kms.Client, keyId string) (*ecd
KeyId: aws.String(keyId),
})
if err != nil {
return nil, fmt.Errorf("failed to get public key for KeyId=%s: %w", keyId, err)
text := fmt.Sprintf("failed to get public key for KeyId=%s", keyId)
return nil, utils.WrapError(text, err)
}

var asn1pubk asn1EcPublicKey
_, err = asn1.Unmarshal(getPubKeyOutput.PublicKey, &asn1pubk)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal public key for KeyId=%s: %w", keyId, err)
text := fmt.Sprintf("failed to unmarshal public key for KeyId=%s", keyId)
return nil, utils.WrapError(text, err)
}

pubkey, err := crypto.UnmarshalPubkey(asn1pubk.PublicKey.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal public key for KeyId=%s: %w", keyId, err)
text := fmt.Sprintf("failed to unmarshal public key for KeyId=%s", keyId)
return nil, utils.WrapError(text, err)
}

return pubkey, nil
Expand Down
6 changes: 4 additions & 2 deletions chainio/clients/fireblocks/cancel_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"fmt"
"strings"

"github.com/Layr-Labs/eigensdk-go/utils"
)

type CancelTransactionResponse struct {
Expand All @@ -16,12 +18,12 @@ func (f *client) CancelTransaction(ctx context.Context, txID string) (bool, erro
path := fmt.Sprintf("/v1/transactions/%s/cancel", txID)
res, err := f.makeRequest(ctx, "POST", path, nil)
if err != nil {
return false, fmt.Errorf("error making request: %w", err)
return false, utils.WrapError("error making request", err)
}
var response CancelTransactionResponse
err = json.NewDecoder(strings.NewReader(string(res))).Decode(&response)
if err != nil {
return false, fmt.Errorf("error parsing response body: %w", err)
return false, utils.WrapError("error parsing response body", err)
}

return response.Success, nil
Expand Down
34 changes: 18 additions & 16 deletions chainio/clients/fireblocks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/Layr-Labs/eigensdk-go/utils"
"github.com/golang-jwt/jwt"
"github.com/google/uuid"
)
Expand Down Expand Up @@ -93,7 +94,7 @@ func NewClient(
c := http.Client{Timeout: timeout}
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(secretKey)
if err != nil {
return nil, fmt.Errorf("error parsing RSA private key: %w", err)
return nil, utils.WrapError("error parsing RSA private key", err)
}

return &client{
Expand All @@ -116,7 +117,7 @@ func (f *client) signJwt(path string, bodyJson interface{}, durationSeconds int6

bodyBytes, err := json.Marshal(bodyJson)
if err != nil {
return "", fmt.Errorf("error marshaling JSON: %w", err)
return "", utils.WrapError("error marshaling JSON", err)
}

h := sha256.New()
Expand All @@ -135,7 +136,7 @@ func (f *client) signJwt(path string, bodyJson interface{}, durationSeconds int6
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
tokenString, err := token.SignedString(f.privateKey)
if err != nil {
return "", fmt.Errorf("error signing token: %w", err)
return "", utils.WrapError("error signing token", err)
}

return tokenString, nil
Expand All @@ -148,17 +149,18 @@ func (f *client) makeRequest(ctx context.Context, method, path string, body inte
// remove query parameters from path and join with baseURL
pathURI, err := url.Parse(path)
if err != nil {
return nil, fmt.Errorf("error parsing URL: %w", err)
return nil, utils.WrapError("error parsing URL", err)
}
query := pathURI.Query()
pathURI.RawQuery = ""
urlStr, err := url.JoinPath(f.baseURL, pathURI.String())
if err != nil {
return nil, fmt.Errorf("error joining URL path with %s and %s: %w", f.baseURL, path, err)
text := fmt.Sprintf("error joining URL path with %s and %s", f.baseURL, path)
return nil, utils.WrapError(text, err)
}
url, err := url.Parse(urlStr)
if err != nil {
return nil, fmt.Errorf("error parsing URL: %w", err)
return nil, utils.WrapError("error parsing URL", err)
}
// add query parameters back to path
url.RawQuery = query.Encode()
Expand All @@ -168,18 +170,18 @@ func (f *client) makeRequest(ctx context.Context, method, path string, body inte
var err error
reqBodyBytes, err = json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("error marshaling request body: %w", err)
return nil, utils.WrapError("error marshaling request body", err)
}
}

token, err := f.signJwt(path, body, int64(f.timeout.Seconds()))
if err != nil {
return nil, fmt.Errorf("error signing JWT: %w", err)
return nil, utils.WrapError("error signing JWT", err)
}

req, err := http.NewRequest(method, url.String(), bytes.NewBuffer(reqBodyBytes))
if err != nil {
return nil, fmt.Errorf("error creating HTTP request: %w", err)
return nil, utils.WrapError("error creating HTTP request", err)
}

if method == "POST" {
Expand All @@ -191,27 +193,27 @@ func (f *client) makeRequest(ctx context.Context, method, path string, body inte

resp, err := f.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending HTTP request: %w", err)
return nil, utils.WrapError("error sending HTTP request", err)
}
defer resp.Body.Close()

respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %w", err)
return nil, utils.WrapError("error reading response body", err)
}

if resp.StatusCode != http.StatusOK {
var errResp ErrorResponse
err = json.Unmarshal(respBody, &errResp)
if err != nil {
return nil, fmt.Errorf("error parsing error response: %w", err)
return nil, utils.WrapError("error parsing error response", err)
}
return nil, fmt.Errorf(
"error response (%d) from Fireblocks with code %d: %s",

text := fmt.Sprintf("error response (%d) from Fireblocks with code %d: %s",
resp.StatusCode,
errResp.Code,
errResp.Message,
)
errResp.Message)
return nil, utils.WrapError(text, err)
}

return respBody, nil
Expand Down
7 changes: 4 additions & 3 deletions chainio/clients/fireblocks/contract_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package fireblocks
import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/Layr-Labs/eigensdk-go/utils"
)

func NewContractCallRequest(
Expand Down Expand Up @@ -58,12 +59,12 @@ func (f *client) ContractCall(ctx context.Context, req *TransactionRequest) (*Tr
f.logger.Debug("Fireblocks call contract", "req", req)
res, err := f.makeRequest(ctx, "POST", "/v1/transactions", req)
if err != nil {
return nil, fmt.Errorf("error making request: %w", err)
return nil, utils.WrapError("error making request", err)
}
var response TransactionResponse
err = json.NewDecoder(strings.NewReader(string(res))).Decode(&response)
if err != nil {
return nil, fmt.Errorf("error parsing response body: %w", err)
return nil, utils.WrapError("error parsing response body", err)
}

return &TransactionResponse{
Expand Down
9 changes: 6 additions & 3 deletions chainio/clients/fireblocks/get_asset_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"net/url"
"strings"

"github.com/Layr-Labs/eigensdk-go/utils"
)

type AssetAddress struct {
Expand Down Expand Up @@ -40,7 +42,7 @@ func (f *client) GetAssetAddresses(ctx context.Context, vaultID string, assetID
path := fmt.Sprintf("/v1/vault/accounts/%s/%s/addresses_paginated", vaultID, assetID)
u, err := url.Parse(path)
if err != nil {
return addresses, fmt.Errorf("error parsing URL: %w", err)
return nil, utils.WrapError("error parsing URL", err)
}
q := u.Query()
q.Set("before", p.Before)
Expand All @@ -49,12 +51,13 @@ func (f *client) GetAssetAddresses(ctx context.Context, vaultID string, assetID

res, err := f.makeRequest(ctx, "GET", u.String(), nil)
if err != nil {
return nil, fmt.Errorf("error making request: %w", err)
return nil, utils.WrapError("error making request", err)
}
body := string(res)
err = json.NewDecoder(strings.NewReader(body)).Decode(&response)
if err != nil {
return addresses, fmt.Errorf("error parsing response body: %s: %w", body, err)
text := fmt.Sprintf("error parsing response body: %s", body)
return nil, utils.WrapError(text, err)
}

addresses = append(addresses, response.Addresses...)
Expand Down
6 changes: 4 additions & 2 deletions chainio/clients/fireblocks/get_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"fmt"
"strings"

"github.com/Layr-Labs/eigensdk-go/utils"
)

// Transaction is a type for the transaction response from Fireblocks
Expand Down Expand Up @@ -52,12 +54,12 @@ func (f *client) GetTransaction(ctx context.Context, txID string) (*Transaction,
path := fmt.Sprintf("/v1/transactions/%s", txID)
res, err := f.makeRequest(ctx, "GET", path, nil)
if err != nil {
return nil, fmt.Errorf("error making request: %w", err)
return nil, utils.WrapError("error making request", err)
}
var tx Transaction
err = json.NewDecoder(strings.NewReader(string(res))).Decode(&tx)
if err != nil {
return nil, fmt.Errorf("error parsing response body: %w", err)
return nil, utils.WrapError("error parsing response body", err)
}

return &tx, nil
Expand Down
6 changes: 4 additions & 2 deletions chainio/clients/fireblocks/list_contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"strings"

"github.com/Layr-Labs/eigensdk-go/utils"
"github.com/ethereum/go-ethereum/common"
)

Expand All @@ -24,12 +25,13 @@ func (f *client) ListContracts(ctx context.Context) ([]WhitelistedContract, erro
var contracts []WhitelistedContract
res, err := f.makeRequest(ctx, "GET", "/v1/contracts", nil)
if err != nil {
return contracts, fmt.Errorf("error making request: %w", err)
return contracts, utils.WrapError("error making request", err)
}
body := string(res)
err = json.NewDecoder(strings.NewReader(body)).Decode(&contracts)
if err != nil {
return contracts, fmt.Errorf("error parsing response body: %s: %w", body, err)
text := fmt.Sprintf("error parsing response body: %s", body)
return contracts, utils.WrapError(text, err)
}

return contracts, nil
Expand Down
6 changes: 4 additions & 2 deletions chainio/clients/fireblocks/list_external_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"strings"

"github.com/Layr-Labs/eigensdk-go/utils"
"github.com/ethereum/go-ethereum/common"
)

Expand All @@ -26,12 +27,13 @@ func (f *client) ListExternalWallets(ctx context.Context) ([]WhitelistedAccount,
var accounts []WhitelistedAccount
res, err := f.makeRequest(ctx, "GET", "/v1/external_wallets", nil)
if err != nil {
return accounts, fmt.Errorf("error making request: %w", err)
return accounts, utils.WrapError("error making request", err)
}
body := string(res)
err = json.NewDecoder(strings.NewReader(body)).Decode(&accounts)
if err != nil {
return accounts, fmt.Errorf("error parsing response body: %s: %w", body, err)
text := fmt.Sprintf("error parsing response body: %s", body)
return accounts, utils.WrapError(text, err)
}

return accounts, nil
Expand Down
9 changes: 6 additions & 3 deletions chainio/clients/fireblocks/list_vault_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"net/url"
"strings"

"github.com/Layr-Labs/eigensdk-go/utils"
)

type Asset struct {
Expand Down Expand Up @@ -36,20 +38,21 @@ func (f *client) ListVaultAccounts(ctx context.Context) ([]VaultAccount, error)
for next {
u, err := url.Parse("/v1/vault/accounts_paged")
if err != nil {
return accounts, fmt.Errorf("error parsing URL: %w", err)
return accounts, utils.WrapError("error parsing URL", err)
}
q := u.Query()
q.Set("before", p.Before)
q.Set("after", p.After)
u.RawQuery = q.Encode()
res, err := f.makeRequest(ctx, "GET", u.String(), nil)
if err != nil {
return accounts, fmt.Errorf("error making request: %w", err)
return accounts, utils.WrapError("error making request", err)
}
body := string(res)
err = json.NewDecoder(strings.NewReader(body)).Decode(&response)
if err != nil {
return accounts, fmt.Errorf("error parsing response body: %s: %w", body, err)
text := fmt.Sprintf("error parsing response body: %s", body)
return accounts, utils.WrapError(text, err)
}

accounts = append(accounts, response.Accounts...)
Expand Down
7 changes: 4 additions & 3 deletions chainio/clients/fireblocks/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package fireblocks
import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/Layr-Labs/eigensdk-go/utils"
)

func NewTransferRequest(
Expand Down Expand Up @@ -54,12 +55,12 @@ func (f *client) Transfer(ctx context.Context, req *TransactionRequest) (*Transa
f.logger.Debug("Fireblocks transfer", "req", req)
res, err := f.makeRequest(ctx, "POST", "/v1/transactions", req)
if err != nil {
return nil, fmt.Errorf("error making request: %w", err)
return nil, utils.WrapError("error making request", err)
}
var response TransactionResponse
err = json.NewDecoder(strings.NewReader(string(res))).Decode(&response)
if err != nil {
return nil, fmt.Errorf("error parsing response body: %w", err)
return nil, utils.WrapError("error parsing response body", err)
}

return &TransactionResponse{
Expand Down
Loading
Loading