Skip to content

Commit

Permalink
feat: better SQL error handling (notfound != internal error)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Jan 25, 2019
1 parent 82b0a36 commit f39eda7
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 130 deletions.
8 changes: 4 additions & 4 deletions core/node/event_handlers.go
Expand Up @@ -56,7 +56,7 @@ func (n *Node) handleContactRequest(ctx context.Context, input *p2p.Event) error
})

if err := entity.SaveDevices(sql, attrs.Me.ID, devices); err != nil {
return errorcodes.ErrDb.Wrap(err)
return errorcodes.ErrDbCreate.Wrap(err)
}

// nothing more to do, now we wait for the UI to accept the request
Expand All @@ -68,7 +68,7 @@ func (n *Node) handleContactRequestAccepted(ctx context.Context, input *p2p.Even
sql := n.sql(ctx)
contact, err := bsql.ContactByID(sql, input.SenderID)
if err != nil {
return errorcodes.ErrDbNothingFound.Wrap(err)
return bsql.GenericError(err)
}

contact.Status = entity.Contact_IsFriend
Expand Down Expand Up @@ -112,7 +112,7 @@ func (n *Node) handleContactShareMe(ctx context.Context, input *p2p.Event) error
sql := n.sql(ctx)
contact, err := bsql.ContactByID(sql, input.SenderID)
if err != nil {
return errorcodes.ErrDbNothingFound.Wrap(err)
return bsql.GenericError(err)
}

// FIXME: UI: ask for confirmation before update
Expand All @@ -126,7 +126,7 @@ func (n *Node) handleContactShareMe(ctx context.Context, input *p2p.Event) error
}

