Skip to content

Commit

Permalink
database: let handleErrors deal with the not found case
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin-M authored and jzelinskie committed Feb 24, 2016
1 parent c60d005 commit 563b382
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 21 deletions.
2 changes: 1 addition & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 1 addition & 9 deletions database/pgsql/keyvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 0 additions & 4 deletions database/pgsql/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 0 additions & 5 deletions database/pgsql/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package pgsql

import (
"database/sql"
"time"

cerrors "github.com/coreos/clair/utils/errors"
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 3 additions & 2 deletions database/pgsql/pgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 563b382

Please sign in to comment.