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

Use errors and fmt package to join and wrap errors #460

Closed
wants to merge 4 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
stdSQL "database/sql"
"encoding/json"
"errors"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/ThreeDotsLabs/watermill/message"
driver "github.com/go-sql-driver/mysql"
"github.com/pkg/errors"

"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill-sql/v3/pkg/sql"
Expand Down Expand Up @@ -75,7 +76,7 @@ func processMessage(msg *message.Message) error {
payload := messagePayload{}
err := json.Unmarshal(msg.Payload, &payload)
if err != nil {
return errors.Wrap(err, "unable to unmarshal payload")
return fmt.Errorf("unable to unmarshal payload: %w", err)
}

// let's do it more fragile, let's get the value from DB instead of simple increment
Expand All @@ -102,7 +103,7 @@ func updateDbCounter(ctx context.Context, tx *stdSQL.Tx, counterUUD string, coun
counterValue,
)
if err != nil {
return errors.Wrap(err, "can't update counter value")
return fmt.Errorf("can't update counter value: %w", err)
}

return nil
Expand All @@ -117,7 +118,7 @@ func dbCounterValue(ctx context.Context, tx *stdSQL.Tx, counterUUID string) (int
case stdSQL.ErrNoRows:
return 0, nil
default:
return 0, errors.Wrap(err, "can't get counter value")
return 0, fmt.Errorf("can't get counter value: %w", err)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package main

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"time"
)

// handler receives the webhook requests and logs them in stdout.
func handler(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
Expand Down
16 changes: 8 additions & 8 deletions components/cqrs/command_bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package cqrs

import (
"context"
stdErrors "errors"
"errors"
"fmt"

"github.com/ThreeDotsLabs/watermill"
"github.com/pkg/errors"

"github.com/ThreeDotsLabs/watermill/message"
)
Expand Down Expand Up @@ -39,11 +39,11 @@ func (c CommandBusConfig) Validate() error {
var err error

if c.Marshaler == nil {
err = stdErrors.Join(err, errors.New("missing Marshaler"))
err = errors.Join(err, errors.New("missing Marshaler"))
}

if c.GeneratePublishTopic == nil {
err = stdErrors.Join(err, errors.New("missing GeneratePublishTopic"))
err = errors.Join(err, errors.New("missing GeneratePublishTopic"))
}

return err
Expand Down Expand Up @@ -81,7 +81,7 @@ func NewCommandBusWithConfig(publisher message.Publisher, config CommandBusConfi

config.setDefaults()
if err := config.Validate(); err != nil {
return nil, errors.Wrap(err, "invalid config")
return nil, fmt.Errorf("invalid config: %w", err)
}

return &CommandBus{publisher, config}, nil
Expand Down Expand Up @@ -125,7 +125,7 @@ func (c CommandBus) SendWithModifiedMessage(ctx context.Context, cmd any, modify

if modify != nil {
if err := modify(msg); err != nil {
return errors.Wrap(err, "cannot modify message")
return fmt.Errorf("cannot modify message: %w", err)
}
}

Expand All @@ -148,7 +148,7 @@ func (c CommandBus) newMessage(ctx context.Context, command any) (*message.Messa
Command: command,
})
if err != nil {
return nil, "", errors.Wrap(err, "cannot generate topic name")
return nil, "", fmt.Errorf("cannot generate topic name: %w", err)
}

msg.SetContext(ctx)
Expand All @@ -160,7 +160,7 @@ func (c CommandBus) newMessage(ctx context.Context, command any) (*message.Messa
Message: msg,
})
if err != nil {
return nil, "", errors.Wrap(err, "cannot execute OnSend")
return nil, "", fmt.Errorf("cannot execute OnSend: %w", err)
}
}

Expand Down
8 changes: 4 additions & 4 deletions components/cqrs/command_bus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package cqrs_test

import (
"context"
"errors"
"testing"

"github.com/ThreeDotsLabs/watermill/message"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ThreeDotsLabs/watermill/components/cqrs"
"github.com/ThreeDotsLabs/watermill/message"
)

func TestCommandBusConfig_Validate(t *testing.T) {
Expand All @@ -28,14 +28,14 @@ func TestCommandBusConfig_Validate(t *testing.T) {
ModifyValidConfig: func(c *cqrs.CommandBusConfig) {
c.Marshaler = nil
},
ExpectedErr: errors.Errorf("missing Marshaler"),
ExpectedErr: errors.New("missing Marshaler"),
},
{
Name: "missing_GeneratePublishTopic",
ModifyValidConfig: func(c *cqrs.CommandBusConfig) {
c.GeneratePublishTopic = nil
},
ExpectedErr: errors.Errorf("missing GeneratePublishTopic"),
ExpectedErr: errors.New("missing GeneratePublishTopic"),
},
}
for i := range testCases {
Expand Down
16 changes: 7 additions & 9 deletions components/cqrs/command_processor.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package cqrs

import (
stdErrors "errors"
"errors"
"fmt"

"github.com/pkg/errors"

"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message"
)
Expand Down Expand Up @@ -70,14 +68,14 @@ func (c CommandProcessorConfig) Validate() error {
var err error

if c.Marshaler == nil {
err = stdErrors.Join(err, errors.New("missing Marshaler"))
err = errors.Join(err, errors.New("missing Marshaler"))
}

if c.GenerateSubscribeTopic == nil {
err = stdErrors.Join(err, errors.New("missing GenerateSubscribeTopic"))
err = errors.Join(err, errors.New("missing GenerateSubscribeTopic"))
}
if c.SubscriberConstructor == nil {
err = stdErrors.Join(err, errors.New("missing SubscriberConstructor"))
err = errors.Join(err, errors.New("missing SubscriberConstructor"))
}

return err
Expand Down Expand Up @@ -256,7 +254,7 @@ func (p CommandProcessor) addHandlerToRouter(r *message.Router, handler CommandH
CommandHandler: handler,
})
if err != nil {
return errors.Wrapf(err, "cannot generate topic for command handler %s", handlerName)
return fmt.Errorf("cannot generate topic for command handler %s: %w", handlerName, err)
}

logger := p.config.Logger.With(watermill.LogFields{
Expand All @@ -276,7 +274,7 @@ func (p CommandProcessor) addHandlerToRouter(r *message.Router, handler CommandH
Handler: handler,
})
if err != nil {
return errors.Wrap(err, "cannot create subscriber for command processor")
return fmt.Errorf("cannot create subscriber for command processor: %w", err)
}

r.AddNoPublisherHandler(
Expand Down Expand Up @@ -357,7 +355,7 @@ func (p CommandProcessor) routerHandlerFunc(handler CommandHandler, logger water
func (p CommandProcessor) validateCommand(cmd interface{}) error {
// CommandHandler's NewCommand must return a pointer, because it is used to unmarshal
if err := isPointer(cmd); err != nil {
return errors.Wrap(err, "command must be a non-nil pointer")
return fmt.Errorf("command must be a non-nil pointer: %w", err)
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions components/cqrs/command_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package cqrs_test

import (
"context"
"errors"
"sync/atomic"
"testing"
"time"

"github.com/ThreeDotsLabs/watermill"
"github.com/pkg/errors"

"github.com/ThreeDotsLabs/watermill/components/cqrs"
"github.com/ThreeDotsLabs/watermill/message"
Expand All @@ -32,21 +32,21 @@ func TestCommandProcessorConfig_Validate(t *testing.T) {
ModifyValidConfig: func(c *cqrs.CommandProcessorConfig) {
c.Marshaler = nil
},
ExpectedErr: errors.Errorf("missing Marshaler"),
ExpectedErr: errors.New("missing Marshaler"),
},
{
Name: "missing_SubscriberConstructor",
ModifyValidConfig: func(c *cqrs.CommandProcessorConfig) {
c.SubscriberConstructor = nil
},
ExpectedErr: errors.Errorf("missing SubscriberConstructor"),
ExpectedErr: errors.New("missing SubscriberConstructor"),
},
{
Name: "missing_GenerateHandlerSubscribeTopic",
ModifyValidConfig: func(c *cqrs.CommandProcessorConfig) {
c.GenerateSubscribeTopic = nil
},
ExpectedErr: errors.Errorf("missing GenerateSubscribeTopic"),
ExpectedErr: errors.New("missing GenerateSubscribeTopic"),
},
}
for i := range testCases {
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestCommandProcessor_non_pointer_command(t *testing.T) {
require.NoError(t, err)

err = commandProcessor.AddHandlers(handler)
assert.IsType(t, cqrs.NonPointerError{}, errors.Cause(err))
assert.ErrorAs(t, err, &cqrs.NonPointerError{})
}

// TestCommandProcessor_multiple_same_command_handlers checks, that we don't register multiple handlers for the same command.
Expand Down
33 changes: 17 additions & 16 deletions components/cqrs/cqrs.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cqrs

import (
"errors"
"fmt"

"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
)

// Deprecated: use CommandProcessor and EventProcessor instead.
Expand Down Expand Up @@ -57,35 +58,35 @@ func (c FacadeConfig) Validate() error {

if c.CommandsEnabled() {
if c.GenerateCommandsTopic == nil {
err = multierror.Append(err, errors.New("GenerateCommandsTopic is nil"))
err = errors.Join(err, errors.New("GenerateCommandsTopic is nil"))
}
if c.CommandsSubscriberConstructor == nil {
err = multierror.Append(err, errors.New("CommandsSubscriberConstructor is nil"))
err = errors.Join(err, errors.New("CommandsSubscriberConstructor is nil"))
}
if c.CommandsPublisher == nil {
err = multierror.Append(err, errors.New("CommandsPublisher is nil"))
err = errors.Join(err, errors.New("CommandsPublisher is nil"))
}
}
if c.EventsEnabled() {
if c.GenerateEventsTopic == nil {
err = multierror.Append(err, errors.New("GenerateEventsTopic is nil"))
err = errors.Join(err, errors.New("GenerateEventsTopic is nil"))
}
if c.EventsSubscriberConstructor == nil {
err = multierror.Append(err, errors.New("EventsSubscriberConstructor is nil"))
err = errors.Join(err, errors.New("EventsSubscriberConstructor is nil"))
}
if c.EventsPublisher == nil {
err = multierror.Append(err, errors.New("EventsPublisher is nil"))
err = errors.Join(err, errors.New("EventsPublisher is nil"))
}
}

if c.Router == nil {
err = multierror.Append(err, errors.New("Router is nil"))
err = errors.Join(err, errors.New("Router is nil"))
}
if c.Logger == nil {
err = multierror.Append(err, errors.New("Logger is nil"))
err = errors.Join(err, errors.New("Logger is nil"))
}
if c.CommandEventMarshaler == nil {
err = multierror.Append(err, errors.New("CommandEventMarshaler is nil"))
err = errors.Join(err, errors.New("CommandEventMarshaler is nil"))
}

return err
Expand Down Expand Up @@ -129,7 +130,7 @@ func (f Facade) CommandEventMarshaler() CommandEventMarshaler {
// Deprecated: use CommandHandler and EventHandler instead.
func NewFacade(config FacadeConfig) (*Facade, error) {
if err := config.Validate(); err != nil {
return nil, errors.Wrap(err, "invalid config")
return nil, fmt.Errorf("invalid config: %w", err)
}

c := &Facade{
Expand All @@ -146,7 +147,7 @@ func NewFacade(config FacadeConfig) (*Facade, error) {
config.CommandEventMarshaler,
)
if err != nil {
return nil, errors.Wrap(err, "cannot create command bus")
return nil, fmt.Errorf("cannot create command bus: %w", err)
}
} else {
config.Logger.Info("Empty GenerateCommandsTopic, command bus will be not created", nil)
Expand All @@ -155,7 +156,7 @@ func NewFacade(config FacadeConfig) (*Facade, error) {
var err error
c.eventBus, err = NewEventBus(config.EventsPublisher, config.GenerateEventsTopic, config.CommandEventMarshaler)
if err != nil {
return nil, errors.Wrap(err, "cannot create event bus")
return nil, fmt.Errorf("cannot create event bus: %w", err)
}
} else {
config.Logger.Info("Empty GenerateEventsTopic, event bus will be not created", nil)
Expand All @@ -170,7 +171,7 @@ func NewFacade(config FacadeConfig) (*Facade, error) {
config.Logger,
)
if err != nil {
return nil, errors.Wrap(err, "cannot create command processor")
return nil, fmt.Errorf("cannot create command processor: %w", err)
}

if err := commandProcessor.AddHandlersToRouter(config.Router); err != nil {
Expand All @@ -186,7 +187,7 @@ func NewFacade(config FacadeConfig) (*Facade, error) {
config.Logger,
)
if err != nil {
return nil, errors.Wrap(err, "cannot create event processor")
return nil, fmt.Errorf("cannot create event processor: %w", err)
}

if err := eventProcessor.AddHandlersToRouter(config.Router); err != nil {
Expand Down
Loading
Loading