Skip to content

neuronlabs/uni-db

Repository files navigation

unidb

Package unidb 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) *unidb.Error
}

...

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

func (c *CustomConverter) Convert(err error) *unidb.Error {
	// get unidb.ErrPrototype from the mapping and
	// create new *unidb.Error on it's base
	customErr := err.(CustomErrorType)
	proto, ok := mapping[customErr]
	if !ok {
		return unidb.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.