Skip to content

Commit

Permalink
pkg: cerrors -> commonerr
Browse files Browse the repository at this point in the history
  • Loading branch information
jzelinskie committed Jan 23, 2017
1 parent 8dea744 commit 78cef02
Show file tree
Hide file tree
Showing 24 changed files with 120 additions and 119 deletions.
30 changes: 15 additions & 15 deletions api/v1/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (

"github.com/coreos/clair/api/context"
"github.com/coreos/clair/database"
"github.com/coreos/clair/pkg/commonerr"
"github.com/coreos/clair/pkg/tarutil"
cerrors "github.com/coreos/clair/utils/errors"
"github.com/coreos/clair/worker"
)

Expand Down Expand Up @@ -118,7 +118,7 @@ func postLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx
return postLayerRoute, statusUnprocessableEntity
}

if _, badreq := err.(*cerrors.ErrBadRequest); badreq {
if _, badreq := err.(*commonerr.ErrBadRequest); badreq {
writeResponse(w, r, http.StatusBadRequest, LayerEnvelope{Error: &Error{err.Error()}})
return postLayerRoute, http.StatusBadRequest
}
Expand All @@ -143,7 +143,7 @@ func getLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *
_, withVulnerabilities := r.URL.Query()["vulnerabilities"]

dbLayer, err := ctx.Store.FindLayer(p.ByName("layerName"), withFeatures, withVulnerabilities)
if err == cerrors.ErrNotFound {
if err == commonerr.ErrNotFound {
writeResponse(w, r, http.StatusNotFound, LayerEnvelope{Error: &Error{err.Error()}})
return getLayerRoute, http.StatusNotFound
} else if err != nil {
Expand All @@ -159,7 +159,7 @@ func getLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *

func deleteLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
err := ctx.Store.DeleteLayer(p.ByName("layerName"))
if err == cerrors.ErrNotFound {
if err == commonerr.ErrNotFound {
writeResponse(w, r, http.StatusNotFound, LayerEnvelope{Error: &Error{err.Error()}})
return deleteLayerRoute, http.StatusNotFound
} else if err != nil {
Expand Down Expand Up @@ -223,7 +223,7 @@ func getVulnerabilities(w http.ResponseWriter, r *http.Request, p httprouter.Par
}

dbVulns, nextPage, err := ctx.Store.ListVulnerabilities(namespace, limit, page)
if err == cerrors.ErrNotFound {
if err == commonerr.ErrNotFound {
writeResponse(w, r, http.StatusNotFound, VulnerabilityEnvelope{Error: &Error{err.Error()}})
return getVulnerabilityRoute, http.StatusNotFound
} else if err != nil {
Expand Down Expand Up @@ -273,7 +273,7 @@ func postVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Para
err = ctx.Store.InsertVulnerabilities([]database.Vulnerability{vuln}, true)
if err != nil {
switch err.(type) {
case *cerrors.ErrBadRequest:
case *commonerr.ErrBadRequest:
writeResponse(w, r, http.StatusBadRequest, VulnerabilityEnvelope{Error: &Error{err.Error()}})
return postVulnerabilityRoute, http.StatusBadRequest
default:
Expand All @@ -290,7 +290,7 @@ func getVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Param
_, withFixedIn := r.URL.Query()["fixedIn"]

dbVuln, err := ctx.Store.FindVulnerability(p.ByName("namespaceName"), p.ByName("vulnerabilityName"))
if err == cerrors.ErrNotFound {
if err == commonerr.ErrNotFound {
writeResponse(w, r, http.StatusNotFound, VulnerabilityEnvelope{Error: &Error{err.Error()}})
return getVulnerabilityRoute, http.StatusNotFound
} else if err != nil {
Expand Down Expand Up @@ -334,7 +334,7 @@ func putVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Param
err = ctx.Store.InsertVulnerabilities([]database.Vulnerability{vuln}, true)
if err != nil {
switch err.(type) {
case *cerrors.ErrBadRequest:
case *commonerr.ErrBadRequest:
writeResponse(w, r, http.StatusBadRequest, VulnerabilityEnvelope{Error: &Error{err.Error()}})
return putVulnerabilityRoute, http.StatusBadRequest
default:
Expand All @@ -349,7 +349,7 @@ func putVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Param

func deleteVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
err := ctx.Store.DeleteVulnerability(p.ByName("namespaceName"), p.ByName("vulnerabilityName"))
if err == cerrors.ErrNotFound {
if err == commonerr.ErrNotFound {
writeResponse(w, r, http.StatusNotFound, VulnerabilityEnvelope{Error: &Error{err.Error()}})
return deleteVulnerabilityRoute, http.StatusNotFound
} else if err != nil {
Expand All @@ -363,7 +363,7 @@ func deleteVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Pa

func getFixes(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
dbVuln, err := ctx.Store.FindVulnerability(p.ByName("namespaceName"), p.ByName("vulnerabilityName"))
if err == cerrors.ErrNotFound {
if err == commonerr.ErrNotFound {
writeResponse(w, r, http.StatusNotFound, FeatureEnvelope{Error: &Error{err.Error()}})
return getFixesRoute, http.StatusNotFound
} else if err != nil {
Expand Down Expand Up @@ -403,11 +403,11 @@ func putFix(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *co
err = ctx.Store.InsertVulnerabilityFixes(p.ByName("vulnerabilityNamespace"), p.ByName("vulnerabilityName"), []database.FeatureVersion{dbFix})
if err != nil {
switch err.(type) {
case *cerrors.ErrBadRequest:
case *commonerr.ErrBadRequest:
writeResponse(w, r, http.StatusBadRequest, FeatureEnvelope{Error: &Error{err.Error()}})
return putFixRoute, http.StatusBadRequest
default:
if err == cerrors.ErrNotFound {
if err == commonerr.ErrNotFound {
writeResponse(w, r, http.StatusNotFound, FeatureEnvelope{Error: &Error{err.Error()}})
return putFixRoute, http.StatusNotFound
}
Expand All @@ -422,7 +422,7 @@ func putFix(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *co

func deleteFix(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
err := ctx.Store.DeleteVulnerabilityFix(p.ByName("vulnerabilityNamespace"), p.ByName("vulnerabilityName"), p.ByName("fixName"))
if err == cerrors.ErrNotFound {
if err == commonerr.ErrNotFound {
writeResponse(w, r, http.StatusNotFound, FeatureEnvelope{Error: &Error{err.Error()}})
return deleteFixRoute, http.StatusNotFound
} else if err != nil {
Expand Down Expand Up @@ -468,7 +468,7 @@ func getNotification(w http.ResponseWriter, r *http.Request, p httprouter.Params
}

dbNotification, nextPage, err := ctx.Store.GetNotification(p.ByName("notificationName"), limit, page)
if err == cerrors.ErrNotFound {
if err == commonerr.ErrNotFound {
writeResponse(w, r, http.StatusNotFound, NotificationEnvelope{Error: &Error{err.Error()}})
return deleteNotificationRoute, http.StatusNotFound
} else if err != nil {
Expand All @@ -484,7 +484,7 @@ func getNotification(w http.ResponseWriter, r *http.Request, p httprouter.Params

func deleteNotification(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
err := ctx.Store.DeleteNotification(p.ByName("notificationName"))
if err == cerrors.ErrNotFound {
if err == commonerr.ErrNotFound {
writeResponse(w, r, http.StatusNotFound, NotificationEnvelope{Error: &Error{err.Error()}})
return deleteNotificationRoute, http.StatusNotFound
} else if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions database/pgsql/feature.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 clair authors
// Copyright 2017 clair authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -21,12 +21,12 @@ import (

"github.com/coreos/clair/database"
"github.com/coreos/clair/ext/versionfmt"
cerrors "github.com/coreos/clair/utils/errors"
"github.com/coreos/clair/pkg/commonerr"
)

func (pgSQL *pgSQL) insertFeature(feature database.Feature) (int, error) {
if feature.Name == "" {
return 0, cerrors.NewBadRequestError("could not find/insert invalid Feature")
return 0, commonerr.NewBadRequestError("could not find/insert invalid Feature")
}

// Do cache lookup.
Expand Down Expand Up @@ -65,7 +65,7 @@ func (pgSQL *pgSQL) insertFeature(feature database.Feature) (int, error) {
func (pgSQL *pgSQL) insertFeatureVersion(fv database.FeatureVersion) (id int, err error) {
err = versionfmt.Valid(fv.Feature.Namespace.VersionFormat, fv.Version)
if err != nil {
return 0, cerrors.NewBadRequestError("could not find/insert invalid FeatureVersion")
return 0, commonerr.NewBadRequestError("could not find/insert invalid FeatureVersion")
}

// Do cache lookup.
Expand Down
6 changes: 3 additions & 3 deletions database/pgsql/keyvalue.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015 clair authors
// Copyright 2017 clair authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -18,14 +18,14 @@ import (
"database/sql"
"time"

cerrors "github.com/coreos/clair/utils/errors"
"github.com/coreos/clair/pkg/commonerr"
)

// InsertKeyValue stores (or updates) a single key / value tuple.
func (pgSQL *pgSQL) InsertKeyValue(key, value string) (err error) {
if key == "" || value == "" {
log.Warning("could not insert a flag which has an empty name or value")
return cerrors.NewBadRequestError("could not insert a flag which has an empty name or value")
return commonerr.NewBadRequestError("could not insert a flag which has an empty name or value")
}

defer observeQueryTime("InsertKeyValue", "all", time.Now())
Expand Down
12 changes: 6 additions & 6 deletions database/pgsql/layer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015 clair authors
// Copyright 2017 clair authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -22,8 +22,8 @@ import (
"github.com/guregu/null/zero"

"github.com/coreos/clair/database"
"github.com/coreos/clair/pkg/commonerr"
"github.com/coreos/clair/utils"
cerrors "github.com/coreos/clair/utils/errors"
)

func (pgSQL *pgSQL) FindLayer(name string, withFeatures, withVulnerabilities bool) (database.Layer, error) {
Expand Down Expand Up @@ -247,12 +247,12 @@ func (pgSQL *pgSQL) InsertLayer(layer database.Layer) error {
// Verify parameters
if layer.Name == "" {
log.Warning("could not insert a layer which has an empty Name")
return cerrors.NewBadRequestError("could not insert a layer which has an empty Name")
return commonerr.NewBadRequestError("could not insert a layer which has an empty Name")
}

// Get a potentially existing layer.
existingLayer, err := pgSQL.FindLayer(layer.Name, true, false)
if err != nil && err != cerrors.ErrNotFound {
if err != nil && err != commonerr.ErrNotFound {
return err
} else if err == nil {
if existingLayer.EngineVersion >= layer.EngineVersion {
Expand All @@ -271,7 +271,7 @@ func (pgSQL *pgSQL) InsertLayer(layer database.Layer) error {
if layer.Parent != nil {
if layer.Parent.ID == 0 {
log.Warning("Parent is expected to be retrieved from database when inserting a layer.")
return cerrors.NewBadRequestError("Parent is expected to be retrieved from database when inserting a layer.")
return commonerr.NewBadRequestError("Parent is expected to be retrieved from database when inserting a layer.")
}

parentID = zero.IntFrom(int64(layer.Parent.ID))
Expand Down Expand Up @@ -429,7 +429,7 @@ func (pgSQL *pgSQL) DeleteLayer(name string) error {
}

if affected <= 0 {
return cerrors.ErrNotFound
return commonerr.ErrNotFound
}

return nil
Expand Down
12 changes: 6 additions & 6 deletions database/pgsql/layer_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 clair authors
// Copyright 2017 clair authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -22,7 +22,7 @@ import (

"github.com/coreos/clair/database"
"github.com/coreos/clair/ext/versionfmt/dpkg"
cerrors "github.com/coreos/clair/utils/errors"
"github.com/coreos/clair/pkg/commonerr"
"github.com/coreos/clair/utils/types"
)

Expand Down Expand Up @@ -363,19 +363,19 @@ func testInsertLayerUpdate(t *testing.T, datastore database.Datastore) {

func testInsertLayerDelete(t *testing.T, datastore database.Datastore) {
err := datastore.DeleteLayer("TestInsertLayerX")
assert.Equal(t, cerrors.ErrNotFound, err)
assert.Equal(t, commonerr.ErrNotFound, err)

err = datastore.DeleteLayer("TestInsertLayer3")
assert.Nil(t, err)

_, err = datastore.FindLayer("TestInsertLayer3", false, false)
assert.Equal(t, cerrors.ErrNotFound, err)
assert.Equal(t, commonerr.ErrNotFound, err)

_, err = datastore.FindLayer("TestInsertLayer4a", false, false)
assert.Equal(t, cerrors.ErrNotFound, err)
assert.Equal(t, commonerr.ErrNotFound, err)

_, err = datastore.FindLayer("TestInsertLayer4b", true, false)
assert.Equal(t, cerrors.ErrNotFound, err)
assert.Equal(t, commonerr.ErrNotFound, err)
}

func cmpFV(a, b database.FeatureVersion) bool {
Expand Down
6 changes: 3 additions & 3 deletions database/pgsql/lock.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015 clair authors
// Copyright 2017 clair authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,7 @@ package pgsql
import (
"time"

cerrors "github.com/coreos/clair/utils/errors"
"github.com/coreos/clair/pkg/commonerr"
)

// Lock tries to set a temporary lock in the database.
Expand Down Expand Up @@ -80,7 +80,7 @@ func (pgSQL *pgSQL) Unlock(name, owner string) {
func (pgSQL *pgSQL) FindLock(name string) (string, time.Time, error) {
if name == "" {
log.Warning("could not find an invalid lock")
return "", time.Time{}, cerrors.NewBadRequestError("could not find an invalid lock")
return "", time.Time{}, commonerr.NewBadRequestError("could not find an invalid lock")
}

defer observeQueryTime("FindLock", "all", time.Now())
Expand Down
6 changes: 3 additions & 3 deletions database/pgsql/namespace.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015 clair authors
// Copyright 2017 clair authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -18,12 +18,12 @@ import (
"time"

"github.com/coreos/clair/database"
cerrors "github.com/coreos/clair/utils/errors"
"github.com/coreos/clair/pkg/commonerr"
)

func (pgSQL *pgSQL) insertNamespace(namespace database.Namespace) (int, error) {
if namespace.Name == "" {
return 0, cerrors.NewBadRequestError("could not find/insert invalid Namespace")
return 0, commonerr.NewBadRequestError("could not find/insert invalid Namespace")
}

if pgSQL.cache != nil {
Expand Down
6 changes: 3 additions & 3 deletions database/pgsql/notification.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015 clair authors
// Copyright 2017 clair authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,7 @@ import (
"time"

"github.com/coreos/clair/database"
cerrors "github.com/coreos/clair/utils/errors"
"github.com/coreos/clair/pkg/commonerr"
"github.com/guregu/null/zero"
"github.com/pborman/uuid"
)
Expand Down Expand Up @@ -242,7 +242,7 @@ func (pgSQL *pgSQL) DeleteNotification(name string) error {
}

if affected <= 0 {
return cerrors.ErrNotFound
return commonerr.ErrNotFound
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions database/pgsql/notification_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 clair authors
// Copyright 2017 clair authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -23,7 +23,7 @@ import (
"github.com/coreos/clair/database"
"github.com/coreos/clair/ext/versionfmt"
"github.com/coreos/clair/ext/versionfmt/dpkg"
cerrors "github.com/coreos/clair/utils/errors"
"github.com/coreos/clair/pkg/commonerr"
"github.com/coreos/clair/utils/types"
)

Expand All @@ -37,7 +37,7 @@ func TestNotification(t *testing.T) {

// Try to get a notification when there is none.
_, err = datastore.GetAvailableNotification(time.Second)
assert.Equal(t, cerrors.ErrNotFound, err)
assert.Equal(t, commonerr.ErrNotFound, err)

// Create some data.
f1 := database.Feature{
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestNotification(t *testing.T) {
// Verify the renotify behaviour.
if assert.Nil(t, datastore.SetNotificationNotified(notification.Name)) {
_, err := datastore.GetAvailableNotification(time.Second)
assert.Equal(t, cerrors.ErrNotFound, err)
assert.Equal(t, commonerr.ErrNotFound, err)

time.Sleep(50 * time.Millisecond)
notificationB, err := datastore.GetAvailableNotification(20 * time.Millisecond)
Expand Down Expand Up @@ -164,7 +164,7 @@ func TestNotification(t *testing.T) {
assert.Nil(t, datastore.DeleteNotification(notification.Name))

_, err = datastore.GetAvailableNotification(time.Millisecond)
assert.Equal(t, cerrors.ErrNotFound, err)
assert.Equal(t, commonerr.ErrNotFound, err)
}

// Update a vulnerability and ensure that the old/new vulnerabilities are correct.
Expand Down

0 comments on commit 78cef02

Please sign in to comment.