database: let handleErrors deal with the not found case

This commit is contained in:
Quentin Machu 2016-01-21 18:09:38 -05:00 committed by Jimmy Zelinskie
parent c60d0054fa
commit 563b3825d8
5 changed files with 5 additions and 21 deletions

View File

@ -39,7 +39,7 @@ var (
type Datastore interface { type Datastore interface {
// Layer // Layer
InsertLayer(Layer) error 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 DeleteLayer(name string) error
// Vulnerability // Vulnerability

View File

@ -14,11 +14,7 @@
package pgsql package pgsql
import ( import cerrors "github.com/coreos/clair/utils/errors"
"database/sql"
cerrors "github.com/coreos/clair/utils/errors"
)
// InsertKeyValue stores (or updates) a single key / value tuple. // InsertKeyValue stores (or updates) a single key / value tuple.
func (pgSQL *pgSQL) InsertKeyValue(key, value string) (err error) { 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) { func (pgSQL *pgSQL) GetKeyValue(key string) (string, error) {
var value string var value string
err := pgSQL.QueryRow(getQuery("s_keyvalue"), key).Scan(&value) err := pgSQL.QueryRow(getQuery("s_keyvalue"), key).Scan(&value)
if err == sql.ErrNoRows {
return "", nil
}
if err != nil { if err != nil {
return "", handleError("s_keyvalue", err) return "", handleError("s_keyvalue", err)
} }

View File

@ -34,10 +34,6 @@ func (pgSQL *pgSQL) FindLayer(name string, withFeatures, withVulnerabilities boo
err := pgSQL.QueryRow(getQuery("s_layer"), name). err := pgSQL.QueryRow(getQuery("s_layer"), name).
Scan(&layer.ID, &layer.Name, &layer.EngineVersion, &parentID, &parentName, &namespaceID, Scan(&layer.ID, &layer.Name, &layer.EngineVersion, &parentID, &parentName, &namespaceID,
&namespaceName) &namespaceName)
if err == sql.ErrNoRows {
return layer, cerrors.ErrNotFound
}
if err != nil { if err != nil {
return layer, handleError("s_layer", err) return layer, handleError("s_layer", err)
} }

View File

@ -15,7 +15,6 @@
package pgsql package pgsql
import ( import (
"database/sql"
"time" "time"
cerrors "github.com/coreos/clair/utils/errors" 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 owner string
var until time.Time var until time.Time
err := pgSQL.QueryRow(getQuery("f_lock"), name).Scan(&owner, &until) err := pgSQL.QueryRow(getQuery("f_lock"), name).Scan(&owner, &until)
if err == sql.ErrNoRows {
return owner, until, cerrors.ErrNotFound
}
if err != nil { if err != nil {
return owner, until, handleError("f_lock", err) return owner, until, handleError("f_lock", err)
} }

View File

@ -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. // 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. // This ensures we never return plain SQL errors and leak anything.
func handleError(desc string, err error) error { func handleError(desc string, err error) error {
log.Errorf("%s: %v", desc, err)
if _, ok := err.(*pq.Error); ok { if _, ok := err.(*pq.Error); ok {
log.Errorf("%s: %v", desc, err)
return database.ErrBackendException return database.ErrBackendException
} else if err == sql.ErrNoRows { } else if err == sql.ErrNoRows {
return cerrors.ErrNotFound return cerrors.ErrNotFound
} else if err == sql.ErrTxDone || strings.HasPrefix(err.Error(), "sql:") { } else if err == sql.ErrTxDone || strings.HasPrefix(err.Error(), "sql:") {
log.Errorf("%s: %v", desc, err)
return database.ErrBackendException return database.ErrBackendException
} }
log.Errorf("%s: %v", desc, err)
return err return err
} }