Coder Social home page Coder Social logo

go-dberror's Introduction

Readable database errors

Do you like forcing constraint validation down into the database, but dislike the native error messages returned by Postgres?

invalid input syntax for uuid: "foo"

invalid input value for enum account_status: "blah"

new row for relation "accounts" violates check constraint "accounts_balance_check"

null value in column "id" violates not-null constraint

This library attempts to parse those error messages and return error messages that you can expose via your API.

No id was provided. Please provide a id

Cannot write a negative balance

Invalid input syntax for type uuid: "foo"

Invalid account_status: "blah"

Can't save to payments because the account_id (91f47e99-d616-4d8c-9c02-cbd13bceac60) isn't present in the accounts table

A email already exists with this value ([email protected])

In addition, this library exports common Postgres error codes, so you can check against them in your application.

Basic Usage

import dberror "github.com/Shyp/go-dberror"

func main() {
	_, err := db.Exec("INSERT INTO accounts (id) VALUES (null)")
	dberr := dberror.GetError(err)
	switch e := dberr.(type) {
	case *dberror.Error:
		fmt.Println(e.Error()) // "No id was provided. Please provide a id"
	default:
		// not a pq error
}

Database Constraints

Failed check constraints are tricky - the native error messages just say "failed", and don't reference a column.

So you can define your own constraint handlers, and then register them:

import dberror "github.com/Shyp/go-dberror"
import "github.com/lib/pq"

func init()
	constraint := &dberror.Constraint{
		Name: "accounts_balance_check",
		GetError: func(e *pq.Error) *dberror.Error {
			return &dberror.Error{
				Message:  "Cannot write a negative balance",
				Severity: e.Severity,
				Table:    e.Table,
				Detail:   e.Detail,
				Code:     string(e.Code),
			}
		},
	}
	dberror.RegisterConstraint(constraint)

	// test.AssertEquals(t, e.Error(), "Cannot write a negative balance")
}

If we get a constraint failure, we'll call your GetError handler, to get a well-formatted message.

go-dberror's People

Contributors

fzerorubigd avatar kevinburke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-dberror's Issues

Please add MIT License

Hi,
I wrote this project while I was at Shyp and intended for it to be used by others. Apparently I forgot to put a LICENSE file on it. This means that it's not usable by anyone.

Could you put a LICENSE file on it?

Thanks,
Kevin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.