All Projects → Shyp → Go Dberror

Shyp / Go Dberror

parsing postgres errors

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Go Dberror

Rein
Database constraints made easy for ActiveRecord.
Stars: ✭ 657 (+742.31%)
Mutual labels:  database, postgres, constraints
Reactive record
Generate ActiveRecord models for a pre-existing Postgres db
Stars: ✭ 132 (+69.23%)
Mutual labels:  database, postgres, constraints
Plv8
V8 Engine Javascript Procedural Language add-on for PostgreSQL
Stars: ✭ 1,195 (+1432.05%)
Mutual labels:  database, postgres
Entityframeworkcore.bootkit
EntityFrameworkCore Start Kit
Stars: ✭ 29 (-62.82%)
Mutual labels:  database, postgres
Cosyan
Transactional SQL based RDBMS with sophisticated multi table constraint logic.
Stars: ✭ 45 (-42.31%)
Mutual labels:  database, constraints
Node Pg Migrate
Node.js database migration management for Postgresql
Stars: ✭ 838 (+974.36%)
Mutual labels:  database, postgres
Go Kallax
Kallax is a PostgreSQL typesafe ORM for the Go language.
Stars: ✭ 853 (+993.59%)
Mutual labels:  database, postgres
Niklick
Rails Versioned API solution template for hipsters! (Ruby, Ruby on Rails, REST API, GraphQL, Docker, RSpec, Devise, Postgress DB)
Stars: ✭ 39 (-50%)
Mutual labels:  database, postgres
Metabase
The simplest, fastest way to get business intelligence and analytics to everyone in your company 😋
Stars: ✭ 26,803 (+34262.82%)
Mutual labels:  database, postgres
Docker Backup Database
Docker image to periodically backup your database (MySQL, Postgres, or MongoDB) to S3 or local disk.
Stars: ✭ 57 (-26.92%)
Mutual labels:  database, postgres
East
node.js database migration tool
Stars: ✭ 53 (-32.05%)
Mutual labels:  database, postgres
Squid
🦑 Provides SQL tagged template strings and schema definition functions.
Stars: ✭ 57 (-26.92%)
Mutual labels:  database, postgres
Docker Postgres
A docker container running PostgreSQL
Stars: ✭ 22 (-71.79%)
Mutual labels:  database, postgres
Efcore.pg
Entity Framework Core provider for PostgreSQL
Stars: ✭ 838 (+974.36%)
Mutual labels:  database, postgres
Awesome Postgres
A curated list of awesome PostgreSQL software, libraries, tools and resources, inspired by awesome-mysql
Stars: ✭ 7,468 (+9474.36%)
Mutual labels:  database, postgres
Migrate
Database migrations. CLI and Golang library.
Stars: ✭ 7,712 (+9787.18%)
Mutual labels:  database, postgres
Goqu
SQL builder and query library for golang
Stars: ✭ 984 (+1161.54%)
Mutual labels:  database, postgres
Skunk
A data access library for Scala + Postgres.
Stars: ✭ 1,107 (+1319.23%)
Mutual labels:  database, postgres
Blog
Everything about database,business.(Most for PostgreSQL).
Stars: ✭ 6,330 (+8015.38%)
Mutual labels:  database, postgres
Postgresclientkit
A PostgreSQL client library for Swift. Does not require libpq.
Stars: ✭ 49 (-37.18%)
Mutual labels:  database, postgres

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].