Skip to content

Commit

Permalink
Merge pull request rapidpro#251 from nyaruka/std_errors
Browse files Browse the repository at this point in the history
Use std library errors
  • Loading branch information
rowanseymour authored May 21, 2024
2 parents 28869df + 0a500f4 commit 9ceb0f1
Show file tree
Hide file tree
Showing 139 changed files with 997 additions and 950 deletions.
4 changes: 2 additions & 2 deletions core/goflow/modifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package goflow

import (
"encoding/json"
"fmt"

"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/modifiers"
"github.com/pkg/errors"
)

// MissingAssets is the type for defining missing assets behavior
Expand All @@ -29,7 +29,7 @@ func ReadModifiers(sa flows.SessionAssets, data []json.RawMessage, missing Missi
continue
}
if err != nil {
return nil, errors.Wrapf(err, "error reading modifier: %s", string(m))
return nil, fmt.Errorf("error reading modifier: %s: %w", string(m), err)
}
mods = append(mods, mod)
}
Expand Down
7 changes: 4 additions & 3 deletions core/handlers/input_labels_added.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package handlers

import (
"context"
"errors"
"fmt"
"log/slog"

"github.com/jmoiron/sqlx"
Expand All @@ -10,7 +12,6 @@ import (
"github.com/nyaruka/mailroom/core/hooks"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/runtime"
"github.com/pkg/errors"
)

func init() {
Expand All @@ -20,7 +21,7 @@ func init() {
// handleInputLabelsAdded is called for each input labels added event in a scene
func handleInputLabelsAdded(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, oa *models.OrgAssets, scene *models.Scene, e flows.Event) error {
if scene.Session() == nil {
return errors.Errorf("cannot add label, not in a session")
return errors.New("cannot add label, not in a session")
}

event := e.(*events.InputLabelsAddedEvent)
Expand All @@ -37,7 +38,7 @@ func handleInputLabelsAdded(ctx context.Context, rt *runtime.Runtime, tx *sqlx.T
for _, l := range event.Labels {
label := oa.LabelByUUID(l.UUID)
if label == nil {
return errors.Errorf("unable to find label with UUID: %s", l.UUID)
return fmt.Errorf("unable to find label with UUID: %s", l.UUID)
}

scene.AppendToEventPreCommitHook(hooks.CommitAddedLabelsHook, &models.MsgLabelAdd{MsgID: inputMsgID, LabelID: label.ID()})
Expand Down
4 changes: 2 additions & 2 deletions core/handlers/ivr_created.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"context"
"fmt"
"log/slog"

"github.com/jmoiron/sqlx"
Expand All @@ -10,7 +11,6 @@ import (
"github.com/nyaruka/mailroom/core/hooks"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/runtime"
"github.com/pkg/errors"
)

func init() {
Expand All @@ -26,7 +26,7 @@ func handleIVRCreated(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, oa
// get our call
call := scene.Session().Call()
if call == nil {
return errors.Errorf("ivr session must have a call set")
return fmt.Errorf("ivr session must have a call set")
}

// if our call is no longer in progress, return
Expand Down
12 changes: 6 additions & 6 deletions core/handlers/msg_created.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"context"
"fmt"
"log/slog"

"github.com/jmoiron/sqlx"
Expand All @@ -11,7 +12,6 @@ import (
"github.com/nyaruka/mailroom/core/hooks"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/runtime"
"github.com/pkg/errors"
)

func init() {
Expand All @@ -34,7 +34,7 @@ func handlePreMsgCreated(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx,
if event.Msg.Channel() != nil {
channel = oa.ChannelByUUID(event.Msg.Channel().UUID)
if channel == nil {
return errors.Errorf("unable to load channel with uuid: %s", event.Msg.Channel().UUID)
return fmt.Errorf("unable to load channel with uuid: %s", event.Msg.Channel().UUID)
}
}

Expand All @@ -60,7 +60,7 @@ func handleMsgCreated(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, oa

// must be in a session
if scene.Session() == nil {
return errors.Errorf("cannot handle msg created event without session")
return fmt.Errorf("cannot handle msg created event without session")
}

slog.Debug("msg created", "contact", scene.ContactUUID(), "session", scene.SessionID(), "text", event.Msg.Text(), "urn", event.Msg.URN())
Expand All @@ -71,7 +71,7 @@ func handleMsgCreated(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, oa
if models.GetURNInt(urn, "id") == 0 {
urn, err := models.GetOrCreateURN(ctx, tx, oa, scene.ContactID(), event.Msg.URN())
if err != nil {
return errors.Wrapf(err, "unable to get or create URN: %s", event.Msg.URN())
return fmt.Errorf("unable to get or create URN: %s: %w", event.Msg.URN(), err)
}
// update our Msg with our full URN
event.Msg.SetURN(urn)
Expand All @@ -83,7 +83,7 @@ func handleMsgCreated(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, oa
if event.Msg.Channel() != nil {
channel = oa.ChannelByUUID(event.Msg.Channel().UUID)
if channel == nil {
return errors.Errorf("unable to load channel with uuid: %s", event.Msg.Channel().UUID)
return fmt.Errorf("unable to load channel with uuid: %s", event.Msg.Channel().UUID)
}
}

Expand All @@ -97,7 +97,7 @@ func handleMsgCreated(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, oa

msg, err := models.NewOutgoingFlowMsg(rt, oa.Org(), channel, scene.Session(), flow, event.Msg, event.CreatedOn())
if err != nil {
return errors.Wrapf(err, "error creating outgoing message to %s", event.Msg.URN())
return fmt.Errorf("error creating outgoing message to %s: %w", event.Msg.URN(), err)
}

// commit this message in the transaction
Expand Down
8 changes: 4 additions & 4 deletions core/handlers/optin_requested.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"context"
"fmt"
"log/slog"

"github.com/jmoiron/sqlx"
Expand All @@ -11,7 +12,6 @@ import (
"github.com/nyaruka/mailroom/core/hooks"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/runtime"
"github.com/pkg/errors"
)

func init() {
Expand All @@ -31,21 +31,21 @@ func handleOptInRequested(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx,
if models.GetURNInt(urn, "id") == 0 {
urn, err = models.GetOrCreateURN(ctx, tx, oa, scene.ContactID(), event.URN)
if err != nil {
return errors.Wrapf(err, "unable to get or create URN: %s", event.URN)
return fmt.Errorf("unable to get or create URN: %s: %w", event.URN, err)
}
}
}

// get our opt in
optIn := oa.OptInByUUID(event.OptIn.UUID)
if optIn == nil {
return errors.Errorf("unable to load optin with uuid: %s", event.OptIn.UUID)
return fmt.Errorf("unable to load optin with uuid: %s", event.OptIn.UUID)
}

// get our channel
channel := oa.ChannelByUUID(event.Channel.UUID)
if channel == nil {
return errors.Errorf("unable to load channel with uuid: %s", event.Channel.UUID)
return fmt.Errorf("unable to load channel with uuid: %s", event.Channel.UUID)
}

// and the flow
Expand Down
4 changes: 2 additions & 2 deletions core/handlers/service_called.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"context"
"fmt"
"log/slog"
"time"

Expand All @@ -11,7 +12,6 @@ import (
"github.com/nyaruka/mailroom/core/hooks"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/runtime"
"github.com/pkg/errors"
)

func init() {
Expand All @@ -29,7 +29,7 @@ func handleServiceCalled(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx,
if event.Service == "classifier" {
classifier = oa.ClassifierByUUID(event.Classifier.UUID)
if classifier == nil {
return errors.Errorf("unable to find classifier with UUID: %s", event.Classifier.UUID)
return fmt.Errorf("unable to find classifier with UUID: %s", event.Classifier.UUID)
}
}

Expand Down
6 changes: 3 additions & 3 deletions core/handlers/ticket_opened.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"context"
"fmt"
"log/slog"

"github.com/jmoiron/sqlx"
Expand All @@ -10,7 +11,6 @@ import (
"github.com/nyaruka/mailroom/core/hooks"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/runtime"
"github.com/pkg/errors"
)

func init() {
Expand All @@ -27,7 +27,7 @@ func handleTicketOpened(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, o
if event.Ticket.Topic != nil {
topic := oa.TopicByUUID(event.Ticket.Topic.UUID)
if topic == nil {
return errors.Errorf("unable to find topic with UUID: %s", event.Ticket.Topic.UUID)
return fmt.Errorf("unable to find topic with UUID: %s", event.Ticket.Topic.UUID)
}
topicID = topic.ID()
}
Expand All @@ -36,7 +36,7 @@ func handleTicketOpened(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, o
if event.Ticket.Assignee != nil {
assignee := oa.UserByEmail(event.Ticket.Assignee.Email)
if assignee == nil {
return errors.Errorf("unable to find user with email: %s", event.Ticket.Assignee.Email)
return fmt.Errorf("unable to find user with email: %s", event.Ticket.Assignee.Email)
}
assigneeID = assignee.ID()
}
Expand Down
8 changes: 4 additions & 4 deletions core/hooks/commit_field_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hooks
import (
"context"
"encoding/json"
"fmt"
"log/slog"

"github.com/jmoiron/sqlx"
Expand All @@ -11,7 +12,6 @@ import (
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/runtime"
"github.com/pkg/errors"
)

// CommitFieldChangesHook is our hook for contact field changes
Expand Down Expand Up @@ -55,7 +55,7 @@ func (h *commitFieldChangesHook) Apply(ctx context.Context, rt *runtime.Runtime,
// marshal the rest of our updates to JSON
fieldJSON, err := json.Marshal(updates)
if err != nil {
return errors.Wrapf(err, "error marshalling field values")
return fmt.Errorf("error marshalling field values: %w", err)
}

// and queue them up for our update
Expand All @@ -70,15 +70,15 @@ func (h *commitFieldChangesHook) Apply(ctx context.Context, rt *runtime.Runtime,
for _, fds := range fieldDeletes {
err := models.BulkQuery(ctx, "deleting contact field values", tx, sqlDeleteContactFields, fds)
if err != nil {
return errors.Wrapf(err, "error deleting contact fields")
return fmt.Errorf("error deleting contact fields: %w", err)
}
}

// then our updates
if len(fieldUpdates) > 0 {
err := models.BulkQuery(ctx, "updating contact field values", tx, sqlUpdateContactFields, fieldUpdates)
if err != nil {
return errors.Wrapf(err, "error updating contact fields")
return fmt.Errorf("error updating contact fields: %w", err)
}
}

Expand Down
6 changes: 3 additions & 3 deletions core/hooks/commit_group_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package hooks

import (
"context"
"fmt"

"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/runtime"

"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)

// CommitGroupChangesHook is our hook for all group changes
Expand Down Expand Up @@ -53,12 +53,12 @@ func (h *commitGroupChangesHook) Apply(ctx context.Context, rt *runtime.Runtime,
// do our updates
err := models.AddContactsToGroups(ctx, tx, adds)
if err != nil {
return errors.Wrapf(err, "error adding contacts to groups")
return fmt.Errorf("error adding contacts to groups: %w", err)
}

err = models.RemoveContactsFromGroups(ctx, tx, removes)
if err != nil {
return errors.Wrapf(err, "error removing contacts from groups")
return fmt.Errorf("error removing contacts from groups: %w", err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions core/hooks/commit_ivr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package hooks

import (
"context"
"fmt"

"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/runtime"

"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)

// CommitIVRHook is our hook for comitting scene messages / say commands
Expand All @@ -26,7 +26,7 @@ func (h *commitIVRHook) Apply(ctx context.Context, rt *runtime.Runtime, tx *sqlx

// insert all our messages
if err := models.InsertMessages(ctx, tx, msgs); err != nil {
return errors.Wrapf(err, "error writing messages")
return fmt.Errorf("error writing messages: %w", err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions core/hooks/commit_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package hooks

import (
"context"
"fmt"

"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/runtime"

"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)

// CommitMessagesHook is our hook for comitting scene messages
Expand All @@ -26,7 +26,7 @@ func (h *commitMessagesHook) Apply(ctx context.Context, rt *runtime.Runtime, tx

// insert all our messages
if err := models.InsertMessages(ctx, tx, msgs); err != nil {
return errors.Wrapf(err, "error writing messages")
return fmt.Errorf("error writing messages: %w", err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions core/hooks/commit_status_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package hooks

import (
"context"
"fmt"

"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/runtime"

"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)

// CommitStatusChangesHook is our hook for status changes
Expand All @@ -28,7 +28,7 @@ func (h *commitStatusChangesHook) Apply(ctx context.Context, rt *runtime.Runtime

err := models.UpdateContactStatus(ctx, tx, statusChanges)
if err != nil {
return errors.Wrapf(err, "error updating contact statuses")
return fmt.Errorf("error updating contact statuses: %w", err)
}
return nil
}
Loading

0 comments on commit 9ceb0f1

Please sign in to comment.