Skip to content

Commit

Permalink
[FAB-5678] Improve SQL not found error message
Browse files Browse the repository at this point in the history
Provide a more user-friendly error message when
items are not found in database.

Change-Id: I67ad52ece2c48130495b07476496774e082a789c
Signed-off-by: Saad Karim <[email protected]>
  • Loading branch information
Saad Karim committed Aug 15, 2017
1 parent 00caa9c commit da97bc8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/certdbaccessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (d *CertDBAccessor) GetCertificateWithID(serial, aki string) (crs CertRecor

err = d.db.Get(&crs, fmt.Sprintf(d.db.Rebind(selectSQL), sqlstruct.Columns(CertRecord{})), serial, aki)
if err != nil {
return crs, err
return crs, dbGetError(err, "Certificate")
}

return crs, nil
Expand Down
51 changes: 51 additions & 0 deletions lib/dasqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package lib_test

import (
"fmt"
"os"
"strings"
"testing"
Expand All @@ -27,6 +28,7 @@ import (
"github.com/hyperledger/fabric-ca/lib/spi"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
)

const (
Expand Down Expand Up @@ -234,3 +236,52 @@ func testDeleteAffiliation(ta TestAccessor, t *testing.T) {
t.Error("Should have errored, and not returned any results")
}
}

func TestDBErrorMessages(t *testing.T) {
var err error

if _, err = os.Stat(dbPath); err != nil {
if os.IsNotExist(err) {
os.MkdirAll(dbPath, 0755)
}
} else {
os.RemoveAll(dbPath)
os.MkdirAll(dbPath, 0755)
}

dataSource := dbPath + "/fabric-ca.db"
db, _, err := dbutil.NewUserRegistrySQLLite3(dataSource)
if err != nil {
t.Error("Failed to open connection to DB")
}

accessor := NewDBAccessor()
accessor.SetDB(db)

ta := TestAccessor{
Accessor: accessor,
DB: db,
}

expectedErr := "%s not found"
_, err = ta.Accessor.GetAffiliation("hyperledger")
if assert.Error(t, err, "Should have errored, and not returned any results") {
assert.Contains(t, err.Error(), fmt.Sprintf(expectedErr, "Affiliation"))
}

_, err = ta.Accessor.GetUser("testuser", []string{})
if assert.Error(t, err, "Should have errored, and not returned any results") {
assert.Contains(t, err.Error(), fmt.Sprintf(expectedErr, "User"))
}

_, err = ta.Accessor.GetUserInfo("testuser")
if assert.Error(t, err, "Should have errored, and not returned any results") {
assert.Contains(t, err.Error(), fmt.Sprintf(expectedErr, "User"))
}

newCertDBAcc := NewCertDBAccessor(db)
_, err = newCertDBAcc.GetCertificateWithID("serial", "aki")
if assert.Error(t, err, "Should have errored, and not returned any results") {
assert.Contains(t, err.Error(), fmt.Sprintf(expectedErr, "Certificate"))
}
}
13 changes: 10 additions & 3 deletions lib/dbaccessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (d *Accessor) GetUser(id string, attrs []string) (spi.User, error) {
var userRec UserRecord
err = d.db.Get(&userRec, d.db.Rebind(getUser), id)
if err != nil {
return nil, err
return nil, dbGetError(err, "User")
}

return d.newDBUser(&userRec), nil
Expand All @@ -253,7 +253,7 @@ func (d *Accessor) GetUserInfo(id string) (spi.UserInfo, error) {
var userRec UserRecord
err = d.db.Get(&userRec, d.db.Rebind(getUser), id)
if err != nil {
return userInfo, err
return userInfo, dbGetError(err, "User")
}

var attributes []api.Attribute
Expand Down Expand Up @@ -312,7 +312,7 @@ func (d *Accessor) GetAffiliation(name string) (spi.Affiliation, error) {

err = d.db.Get(&affiliation, d.db.Rebind(getAffiliation), name)
if err != nil {
return nil, err
return nil, dbGetError(err, "Affiliation")
}

return &affiliation, nil
Expand Down Expand Up @@ -435,3 +435,10 @@ func (u *DBUser) GetAffiliationPath() []string {
func (u *DBUser) GetAttribute(name string) string {
return u.attrs[name]
}

func dbGetError(err error, prefix string) error {
if err.Error() == "sql: no rows in result set" {
return fmt.Errorf("%s not found", prefix)
}
return err
}

0 comments on commit da97bc8

Please sign in to comment.