if err := entity.SaveDevices(sql, attrs.Me.ID, attrs.Me.Devices); err != nil {
return errorcodes.ErrDb.Wrap(err)
return errorcodes.ErrDbCreate.Wrap(err)
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions core/node/nodeapi.go
Expand Up @@ -163,7 +163,7 @@ func (n *Node) ConversationRemove(ctx context.Context, input *entity.Conversatio

// get conversation
if err = n.sql(ctx).First(input).Error; err != nil {
return nil, errorcodes.ErrDbNothingFound.Wrap(err)
return nil, bsql.GenericError(err)
}

// remove conversation
Expand All @@ -185,7 +185,7 @@ func (n *Node) GetEvent(ctx context.Context, input *p2p.Event) (*p2p.Event, erro
sql := n.sql(ctx)
event := &p2p.Event{}
if err := sql.Where(input).First(event).Error; err != nil {
return nil, errorcodes.ErrDbNothingFound.Wrap(err)
return nil, bsql.GenericError(err)
}

return event, nil
Expand Down Expand Up @@ -612,7 +612,7 @@ func (n *Node) Conversation(ctx context.Context, input *entity.Conversation) (*e
sql := n.sql(ctx)
output := &entity.Conversation{}
if err := sql.Where(input).First(output).Error; err != nil {
return nil, errorcodes.ErrDbNothingFound.Wrap(err)
return nil, bsql.GenericError(err)
}

return output, nil
Expand All @@ -629,7 +629,7 @@ func (n *Node) ConversationMember(ctx context.Context, input *entity.Conversatio
sql := n.sql(ctx)
output := &entity.ConversationMember{}
if err := sql.Where(input).First(output).Error; err != nil {
return nil, errorcodes.ErrDbNothingFound.Wrap(err)
return nil, bsql.GenericError(err)
}

return output, nil
Expand Down
6 changes: 3 additions & 3 deletions core/node/nodeapi_devtools.go
Expand Up @@ -34,7 +34,7 @@ import (
"berty.tech/core/pkg/errorcodes"
"berty.tech/core/pkg/logmanager"
"berty.tech/core/pkg/tracing"
"berty.tech/core/sql"
bsql "berty.tech/core/sql"
"berty.tech/core/testrunner"
)

Expand Down Expand Up @@ -191,7 +191,7 @@ func (n *Node) NodeInfos(ctx context.Context) (*deviceinfo.DeviceInfos, error) {
sqlMetrics := map[string]int{}
// FIXME: add some info about last addition date, last modification date
db := n.sql(ctx)
for _, table := range sql.AllTables() {
for _, table := range bsql.AllTables() {
var count int
if err := db.Table(table).Count(&count).Error; err != nil {
sqlMetrics[table] = -1
Expand Down Expand Up @@ -384,7 +384,7 @@ func (n *Node) DebugRequeueEvent(ctx context.Context, input *node.EventIDInput)
sql := n.sql(ctx)
var event p2p.Event
if err := sql.First(&event, "ID = ?", input.EventID).Error; err != nil {
return nil, errorcodes.ErrDbNothingFound.Wrap(err)
return nil, bsql.GenericError(err)
}

if err := n.EventRequeue(ctx, &event); err != nil {
Expand Down
7 changes: 4 additions & 3 deletions core/node/nodeapi_push.go
Expand Up @@ -11,6 +11,7 @@ import (
"berty.tech/core/pkg/errorcodes"
"berty.tech/core/pkg/tracing"
"berty.tech/core/push"
"berty.tech/core/sql"
"github.com/google/uuid"
)

Expand Down Expand Up @@ -42,7 +43,7 @@ func (n *Node) DevicePushConfigCreate(ctx context.Context, input *node.DevicePus
if input.RelayPubkey == "" {
config, err := n.Config(ctx)
if err != nil {
return nil, errorcodes.ErrDbNothingFound.Wrap(err)
return nil, sql.GenericError(err)
}

if input.PushType == push.DevicePushType_APNS {
Expand Down Expand Up @@ -89,7 +90,7 @@ func (n *Node) DevicePushConfigRemove(ctx context.Context, devicePushConfig *ent

// get devicePushConfig
if err = n.sql(ctx).First(devicePushConfig, &entity.DevicePushConfig{ID: devicePushConfig.ID}).Error; err != nil {
return nil, errorcodes.ErrDbNothingFound.Wrap(err)
return nil, sql.GenericError(err)
}

if devicePushConfig == nil {
Expand Down Expand Up @@ -118,7 +119,7 @@ func (n *Node) DevicePushConfigUpdate(ctx context.Context, input *entity.DeviceP

// get devicePushConfig
if err = n.sql(ctx).First(input, devicePushConfig).Error; err != nil {
return nil, errorcodes.ErrDbNothingFound.Wrap(err)
return nil, sql.GenericError(err)
}

if len(input.RelayPubkey) > 0 {
Expand Down
6 changes: 4 additions & 2 deletions core/node/p2papi.go
Expand Up @@ -17,6 +17,8 @@ import (
"berty.tech/core/network"
"berty.tech/core/pkg/errorcodes"
"berty.tech/core/pkg/tracing"
"berty.tech/core/sql"
bsql "berty.tech/core/sql"
)

// WithP2PGrpcServer registers the Node as a 'berty.p2p' protobuf server implementation
Expand Down Expand Up @@ -127,7 +129,7 @@ func (n *Node) OpenEnvelope(ctx context.Context, envelope *p2p.Envelope) (*p2p.E
} else {
contact := &entity.Contact{}
if err := n.sql(ctx).First(contact, &entity.Contact{ID: device.ContactID}).Error; err != nil {
return nil, errorcodes.ErrDbNothingFound.Wrap(err)
return nil, bsql.GenericError(err)
}

trusted = contact.IsTrusted()
Expand Down Expand Up @@ -214,7 +216,7 @@ func (n *Node) getPushDestinationsForEvent(ctx context.Context, event *p2p.Event
Select("device_id").
Where("contact_id IN (?)", subqueryContactIDs).
SubQuery()).Error; err != nil {
return nil, errorcodes.ErrDb.Wrap(err)
return nil, sql.GenericError(err)
}

if len(pushIdentifiers) == 0 {
Expand Down

0 comments on commit f39eda7

Please sign in to comment.