Skip to content

Commit

Permalink
refactor: Improve error handling in Cannon location and Coordinator c…
Browse files Browse the repository at this point in the history
…lient functions
  • Loading branch information
samcm committed Jul 8, 2024
1 parent ec94902 commit 2bba2db
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 42 deletions.
58 changes: 32 additions & 26 deletions pkg/server/persistence/cannon/location.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cannon

import (
"errors"
"fmt"
"time"

Expand All @@ -23,6 +24,11 @@ type Location struct {
Value string `json:"value" db:"value"`
}

var (
ErrFailedToMarshal = errors.New("failed to marshal location")
ErrFailedToUnmarshal = errors.New("failed to unmarshal location")
)

// MarshalValueFromProto marshals a proto message into the Value field.
func (l *Location) Marshal(msg *xatu.CannonLocation) error {
l.NetworkID = msg.NetworkId
Expand All @@ -35,7 +41,7 @@ func (l *Location) Marshal(msg *xatu.CannonLocation) error {

b, err := protojson.Marshal(data)
if err != nil {
return err
return fmt.Errorf("%w: %s", ErrFailedToMarshal, err)
}

l.Value = string(b)
Expand All @@ -46,7 +52,7 @@ func (l *Location) Marshal(msg *xatu.CannonLocation) error {

b, err := protojson.Marshal(data)
if err != nil {
return err
return fmt.Errorf("%w: %s", ErrFailedToMarshal, err)
}

l.Value = string(b)
Expand All @@ -57,7 +63,7 @@ func (l *Location) Marshal(msg *xatu.CannonLocation) error {

b, err := protojson.Marshal(data)
if err != nil {
return err
return fmt.Errorf("%w: %s", ErrFailedToMarshal, err)
}

l.Value = string(b)
Expand All @@ -68,7 +74,7 @@ func (l *Location) Marshal(msg *xatu.CannonLocation) error {

b, err := protojson.Marshal(data)
if err != nil {
return err
return fmt.Errorf("%w: %s", ErrFailedToMarshal, err)
}

l.Value = string(b)
Expand All @@ -80,7 +86,7 @@ func (l *Location) Marshal(msg *xatu.CannonLocation) error {

b, err := protojson.Marshal(data)
if err != nil {
return err
return fmt.Errorf("%w: %s", ErrFailedToMarshal, err)
}

l.Value = string(b)
Expand All @@ -92,7 +98,7 @@ func (l *Location) Marshal(msg *xatu.CannonLocation) error {

b, err := protojson.Marshal(data)
if err != nil {
return err
return fmt.Errorf("%w: %s", ErrFailedToMarshal, err)
}

l.Value = string(b)
Expand All @@ -103,7 +109,7 @@ func (l *Location) Marshal(msg *xatu.CannonLocation) error {

b, err := protojson.Marshal(data)
if err != nil {
return err
return fmt.Errorf("%w: %s", ErrFailedToMarshal, err)
}

l.Value = string(b)
Expand All @@ -115,7 +121,7 @@ func (l *Location) Marshal(msg *xatu.CannonLocation) error {

b, err := protojson.Marshal(data)
if err != nil {
return err
return fmt.Errorf("%w: %s", ErrFailedToMarshal, err)
}

l.Value = string(b)
Expand All @@ -127,7 +133,7 @@ func (l *Location) Marshal(msg *xatu.CannonLocation) error {

b, err := protojson.Marshal(data)
if err != nil {
return err
return fmt.Errorf("%w: %s", ErrFailedToMarshal, err)
}

l.Value = string(b)
Expand All @@ -139,7 +145,7 @@ func (l *Location) Marshal(msg *xatu.CannonLocation) error {

b, err := protojson.Marshal(data)
if err != nil {
return err
return fmt.Errorf("%w: %s", ErrFailedToMarshal, err)
}

l.Value = string(b)
Expand All @@ -150,7 +156,7 @@ func (l *Location) Marshal(msg *xatu.CannonLocation) error {

b, err := protojson.Marshal(data)
if err != nil {
return err
return fmt.Errorf("%w: %s", ErrFailedToMarshal, err)
}

l.Value = string(b)
Expand All @@ -161,7 +167,7 @@ func (l *Location) Marshal(msg *xatu.CannonLocation) error {

b, err := protojson.Marshal(data)
if err != nil {
return err
return fmt.Errorf("%w: %s", ErrFailedToMarshal, err)
}

l.Value = string(b)
Expand All @@ -172,7 +178,7 @@ func (l *Location) Marshal(msg *xatu.CannonLocation) error {

b, err := protojson.Marshal(data)
if err != nil {
return err
return fmt.Errorf("%w: %s", ErrFailedToMarshal, err)
}

l.Value = string(b)
Expand All @@ -196,7 +202,7 @@ func (l *Location) Unmarshal() (*xatu.CannonLocation, error) {

err := protojson.Unmarshal([]byte(l.Value), data)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w: %s", ErrFailedToUnmarshal, err)
}

msg.Data = &xatu.CannonLocation_EthV2BeaconBlockVoluntaryExit{
Expand All @@ -209,7 +215,7 @@ func (l *Location) Unmarshal() (*xatu.CannonLocation, error) {

err := protojson.Unmarshal([]byte(l.Value), data)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w: %s", ErrFailedToUnmarshal, err)
}

msg.Data = &xatu.CannonLocation_EthV2BeaconBlockProposerSlashing{
Expand All @@ -222,7 +228,7 @@ func (l *Location) Unmarshal() (*xatu.CannonLocation, error) {

err := protojson.Unmarshal([]byte(l.Value), data)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w: %s", ErrFailedToUnmarshal, err)
}

msg.Data = &xatu.CannonLocation_EthV2BeaconBlockDeposit{
Expand All @@ -235,7 +241,7 @@ func (l *Location) Unmarshal() (*xatu.CannonLocation, error) {

err := protojson.Unmarshal([]byte(l.Value), data)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w: %s", ErrFailedToUnmarshal, err)
}

msg.Data = &xatu.CannonLocation_EthV2BeaconBlockAttesterSlashing{
Expand All @@ -249,7 +255,7 @@ func (l *Location) Unmarshal() (*xatu.CannonLocation, error) {

err := protojson.Unmarshal([]byte(l.Value), data)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w: %s", ErrFailedToUnmarshal, err)
}

msg.Data = &xatu.CannonLocation_EthV2BeaconBlockExecutionTransaction{
Expand All @@ -263,7 +269,7 @@ func (l *Location) Unmarshal() (*xatu.CannonLocation, error) {

err := protojson.Unmarshal([]byte(l.Value), data)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w: %s", ErrFailedToUnmarshal, err)
}

msg.Data = &xatu.CannonLocation_EthV2BeaconBlockBlsToExecutionChange{
Expand All @@ -277,7 +283,7 @@ func (l *Location) Unmarshal() (*xatu.CannonLocation, error) {

err := protojson.Unmarshal([]byte(l.Value), data)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w: %s", ErrFailedToUnmarshal, err)
}

msg.Data = &xatu.CannonLocation_EthV2BeaconBlockWithdrawal{
Expand All @@ -290,7 +296,7 @@ func (l *Location) Unmarshal() (*xatu.CannonLocation, error) {

err := protojson.Unmarshal([]byte(l.Value), data)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w: %s", ErrFailedToUnmarshal, err)
}

msg.Data = &xatu.CannonLocation_EthV2BeaconBlock{
Expand All @@ -303,7 +309,7 @@ func (l *Location) Unmarshal() (*xatu.CannonLocation, error) {

err := protojson.Unmarshal([]byte(l.Value), data)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w: %s", ErrFailedToUnmarshal, err)
}

msg.Data = &xatu.CannonLocation_BlockprintBlockClassification{
Expand All @@ -316,7 +322,7 @@ func (l *Location) Unmarshal() (*xatu.CannonLocation, error) {

err := protojson.Unmarshal([]byte(l.Value), data)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w: %s", ErrFailedToUnmarshal, err)
}

msg.Data = &xatu.CannonLocation_EthV1BeaconBlobSidecar{
Expand All @@ -329,7 +335,7 @@ func (l *Location) Unmarshal() (*xatu.CannonLocation, error) {

err := protojson.Unmarshal([]byte(l.Value), data)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w: %s", ErrFailedToUnmarshal, err)
}

msg.Data = &xatu.CannonLocation_EthV1BeaconProposerDuty{
Expand All @@ -342,7 +348,7 @@ func (l *Location) Unmarshal() (*xatu.CannonLocation, error) {

err := protojson.Unmarshal([]byte(l.Value), data)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w: %s", ErrFailedToUnmarshal, err)
}

msg.Data = &xatu.CannonLocation_EthV2BeaconBlockElaboratedAttestation{
Expand All @@ -355,7 +361,7 @@ func (l *Location) Unmarshal() (*xatu.CannonLocation, error) {

err := protojson.Unmarshal([]byte(l.Value), data)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w: %s", ErrFailedToUnmarshal, err)
}

msg.Data = &xatu.CannonLocation_EthV1BeaconValidators{
Expand Down
10 changes: 6 additions & 4 deletions pkg/server/persistence/cannon_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"time"

perrors "github.com/pkg/errors"

"github.com/ethpandaops/xatu/pkg/server/persistence/cannon"
"github.com/huandu/go-sqlbuilder"
)
Expand Down Expand Up @@ -41,7 +43,7 @@ func (c *Client) GetCannonLocationByID(ctx context.Context, id int64) (*cannon.L

rows, err := c.db.QueryContext(ctx, sql, args...)
if err != nil {
return nil, err
return nil, perrors.Wrap(err, "db query failed")
}

defer rows.Close()
Expand All @@ -53,7 +55,7 @@ func (c *Client) GetCannonLocationByID(ctx context.Context, id int64) (*cannon.L

err = rows.Scan(cannonLocationStruct.Addr(&location)...)
if err != nil {
return nil, err
return nil, perrors.Wrap(err, "db scan failed")
}

locations = append(locations, &location)
Expand All @@ -76,7 +78,7 @@ func (c *Client) GetCannonLocationByNetworkIDAndType(ctx context.Context, networ

rows, err := c.db.QueryContext(ctx, sql, args...)
if err != nil {
return nil, err
return nil, perrors.Wrap(err, "db query failed")
}

defer rows.Close()
Expand All @@ -88,7 +90,7 @@ func (c *Client) GetCannonLocationByNetworkIDAndType(ctx context.Context, networ

err = rows.Scan(cannonLocationStruct.Addr(&location)...)
if err != nil {
return nil, err
return nil, perrors.Wrap(err, "db scan failed")
}

locations = append(locations, &location)
Expand Down
Loading

0 comments on commit 2bba2db

Please sign in to comment.