Git Product home page Git Product logo

mysql-error-numbers's Introduction

MySQL Error Numbers

Build Status

This is a generated package defining all MySQL errors from the documentation as constants. The reasone for this is to make the code more readable and potentially help describe the errors.

The name and number are fetched from version 8.0 of the MySQL documentation.

Motivation

I had a hard time finding a good way to handle errors and often used a loca map with numbers to internal error types. I also found github.com/VividCortex/mysqlerr which has the error number constants but is not generated (and not maintained?). I ended up doing this package and would love feedback and PRs to make it a re-usable way to handle MySQL errors with Go.

Usage

package main

import (
    "database/sql"
    _ "github.com/go-sql-driver/mysql"

    "github.com/bombsimon/mysql-error-numbers"
)

func main() {
    db, _ := sql.Open("mysql", "user:password@/dbname")

    // Pass the error itself.
    if _, err := db.Query("INSERT INTO... <BAD STATEMENT>"); err != nil {
        if mysqlerrnum.FromError(err) == mysqlerrnum.ErrDupEntry {
            panic("You fool, duplicate!")
        }
    }

    // Or do the conversion.
    _, err := db.Query("INSERT INTO... <BAD STATEMENT>")
    if e, ok := err.(*MySQLError); ok {
        if mysqlerrnum.FromNumber(e.Number) != mysqlerrnum.ErrNoReferencedRow {
            panic("Oops, expected ErNoReferencedRow")
        }
    }
}

mysql-error-numbers's People

Contributors

bombsimon avatar

Watchers

James Cloos avatar  avatar

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.