From 27055f2ace9b9240470e5c98a9ccf29aa997e917 Mon Sep 17 00:00:00 2001 From: Frank Meyer Date: Sat, 6 Feb 2021 01:10:34 +0800 Subject: [PATCH] Wrap underlying errors via `%w` verb (#130) As introduced in Go 1.13. This enables user of this library to check for an underlying wrapped error type via errors.Is and errors.As functions. --- consumer.go | 10 +++++----- kinesis.go | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/consumer.go b/consumer.go index 9662a18..cd7e448 100644 --- a/consumer.go +++ b/consumer.go @@ -29,7 +29,7 @@ type Record struct { // any of the optional attributes. func New(streamName string, opts ...Option) (*Consumer, error) { if streamName == "" { - return nil, fmt.Errorf("must provide stream name") + return nil, errors.New("must provide stream name") } // new consumer with noop storage, counter, and logger @@ -120,7 +120,7 @@ func (c *Consumer) Scan(ctx context.Context, fn ScanFunc) error { defer wg.Done() if err := c.ScanShard(ctx, shardID, fn); err != nil { select { - case errc <- fmt.Errorf("shard %s error: %v", shardID, err): + case errc <- fmt.Errorf("shard %s error: %w", shardID, err): // first error to occur cancel() default: @@ -144,13 +144,13 @@ func (c *Consumer) ScanShard(ctx context.Context, shardID string, fn ScanFunc) e // get last seq number from checkpoint lastSeqNum, err := c.group.GetCheckpoint(c.streamName, shardID) if err != nil { - return fmt.Errorf("get checkpoint error: %v", err) + return fmt.Errorf("get checkpoint error: %w", err) } // get shard iterator shardIterator, err := c.getShardIterator(ctx, c.streamName, shardID, lastSeqNum) if err != nil { - return fmt.Errorf("get shard iterator error: %v", err) + return fmt.Errorf("get shard iterator error: %w", err) } c.logger.Log("[CONSUMER] start scan:", shardID, lastSeqNum) @@ -178,7 +178,7 @@ func (c *Consumer) ScanShard(ctx context.Context, shardID string, fn ScanFunc) e shardIterator, err = c.getShardIterator(ctx, c.streamName, shardID, lastSeqNum) if err != nil { - return fmt.Errorf("get shard iterator error: %v", err) + return fmt.Errorf("get shard iterator error: %w", err) } } else { // loop over records, call callback func diff --git a/kinesis.go b/kinesis.go index f63c2a7..af02060 100644 --- a/kinesis.go +++ b/kinesis.go @@ -18,7 +18,7 @@ func listShards(ksis kinesisiface.KinesisAPI, streamName string) ([]*kinesis.Sha for { resp, err := ksis.ListShards(listShardsInput) if err != nil { - return nil, fmt.Errorf("ListShards error: %v", err) + return nil, fmt.Errorf("ListShards error: %w", err) } ss = append(ss, resp.Shards...)