Skip to content

Commit

Permalink
database: Add StorageError type
Browse files Browse the repository at this point in the history
  • Loading branch information
KeyboardNerd committed Feb 19, 2019
1 parent 073c685 commit 5fa1ac8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
9 changes: 4 additions & 5 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package database

import (
"errors"
"fmt"
"time"

Expand All @@ -27,20 +26,20 @@ import (
var (
// ErrBackendException is an error that occurs when the database backend
// does not work properly (ie. unreachable).
ErrBackendException = errors.New("database: an error occurred when querying the backend")
ErrBackendException = NewStorageError("an error occurred when querying the backend")

// ErrInconsistent is an error that occurs when a database consistency check
// fails (i.e. when an entity which is supposed to be unique is detected
// twice)
ErrInconsistent = errors.New("database: inconsistent database")
ErrInconsistent = NewStorageError("inconsistent database")

// ErrInvalidParameters is an error that occurs when the parameters are not valid.
ErrInvalidParameters = errors.New("database: parameters are not valid")
ErrInvalidParameters = NewStorageError("parameters are not valid")

// ErrMissingEntities is an error that occurs when an associated immutable
// entity doesn't exist in the database. This error can indicate a wrong
// implementation or corrupted database.
ErrMissingEntities = errors.New("database: associated immutable entities are missing in the database")
ErrMissingEntities = NewStorageError("associated immutable entities are missing in the database")
)

// RegistrableComponentConfig is a configuration block that can be used to
Expand Down
35 changes: 35 additions & 0 deletions database/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2019 clair authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package database

// StorageError is database error
type StorageError struct {
reason string
original error
}

func (e *StorageError) Error() string {
return e.reason
}

// NewStorageErrorWithInternalError creates a new database error
func NewStorageErrorWithInternalError(reason string, originalError error) *StorageError {
return &StorageError{reason, originalError}
}

// NewStorageError creates a new database error
func NewStorageError(reason string) *StorageError {
return &StorageError{reason, nil}
}

0 comments on commit 5fa1ac8

Please sign in to comment.