From 563b3825d8702b34390223a3a96e86d5ff651c18 Mon Sep 17 00:00:00 2001 From: Quentin Machu Date: Thu, 21 Jan 2016 18:09:38 -0500 Subject: [PATCH] database: let handleErrors deal with the not found case --- database/database.go | 2 +- database/pgsql/keyvalue.go | 10 +--------- database/pgsql/layer.go | 4 ---- database/pgsql/lock.go | 5 ----- database/pgsql/pgsql.go | 5 +++-- 5 files changed, 5 insertions(+), 21 deletions(-) diff --git a/database/database.go b/database/database.go index 04439f80..1af4be06 100644 --- a/database/database.go +++ b/database/database.go @@ -39,7 +39,7 @@ var ( type Datastore interface { // Layer InsertLayer(Layer) error - FindLayer(name string, withFeatures, withVulnerabilities bool) (layer Layer, err error) + FindLayer(name string, withFeatures, withVulnerabilities bool) (Layer, error) DeleteLayer(name string) error // Vulnerability diff --git a/database/pgsql/keyvalue.go b/database/pgsql/keyvalue.go index ce16e894..6cdaf31e 100644 --- a/database/pgsql/keyvalue.go +++ b/database/pgsql/keyvalue.go @@ -14,11 +14,7 @@ package pgsql -import ( - "database/sql" - - cerrors "github.com/coreos/clair/utils/errors" -) +import cerrors "github.com/coreos/clair/utils/errors" // InsertKeyValue stores (or updates) a single key / value tuple. func (pgSQL *pgSQL) InsertKeyValue(key, value string) (err error) { @@ -66,10 +62,6 @@ func (pgSQL *pgSQL) InsertKeyValue(key, value string) (err error) { func (pgSQL *pgSQL) GetKeyValue(key string) (string, error) { var value string err := pgSQL.QueryRow(getQuery("s_keyvalue"), key).Scan(&value) - - if err == sql.ErrNoRows { - return "", nil - } if err != nil { return "", handleError("s_keyvalue", err) } diff --git a/database/pgsql/layer.go b/database/pgsql/layer.go index c98e657e..3f0e28d7 100644 --- a/database/pgsql/layer.go +++ b/database/pgsql/layer.go @@ -34,10 +34,6 @@ func (pgSQL *pgSQL) FindLayer(name string, withFeatures, withVulnerabilities boo err := pgSQL.QueryRow(getQuery("s_layer"), name). Scan(&layer.ID, &layer.Name, &layer.EngineVersion, &parentID, &parentName, &namespaceID, &namespaceName) - - if err == sql.ErrNoRows { - return layer, cerrors.ErrNotFound - } if err != nil { return layer, handleError("s_layer", err) } diff --git a/database/pgsql/lock.go b/database/pgsql/lock.go index 1fa42fca..c9cb2508 100644 --- a/database/pgsql/lock.go +++ b/database/pgsql/lock.go @@ -15,7 +15,6 @@ package pgsql import ( - "database/sql" "time" cerrors "github.com/coreos/clair/utils/errors" @@ -83,10 +82,6 @@ func (pgSQL *pgSQL) FindLock(name string) (string, time.Time, error) { var owner string var until time.Time err := pgSQL.QueryRow(getQuery("f_lock"), name).Scan(&owner, &until) - - if err == sql.ErrNoRows { - return owner, until, cerrors.ErrNotFound - } if err != nil { return owner, until, handleError("f_lock", err) } diff --git a/database/pgsql/pgsql.go b/database/pgsql/pgsql.go index 7e343fe2..797e95e2 100644 --- a/database/pgsql/pgsql.go +++ b/database/pgsql/pgsql.go @@ -194,16 +194,17 @@ func OpenForTest(name string, withTestData bool) (*pgSQLTest, error) { // handleError logs an error with an extra description and masks the error if it's an SQL one. // This ensures we never return plain SQL errors and leak anything. func handleError(desc string, err error) error { - log.Errorf("%s: %v", desc, err) - if _, ok := err.(*pq.Error); ok { + log.Errorf("%s: %v", desc, err) return database.ErrBackendException } else if err == sql.ErrNoRows { return cerrors.ErrNotFound } else if err == sql.ErrTxDone || strings.HasPrefix(err.Error(), "sql:") { + log.Errorf("%s: %v", desc, err) return database.ErrBackendException } + log.Errorf("%s: %v", desc, err) return err }