database: Add StorageError type

This commit is contained in:
Sida Chen 2019-02-19 16:41:06 -05:00
parent 073c685c5b
commit 5fa1ac89b9
2 changed files with 39 additions and 5 deletions

View File

@ -17,7 +17,6 @@
package database package database
import ( import (
"errors"
"fmt" "fmt"
"time" "time"
@ -27,20 +26,20 @@ import (
var ( var (
// ErrBackendException is an error that occurs when the database backend // ErrBackendException is an error that occurs when the database backend
// does not work properly (ie. unreachable). // 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 // 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 // fails (i.e. when an entity which is supposed to be unique is detected
// twice) // twice)
ErrInconsistent = errors.New("database: inconsistent database") ErrInconsistent = NewStorageError("inconsistent database")
// ErrInvalidParameters is an error that occurs when the parameters are not valid. // 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 // ErrMissingEntities is an error that occurs when an associated immutable
// entity doesn't exist in the database. This error can indicate a wrong // entity doesn't exist in the database. This error can indicate a wrong
// implementation or corrupted database. // 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 // RegistrableComponentConfig is a configuration block that can be used to

35
database/error.go Normal file
View File

@ -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}
}