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

Do more error wrapping when creating contacts and URNs #396

Merged
merged 1 commit into from
Jan 10, 2022
Merged
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
24 changes: 12 additions & 12 deletions backends/rapidpro/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"unicode/utf8"

"github.com/nyaruka/courier"
"github.com/nyaruka/gocommon/dbutil"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/librato"
"github.com/nyaruka/null"
"github.com/pkg/errors"

"github.com/jmoiron/sqlx"
"github.com/lib/pq"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -102,7 +103,7 @@ func contactForURN(ctx context.Context, b *backend, org OrgID, channel *DBChanne
err := b.db.GetContext(ctx, contact, lookupContactFromURNSQL, urn.Identity(), org)
if err != nil && err != sql.ErrNoRows {
logrus.WithError(err).WithField("urn", urn.Identity()).WithField("org_id", org).Error("error looking up contact")
return nil, err
return nil, errors.Wrap(err, "error looking up contact by URN")
}

// we found it, return it
Expand All @@ -111,14 +112,14 @@ func contactForURN(ctx context.Context, b *backend, org OrgID, channel *DBChanne
tx, err := b.db.BeginTxx(ctx, nil)
if err != nil {
logrus.WithError(err).WithField("urn", urn.Identity()).WithField("org_id", org).Error("error looking up contact")
return nil, err
return nil, errors.Wrap(err, "error beginning transaction")
}

err = setDefaultURN(tx, channel, contact, urn, auth)
if err != nil {
logrus.WithError(err).WithField("urn", urn.Identity()).WithField("org_id", org).Error("error looking up contact")
tx.Rollback()
return nil, err
return nil, errors.Wrap(err, "error setting default URN for contact")
}
return contact, tx.Commit()
}
Expand Down Expand Up @@ -166,13 +167,13 @@ func contactForURN(ctx context.Context, b *backend, org OrgID, channel *DBChanne
// insert it
tx, err := b.db.BeginTxx(ctx, nil)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "error beginning transaction")
}

err = insertContact(tx, contact)
if err != nil {
tx.Rollback()
return nil, err
return nil, errors.Wrap(err, "error inserting contact")
}

// used for unit testing contact races
Expand All @@ -186,13 +187,12 @@ func contactForURN(ctx context.Context, b *backend, org OrgID, channel *DBChanne
contactURN, err := contactURNForURN(tx, channel, contact.ID_, urn, auth)
if err != nil {
tx.Rollback()
if pqErr, ok := err.(*pq.Error); ok {

if dbutil.IsUniqueViolation(err) {
// if this was a duplicate URN, start over with a contact lookup
if pqErr.Code.Name() == "unique_violation" {
return contactForURN(ctx, b, org, channel, urn, auth, name)
}
return contactForURN(ctx, b, org, channel, urn, auth, name)
}
return nil, err
return nil, errors.Wrap(err, "error getting URN for contact")
}

// we stole the URN from another contact, roll back and start over
Expand All @@ -204,7 +204,7 @@ func contactForURN(ctx context.Context, b *backend, org OrgID, channel *DBChanne
// all is well, we created the new contact, commit and move forward
err = tx.Commit()
if err != nil {
return nil, err
return nil, errors.Wrap(err, "error commiting transaction")
}

// store this URN on our contact
Expand Down
7 changes: 4 additions & 3 deletions backends/rapidpro/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"github.com/buger/jsonparser"
"github.com/pkg/errors"

"mime"

Expand Down Expand Up @@ -134,7 +135,7 @@ func writeMsgToDB(ctx context.Context, b *backend, m *DBMsg) error {

// our db is down, write to the spool, we will write/queue this later
if err != nil {
return err
return errors.Wrap(err, "error getting contact for message")
}

// set our contact and urn ids from our contact
Expand All @@ -143,14 +144,14 @@ func writeMsgToDB(ctx context.Context, b *backend, m *DBMsg) error {

rows, err := b.db.NamedQueryContext(ctx, insertMsgSQL, m)
if err != nil {
return err
return errors.Wrap(err, "error inserting message")
}
defer rows.Close()

rows.Next()
err = rows.Scan(&m.ID_)
if err != nil {
return err
return errors.Wrap(err, "error scanning for inserted message id")
}

// queue this up to be handled by RapidPro
Expand Down
9 changes: 5 additions & 4 deletions backends/rapidpro/urn.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

"github.com/nyaruka/null"
"github.com/pkg/errors"

"github.com/jmoiron/sqlx"
"github.com/nyaruka/courier"
Expand Down Expand Up @@ -209,14 +210,14 @@ func contactURNForURN(db *sqlx.Tx, channel *DBChannel, contactID ContactID, urn
}
err := db.Get(contactURN, selectOrgURN, channel.OrgID(), urn.Identity())
if err != nil && err != sql.ErrNoRows {
return nil, err
return nil, errors.Wrap(err, "error looking up URN by identity")
}

// we didn't find it, let's insert it
if err == sql.ErrNoRows {
err = insertContactURN(db, contactURN)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "error inserting URN")
}
}

Expand All @@ -232,7 +233,7 @@ func contactURNForURN(db *sqlx.Tx, channel *DBChannel, contactID ContactID, urn
contactURN.Display = display
err = updateContactURN(db, contactURN)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "error updating URN")
}
}

Expand All @@ -242,7 +243,7 @@ func contactURNForURN(db *sqlx.Tx, channel *DBChannel, contactID ContactID, urn
err = updateContactURN(db, contactURN)
}

return contactURN, err
return contactURN, errors.Wrap(err, "error updating URN auth")
}

const insertURN = `
Expand Down