From 5fa1ac89b9946f2e32ac666080b4f78ad1f9bbfa Mon Sep 17 00:00:00 2001 From: Sida Chen Date: Tue, 19 Feb 2019 16:41:06 -0500 Subject: [PATCH] database: Add StorageError type --- database/database.go | 9 ++++----- database/error.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 database/error.go diff --git a/database/database.go b/database/database.go index f760061f..0c479575 100644 --- a/database/database.go +++ b/database/database.go @@ -17,7 +17,6 @@ package database import ( - "errors" "fmt" "time" @@ -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 diff --git a/database/error.go b/database/error.go new file mode 100644 index 00000000..bfbe3af6 --- /dev/null +++ b/database/error.go @@ -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} +}