Skip to content

Commit

Permalink
Remove errors package (#692)
Browse files Browse the repository at this point in the history
<!--
Thanks for taking precious time for making a PR.

Before creating a pull request, please make sure:
- Your PR solves one problem for which an issue exist and a solution has
been discussed
- You have read the guide for contributing
- See
https://github.com/beatlabs/patron/blob/master/README.md#how-to-contribute
- You signed all your commits (otherwise we won't be able to merge the
PR)
  - See https://github.com/beatlabs/patron/blob/master/SIGNYOURWORK.md
- You added unit tests for the new functionality
- You mention in the PR description which issue it is addressing, e.g.
"Resolves #123"
-->

## Which problem is this PR solving?

Remove errors package.

## Short description of the changes

Remove errors package and use errors.Join from std.
  • Loading branch information
mantzas committed May 4, 2024
1 parent 51835d0 commit e3d70f5
Show file tree
Hide file tree
Showing 17 changed files with 33 additions and 123 deletions.
5 changes: 2 additions & 3 deletions client/amqp/amqp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"github.com/beatlabs/patron/correlation"
patronerrors "github.com/beatlabs/patron/errors"
"github.com/beatlabs/patron/log"
"github.com/beatlabs/patron/trace"
"github.com/opentracing/opentracing-go"
Expand Down Expand Up @@ -74,7 +73,7 @@ func New(url string, oo ...OptionFunc) (*Publisher, error) {

ch, err := conn.Channel()
if err != nil {
return nil, patronerrors.Aggregate(fmt.Errorf("failed to open channel: %w", err), conn.Close())
return nil, errors.Join(fmt.Errorf("failed to open channel: %w", err), conn.Close())
}

pub.connection = conn
Expand Down Expand Up @@ -116,7 +115,7 @@ func injectTraceHeaders(ctx context.Context, exchange string, msg *amqp.Publishi

// Close the channel and connection.
func (tc *Publisher) Close() error {
return patronerrors.Aggregate(tc.channel.Close(), tc.connection.Close())
return errors.Join(tc.channel.Close(), tc.connection.Close())
}

type amqpHeadersCarrier map[string]interface{}
Expand Down
4 changes: 2 additions & 2 deletions client/kafka/async_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package kafka

import (
"context"
"errors"
"fmt"

"github.com/IBM/sarama"
patronerrors "github.com/beatlabs/patron/errors"
"github.com/beatlabs/patron/trace"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
Expand Down Expand Up @@ -51,7 +51,7 @@ func (ap *AsyncProducer) propagateError(chErr chan<- error) {
// scope, as it may otherwise leak memory.
func (ap *AsyncProducer) Close() error {
if err := ap.asyncProd.Close(); err != nil {
return patronerrors.Aggregate(fmt.Errorf("failed to close async producer client: %w", err), ap.prodClient.Close())
return errors.Join(fmt.Errorf("failed to close async producer client: %w", err), ap.prodClient.Close())
}
if err := ap.prodClient.Close(); err != nil {
return fmt.Errorf("failed to close async producer: %w", err)
Expand Down
5 changes: 2 additions & 3 deletions client/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/IBM/sarama"
"github.com/beatlabs/patron/correlation"
patronerrors "github.com/beatlabs/patron/errors"
"github.com/beatlabs/patron/internal/validation"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -111,7 +110,7 @@ func DefaultProducerSaramaConfig(name string, idempotent bool) (*sarama.Config,
// Create a new synchronous producer.
func (b *Builder) Create() (*SyncProducer, error) {
if len(b.errs) > 0 {
return nil, patronerrors.Aggregate(b.errs...)
return nil, errors.Join(b.errs...)
}

// required for any SyncProducer; 'Errors' is already true by default for both async/sync producers
Expand All @@ -136,7 +135,7 @@ func (b *Builder) Create() (*SyncProducer, error) {
// CreateAsync a new asynchronous producer.
func (b Builder) CreateAsync() (*AsyncProducer, <-chan error, error) {
if len(b.errs) > 0 {
return nil, nil, patronerrors.Aggregate(b.errs...)
return nil, nil, errors.Join(b.errs...)
}

ap := &AsyncProducer{
Expand Down
8 changes: 4 additions & 4 deletions client/kafka/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func TestBuilder_Create(t *testing.T) {
args args
expectedErr string
}{
"missing brokers": {args: args{brokers: nil, cfg: sarama.NewConfig()}, expectedErr: "brokers are empty or have an empty value\n"},
"missing config": {args: args{brokers: []string{"123"}, cfg: nil}, expectedErr: "no Sarama configuration specified\n"},
"missing brokers": {args: args{brokers: nil, cfg: sarama.NewConfig()}, expectedErr: "brokers are empty or have an empty value"},
"missing config": {args: args{brokers: []string{"123"}, cfg: nil}, expectedErr: "no Sarama configuration specified"},
}
for name, tt := range tests {
tt := tt
Expand All @@ -51,8 +51,8 @@ func TestBuilder_CreateAsync(t *testing.T) {
args args
expectedErr string
}{
"missing brokers": {args: args{brokers: nil, cfg: sarama.NewConfig()}, expectedErr: "brokers are empty or have an empty value\n"},
"missing config": {args: args{brokers: []string{"123"}, cfg: nil}, expectedErr: "no Sarama configuration specified\n"},
"missing brokers": {args: args{brokers: nil, cfg: sarama.NewConfig()}, expectedErr: "brokers are empty or have an empty value"},
"missing config": {args: args{brokers: []string{"123"}, cfg: nil}, expectedErr: "no Sarama configuration specified"},
}
for name, tt := range tests {
tt := tt
Expand Down
3 changes: 1 addition & 2 deletions client/kafka/sync_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"

"github.com/IBM/sarama"
patronerrors "github.com/beatlabs/patron/errors"
"github.com/beatlabs/patron/trace"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
Expand Down Expand Up @@ -87,7 +86,7 @@ func (p *SyncProducer) SendBatch(ctx context.Context, messages []*sarama.Produce
// scope, as it may otherwise leak memory.
func (p *SyncProducer) Close() error {
if err := p.syncProd.Close(); err != nil {
return patronerrors.Aggregate(fmt.Errorf("failed to close sync producer client: %w", err), p.prodClient.Close())
return errors.Join(fmt.Errorf("failed to close sync producer client: %w", err), p.prodClient.Close())
}
if err := p.prodClient.Close(); err != nil {
return fmt.Errorf("failed to close sync producer: %w", err)
Expand Down
7 changes: 3 additions & 4 deletions component/amqp/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"github.com/beatlabs/patron/correlation"
patronerrors "github.com/beatlabs/patron/errors"
"github.com/beatlabs/patron/log"
"github.com/beatlabs/patron/trace"
"github.com/google/uuid"
Expand Down Expand Up @@ -259,7 +258,7 @@ func (s *subscription) close() error {
ee = append(ee, s.conn.Close())
}
s.closed = true
return patronerrors.Aggregate(ee...)
return errors.Join(ee...)
}

func (c *Component) subscribe() (subscription, error) {
Expand All @@ -271,7 +270,7 @@ func (c *Component) subscribe() (subscription, error) {

ch, err := conn.Channel()
if err != nil {
return subscription{}, patronerrors.Aggregate(conn.Close(), fmt.Errorf("failed get channel: %w", err))
return subscription{}, errors.Join(conn.Close(), fmt.Errorf("failed get channel: %w", err))
}
sub.channel = ch

Expand All @@ -280,7 +279,7 @@ func (c *Component) subscribe() (subscription, error) {

deliveries, err := ch.Consume(c.queueCfg.queue, tag, false, false, false, false, nil)
if err != nil {
return subscription{}, patronerrors.Aggregate(ch.Close(), conn.Close(), fmt.Errorf("failed initialize amqp consumer: %w", err))
return subscription{}, errors.Join(ch.Close(), conn.Close(), fmt.Errorf("failed initialize amqp consumer: %w", err))
}
sub.deliveries = deliveries

Expand Down
14 changes: 7 additions & 7 deletions component/amqp/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package amqp

import (
"context"
"errors"

patronerrors "github.com/beatlabs/patron/errors"
"github.com/beatlabs/patron/trace"
"github.com/opentracing/opentracing-go"
"github.com/streadway/amqp"
Expand Down Expand Up @@ -87,33 +87,33 @@ type batch struct {
}

func (b *batch) ACK() ([]Message, error) {
var errors []error
var errs []error
var failed []Message

for _, msg := range b.messages {
err := msg.ACK()
if err != nil {
errors = append(errors, err)
errs = append(errs, err)
failed = append(failed, msg)
}
}

return failed, patronerrors.Aggregate(errors...)
return failed, errors.Join(errs...)
}

func (b *batch) NACK() ([]Message, error) {
var errors []error
var errs []error
var failed []Message

for _, msg := range b.messages {
err := msg.NACK()
if err != nil {
errors = append(errors, err)
errs = append(errs, err)
failed = append(failed, msg)
}
}

return failed, patronerrors.Aggregate(errors...)
return failed, errors.Join(errs...)
}

func (b *batch) Messages() []Message {
Expand Down
4 changes: 2 additions & 2 deletions component/amqp/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func Test_batch_ACK(t *testing.T) {
btc := batch{messages: []Message{msg1, msg2}}

got, err := btc.ACK()
assert.EqualError(t, err, "ERROR\n")
assert.EqualError(t, err, "ERROR")
assert.Len(t, got, 1)
assert.Equal(t, msg2, got[0])
}
Expand All @@ -184,7 +184,7 @@ func Test_batch_NACK(t *testing.T) {
btc := batch{messages: []Message{msg1, msg2}}

got, err := btc.NACK()
assert.EqualError(t, err, "ERROR\n")
assert.EqualError(t, err, "ERROR")
assert.Len(t, got, 1)
assert.Equal(t, msg2, got[0])
}
Expand Down
3 changes: 1 addition & 2 deletions component/http/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"

patronhttp "github.com/beatlabs/patron/component/http/middleware"
patronerrors "github.com/beatlabs/patron/errors"
)

// RouteOptionFunc definition for configuring the route in a functional way.
Expand Down Expand Up @@ -80,5 +79,5 @@ func (r *Routes) Append(route *Route, err error) {

// Result of the route aggregation.
func (r *Routes) Result() ([]*Route, error) {
return r.routes, patronerrors.Aggregate(r.ee...)
return r.routes, errors.Join(r.ee...)
}
5 changes: 2 additions & 3 deletions component/http/route_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/beatlabs/patron/component/http/auth"
httpcache "github.com/beatlabs/patron/component/http/cache"
patronhttp "github.com/beatlabs/patron/component/http/middleware"
errs "github.com/beatlabs/patron/errors"
"golang.org/x/time/rate"
)

Expand Down Expand Up @@ -62,11 +61,11 @@ func WithCache(cache cache.TTLCache, ageBounds httpcache.Age) RouteOptionFunc {
}
rc, ee := httpcache.NewRouteCache(cache, ageBounds)
if len(ee) != 0 {
return errs.Aggregate(ee...)
return errors.Join(ee...)
}
m, err := patronhttp.NewCaching(rc)
if err != nil {
return errs.Aggregate(err)
return errors.Join(err)
}
r.middlewares = append(r.middlewares, m)
return nil
Expand Down
2 changes: 1 addition & 1 deletion component/http/route_option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestCache(t *testing.T) {
"fail with args": {
fields: fields{path: "GET /api"},
args: args{cache: nil, ageBounds: httpcache.Age{}},
expectedErr: "route cache is nil\n",
expectedErr: "route cache is nil",
},
}
for name, tt := range tests {
Expand Down
4 changes: 2 additions & 2 deletions component/http/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ func TestRoutes_Append(t *testing.T) {
expectedErr string
}{
"success": {args: args{route: &Route{}, err: nil}},
"error exist": {args: args{route: &Route{}, err: errors.New("TEST")}, expectedErr: "TEST\n"},
"route is nil": {args: args{route: nil, err: nil}, expectedErr: "route is nil\n"},
"error exist": {args: args{route: &Route{}, err: errors.New("TEST")}, expectedErr: "TEST"},
"route is nil": {args: args{route: nil, err: nil}, expectedErr: "route is nil"},
}
for name, tt := range tests {
tt := tt
Expand Down
3 changes: 1 addition & 2 deletions component/kafka/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/IBM/sarama"
"github.com/beatlabs/patron/correlation"
patronErrors "github.com/beatlabs/patron/errors"
"github.com/beatlabs/patron/internal/validation"
"github.com/beatlabs/patron/log"
"github.com/beatlabs/patron/trace"
Expand Down Expand Up @@ -126,7 +125,7 @@ func New(name, group string, brokers, topics []string, proc BatchProcessorFunc,
}

if len(errs) > 0 {
return nil, patronErrors.Aggregate(errs...)
return nil, errors.Join(errs...)
}

cmp := &Component{
Expand Down
35 changes: 0 additions & 35 deletions errors/aggregate.go

This file was deleted.

47 changes: 0 additions & 47 deletions errors/aggregate_test.go

This file was deleted.

5 changes: 2 additions & 3 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"sync"
"syscall"

patronErrors "github.com/beatlabs/patron/errors"
"github.com/beatlabs/patron/log"
"github.com/beatlabs/patron/trace"
"github.com/uber/jaeger-client-go"
Expand Down Expand Up @@ -75,7 +74,7 @@ func New(name, version string, options ...OptionFunc) (*Service, error) {
}

if len(optionErrors) > 0 {
return nil, patronErrors.Aggregate(optionErrors...)
return nil, errors.Join(optionErrors...)
}

setupLogging(s.logConfig)
Expand Down Expand Up @@ -117,7 +116,7 @@ func (s *Service) Run(ctx context.Context, components ...Component) error {
for err := range chErr {
ee = append(ee, err)
}
return patronErrors.Aggregate(ee...)
return errors.Join(ee...)
}

func (s *Service) setupOSSignal() {
Expand Down

0 comments on commit e3d70f5

Please sign in to comment.