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

Improve database error handling #476

Merged
merged 2 commits into from
Sep 20, 2018
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
17 changes: 11 additions & 6 deletions database/services/all.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package services

import (
"encoding/json"

"github.com/mesg-foundation/core/service"
"github.com/sirupsen/logrus"
)

// All returns all deployed services.
Expand All @@ -16,11 +15,17 @@ func All() ([]*service.Service, error) {
var services []*service.Service
iter := db.NewIterator(nil, nil)
for iter.Next() {
var service service.Service
if err := json.Unmarshal(iter.Value(), &service); err != nil {
return nil, err
service, err := decode(string(iter.Key()), iter.Value())
if err != nil {
// Ignore all decode errors (possibly due to a service structure change or database corruption)
if decodeErr, ok := err.(*DecodeError); ok {
logrus.WithField("service", decodeErr.Hash).Warning(decodeErr.Error())
} else {
return nil, err
}
} else {
services = append(services, service)
}
services = append(services, &service)
}
iter.Release()
return services, iter.Error()
Expand Down
21 changes: 21 additions & 0 deletions database/services/encode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package services

import (
"encoding/json"

"github.com/mesg-foundation/core/service"
)

// encode returns the marshaled version of the service
func encode(service *service.Service) ([]byte, error) {
return json.Marshal(service)
}

// decode decodes the data and return an DecodeError if not possible
func decode(hash string, data []byte) (*service.Service, error) {
s := &service.Service{}
if err := json.Unmarshal(data, s); err != nil {
return nil, &DecodeError{Hash: hash}
}
return s, nil
}
14 changes: 14 additions & 0 deletions database/services/encode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package services

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestDecodeError(t *testing.T) {
hash := "IDToTest"
_, err := decode(hash, []byte("oaiwdhhiodoihwaiohwa"))
require.Error(t, err)
require.IsType(t, &DecodeError{}, err)
}
13 changes: 12 additions & 1 deletion database/services/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package services

import (
"fmt"

leveldbErrors "github.com/syndtr/goleveldb/leveldb/errors"
)

Expand All @@ -10,7 +12,7 @@ type NotFound struct {
}

func (e NotFound) Error() string {
return "Database services: Service with hash '" + e.Hash + "' not found"
return fmt.Sprintf("Database services: Service %q not found", e.Hash)
}

func handleErrorNotFound(err error, hash string) error {
Expand All @@ -19,3 +21,12 @@ func handleErrorNotFound(err error, hash string) error {
}
return err
}

// DecodeError represents a service impossible to decode
type DecodeError struct {
Hash string
}

func (e *DecodeError) Error() string {
return fmt.Sprintf("Database services: Could not decode service %q.", e.Hash)
}
5 changes: 1 addition & 4 deletions database/services/get.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package services

import (
"encoding/json"

"github.com/mesg-foundation/core/service"
)

Expand All @@ -18,6 +16,5 @@ func Get(id string) (*service.Service, error) {
err = handleErrorNotFound(err, id)
return nil, err
}
s := &service.Service{}
return s, json.Unmarshal(bytes, s)
return decode(id, bytes)
}
4 changes: 1 addition & 3 deletions database/services/save.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package services

import (
"encoding/json"

"github.com/mesg-foundation/core/service"
)

// Save stores a service in the database.
func Save(service *service.Service) error {
bytes, err := json.Marshal(service)
bytes, err := encode(service)
if err != nil {
return err
}
Expand Down