dberrors

package
v0.0.0-...-3215105 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2018 License: MIT Imports: 2 Imported by: 0

README

dberrors

Package dberrors contains unified database Errors converted from third-party package errors.

In order to create the RESTful API that is indepenedent of the database type, the database errors must be converted into single form. This package defines database Errors with some Prototypes that are the most common error categories.

// Unified database error
type Error struct {
	ID 	uint
	Title 	string
	Message string
}

// database error prototypes
var (
	...
	ErrIntegrityConstViolation = Error{
		ID: 	6, 
		Title: 	"Integrity constraint violation",
	}
	...
)

In order to maintaing uniform form of the error converting, every database driver should implement the 'Converter' interface with it's custom error converter.

// Converter interface
type Converter interface {
	Convert(err error) *dberrors.Error
}

...

type CustomConverter struct {
	// Contains some kind of error mapper 
	// from custom errors into dberrors.ErrPrototype
}

func (c *CustomConverter) Convert(err error) *dberrors.Error {
	// get dberrors.ErrPrototype from the mapping and
	// create new *dberrors.Error on it's base
	customErr := err.(CustomErrorType)
	proto, ok := mapping[customErr]
	if !ok {
		return dberrors.ErrUnspecifiedError.New()
	}
	return proto.New()
}
...

Good examples on how to write error converters are the converters defined in the mysqlconv, pgconv, sqliteconv or gormconv packages.

Documentation

Overview

Package dberrors contains unified database Errors converted from third-party package errors.

In order to create the RESTful API that is indepenedent of the database type, the database errors must be converted into single form. This package defines database Errors with some Prototypes that are the most common error categories.

In order to maintaing uniform form of the error converting, every database driver should implement the 'Converter' interface.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Warnings
	ErrWarning = Error{ID: 1, Title: "Warning"}

	// ErrNoResult used as a replacement for ErrNoRows - for non-sql databases
	ErrNoResult = Error{ID: 2, Title: "No Result"}

	// Connection Exception
	ErrConnExc = Error{ID: 3, Title: "Connection exception"}

	ErrCardinalityViolation = Error{ID: 4, Title: "Cardinality violation"}

	// Data Exception
	ErrDataException = Error{ID: 5, Title: "Data Exception"}

	// Integrity Violation
	ErrIntegrConstViolation = Error{ID: 6, Title: "Integrity constraint violation"}
	ErrRestrictViolation    = Error{ID: 7, Title: "Restrict violation"}
	ErrNotNullViolation     = Error{ID: 8, Title: "Not null violation"}
	ErrForeignKeyViolation  = Error{ID: 9, Title: "Foreign-Key violation"}
	ErrUniqueViolation      = Error{ID: 10, Title: "Unique violation"}
	ErrCheckViolation       = Error{ID: 11, Title: "Check violation"}

	// Transactions
	ErrInvalidTransState = Error{ID: 12, Title: "Invalid transaction state"}
	ErrInvalidTransTerm  = Error{ID: 13, Title: "Invalid transaction termination"}
	ErrTransRollback     = Error{ID: 14, Title: "Transaction Rollback"}

	// TxDone is an equivalent of sql.ErrTxDone error from sql package
	ErrTxDone = Error{ID: 15, Title: "Transaction done"}

	// Invalid Authorization
	ErrInvalidAuthorization = Error{ID: 16, Title: "Invalid Authorization Specification"}
	ErrInvalidPassword      = Error{ID: 17, Title: "Invalid password"}

	// Invalid Schema Name
	ErrInvalidSchemaName = Error{ID: 18, Title: "Invalid Schema Name"}

	// Invalid Catalog Name
	ErrInvalidCatalogName = Error{ID: 19, Title: "Invalid Catalog Name"}

	// Syntax Error
	ErrInvalidSyntax         = Error{ID: 20, Title: "Syntax Error"}
	ErrInsufficientPrivilege = Error{ID: 21, Title: "Insufficient Privilege"}

	// Insufficient Resources
	ErrInsufficientResources = Error{ID: 22, Title: "Insufficient Resources"}

	// Program Limit Exceeded
	ErrProgramLimitExceeded = Error{ID: 23, Title: "Program Limit Exceeded"}

	// System Error
	ErrSystemError = Error{ID: 24, Title: "System error"}

	// Internal Error
	ErrInternalError = Error{ID: 25, Title: "Internal error"}

	// Unspecified Error - all other errors not included in this division
	ErrUnspecifiedError = Error{ID: 26, Title: "Unspecified error"}
)

Functions

This section is empty.

Types

type Converter

type Converter interface {
	Convert(err error) *Error
}

Converter is an interface that defines the form of converting third-party database errors into uniform dberrors.Error. The returned errors should be based on the prototypes provided in this package.

type Error

type Error struct {
	ID      uint
	Title   string
	Message string
}

Error is a unified Database Error.

This package contain error prototypes with name starting with Err... On their base recogniser should create new errors. In order to compare the error entity with prototype use the 'Compare' method.

func (*Error) Compare

func (d *Error) Compare(err Error) bool

Compare - checks if the error is of the same type as given in the argument

Error variables given in the package doesn't have details. Every *Error has its own Message. By comparing the error with Variables of type Error in the package the result will always be false This method allows to check if the error has the same ID as the error provided as an argument

func (*Error) Error

func (d *Error) Error() string

Error implements error interface

func (*Error) GetPrototype

func (d *Error) GetPrototype() (proto Error, err error)

GetPrototype returns the Error prototype on which the given database *Error entity was built.

func (Error) New

func (d Error) New() *Error

New creates a copy of the given *Error Only ID and Title fields are copied to new Error the Message should be unique for given situation. Used on prototypes to create Prototype based Errors

func (Error) NewWithError

func (d Error) NewWithError(err error) (dbError *Error)

NewWithError creates new Error copy based on the Error with a message. The message is an Error value from 'err' argument. Used on prototypes to create new prototype based Error containing a situation specific message based on provided error.

func (Error) NewWithMessage

func (d Error) NewWithMessage(message string) (err *Error)

NewWithMessage creates new *Error copy of the Error with additional message. Used on prototypes to create new prototype based Error containing a situation specific message based on the given argument.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL