Skip to content

Commit

Permalink
main/main.go: fix a TODO related to better error handling (#86)
Browse files Browse the repository at this point in the history
* main/main.go: fix a TODO related to better error handling

Now that Go supports wrapping error natively, let's use it

* replication/client/conn/conn.go: Fix code comment on public methods
  • Loading branch information
JordanP authored Jun 8, 2023
1 parent 138953a commit a14211d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
4 changes: 2 additions & 2 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ func sourceConfig(c *cli.Context) *pgconn.Config {
func runCreate(sourceConfig *pgconn.Config, replicationSlot string) error {
err := utils.PgCreateReplicationSlot(context.Background(), sourceConfig, replicationSlot)

// TODO proper error handling
if err != nil {
if strings.HasSuffix(err.Error(), "(SQLSTATE 42710)") {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) && pgErr.Code == "42710" {
fmt.Printf("Replication slot '%s' already exists.\n", replicationSlot)
return nil
} else {
Expand Down
4 changes: 2 additions & 2 deletions replication/client/conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func New(ctx context.Context, conf *pgconn.Config) (Conn, error) {
return conn, err
}

// getConnWithRetry wraps New with a retry loop. It returns a
// NewConnWithRetry wraps New with a retry loop. It returns a
// new replication connection without starting replication.
func NewConnWithRetry(ctx context.Context, sourceConfig *pgconn.Config) (Conn, error) {
var conn Conn
Expand Down Expand Up @@ -84,7 +84,7 @@ func (c PgReplConnWrapper) SendStandbyStatus(ctx context.Context, status pglogre
return pglogrepl.SendStandbyStatusUpdate(ctx, c.conn, status)
}

// WaitForReplicationMessage wraps pgconn.PgConn.ReceiveMessage
// ReceiveMessage wraps pgconn.PgConn.ReceiveMessage
func (c PgReplConnWrapper) ReceiveMessage(ctx context.Context) (pgproto3.BackendMessage, error) {
return c.conn.ReceiveMessage(ctx)
}
Expand Down
5 changes: 2 additions & 3 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package utils

import (
"context"
"fmt"
"hash/crc32"

Expand All @@ -25,8 +26,6 @@ import (
"github.com/jackc/pglogrepl"
"github.com/jackc/pgx/v5/pgconn"
"github.com/pkg/errors"

"context"
)

// QuickHash buckets the string into i buckets based on crc32 hashing
Expand All @@ -50,7 +49,7 @@ func PgCreateReplicationSlot(ctx context.Context, sourceConfig *pgconn.Config, s

_, err = rplConn.CreateReplicationSlot(ctx, slot, "test_decoding", pglogrepl.CreateReplicationSlotOptions{Temporary: false})
if err != nil {
return errors.Wrapf(err, "unable to create slot %s", slot)
return fmt.Errorf("unable to create slot %s: %w", slot, err)
}

return err
Expand Down

0 comments on commit a14211d

Please sign in to comment.