Socket
Socket
Sign inDemoInstall

github.com/kucjac/uni-db

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/kucjac/uni-db


Version published
Created
Source

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.

FAQs

Package last updated on 30 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc