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 2 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
6 changes: 2 additions & 4 deletions _examples/basic/2-realtime-feed/consumer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"sync/atomic"
"time"

"github.com/pkg/errors"

"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill-kafka/v2/pkg/kafka"
"github.com/ThreeDotsLabs/watermill/message"
Expand Down Expand Up @@ -150,7 +148,7 @@ func (p PostsCounter) Count(msg *message.Message) ([]*message.Message, error) {

newCount, err := p.countStorage.CountAdd()
if err != nil {
return nil, errors.Wrap(err, "cannot add count")
return nil, fmt.Errorf("cannot add count: %w", err)
}

producedMsg := postsCountUpdated{NewCount: newCount}
Expand Down Expand Up @@ -194,7 +192,7 @@ func (f FeedGenerator) UpdateFeed(message *message.Message) error {

err := f.feedStorage.AddToFeed(event.Title, event.Author, event.OccurredOn)
if err != nil {
return errors.Wrap(err, "cannot update feed")
return fmt.Errorf("cannot update feed: %w", err)
}

return nil
Expand Down
3 changes: 1 addition & 2 deletions _examples/basic/5-cqrs-protobuf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/golang/protobuf/ptypes"
"github.com/pkg/errors"

"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill-amqp/v2/pkg/amqp"
Expand Down Expand Up @@ -110,7 +109,7 @@ func (o OrderBeerHandler) Handle(ctx context.Context, c interface{}) error {

if rand.Int63n(10) == 0 {
// sometimes there is no beer left, command will be retried
return errors.Errorf("no beer left for room %s, please try later", cmd.RoomId)
return fmt.Errorf("no beer left for room %s, please try later", cmd.RoomId)
}

if err := o.eventBus.Publish(ctx, &BeerOrdered{
Expand Down
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
12 changes: 6 additions & 6 deletions _examples/real-world-examples/receiving-webhooks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package main
import (
"context"
"encoding/json"
"errors"
"flag"
"io/ioutil"
"fmt"
"io"
stdHttp "net/http"
_ "net/http/pprof"

"github.com/pkg/errors"

"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill-http/pkg/http"
"github.com/ThreeDotsLabs/watermill-kafka/v2/pkg/kafka"
Expand Down Expand Up @@ -46,9 +46,9 @@ func main() {
*httpAddr,
http.SubscriberConfig{
UnmarshalMessageFunc: func(topic string, request *stdHttp.Request) (*message.Message, error) {
b, err := ioutil.ReadAll(request.Body)
b, err := io.ReadAll(request.Body)
if err != nil {
return nil, errors.Wrap(err, "cannot read body")
return nil, fmt.Errorf("cannot read body: %w", err)
}

return message.NewMessage(watermill.NewUUID(), b), nil
Expand Down Expand Up @@ -84,7 +84,7 @@ func main() {
webhook := Webhook{}

if err := json.Unmarshal(msg.Payload, &webhook); err != nil {
return nil, errors.Wrap(err, "cannot unmarshal message")
return nil, fmt.Errorf("cannot unmarshal message: %w", err)
}

// Add simple validation
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
Loading