Git Product home page Git Product logo

dbix-class-parseerror's Introduction

NAME

DBIx::Class::ParseError - Extensible database error handler

SYNOPSIS

From:

DBIx::Class::Storage::DBI::_dbh_execute(): DBI Exception: DBD::mysql::st execute failed: Duplicate entry \'1\' for key \'PRIMARY\' [for Statement "INSERT INTO foo ( bar_id, id, is_foo, name) VALUES ( ?, ?, ?, ? )" with ParamValues: 0=1, 1=1, 2=1, 3=\'Foo1571434801\'] at ...

To:

use Data::Dumper;
my $parser = DBIx::Class::ParseError->new(schema => $dbic_schema);
print Dumper( $parser->process($error) );

# bless({
#    'table' => 'foo',
#    'columns' => [
#        'id'
#    ],
#    'message' => 'DBIx::Class::Storage::DBI::_dbh_execute(): DBI Exception: DBD::mysql::st execute failed: Duplicate entry \'1\' for key \'PRIMARY\' [for Statement "INSERT INTO foo ( bar_id, id, is_foo, name) VALUES ( ?, ?, ?, ? )" with ParamValues: 0=1, 1=1, 2=1, 3=\'Foo1571434801\'] at ...',
#    'operation' => 'insert',
#    'column_data' => {
#        'name' => 'Foo1571434801',
#        'bar_id' => '1',
#        'id' => '1',
#        'is_foo' => '1'
#    },
#    'source_name' => 'Foo',
#    'type' => 'primary_key'
# }, 'DBIx::Class::ParseError::Error' );

DESCRIPTION

This a tool to extend DB errors from DBIx::Class (basically, database error strings wrapped into a DBIx::Class::Exception obj) into an API to provide useful details of the error, allowing app's business layer or helper scripts interfacing with database models to instrospect and better handle errors from multiple DBMS.

ERROR CASES

This is a non-exausted list of common errors which should be handled by this tool:

  • primary key
  • foreign key(s)
  • unique key(s)
  • not null column(s)
  • data type
  • missing column
  • missing table

CUSTOM ERRORS

You may find your code throwing exceptions that you would like to generate custom errors for. You can specify them in the constructor:

my $parser = DBIx::Class::ParseError->new(
    schema => $dbic_schema,
    custom_errors => {
        locking_failed => qr/Could not update due to version mismatch/i
    }
);

The custom_errors key must point to a hash references whose values are regular expressions to match against the error. Due to the unpredictable nature of these errors, the exception will like not have additional information beyond the error message and the error message.

The parser will attempt to match custom errors before standard errors. Any error will have the string custom_ prepended, so the above error will be reported as custom_locking_failed.

DRIVERS

Initial fully support for errors from the following DBMS:

AUTHOR

wreis - Wallace reis [email protected]

CONTRIBUTORS

Ovid - Curtis "Ovid" Poe [email protected]

COPYRIGHT

Copyright (c) the "AUTHOR".

LICENSE

This library is free software and may be distributed under the same terms as perl itself.

dbix-class-parseerror's People

Contributors

ovid avatar wreis avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

ovid clayne pplu

dbix-class-parseerror's Issues

Can't parse errors when using DBIx::Class quote_char

When using Schema->connect($dsn, $user, $pass, { quote_char => '`' }
(https://metacpan.org/pod/DBIx::Class::Storage::DBI#quote_char), DBIx::Class::ParseError is not capable of parsing the exception:

For a duplicate entry I get:

Parsing error string failed at local/lib/perl5/DBIx/Class/ParseError/Parser.pm line 251.
DBIx::Class::Storage::DBI::_dbh_execute(): DBI Exception: DBD::mysql::st execute failed: Duplicate entry 'table' for key 'name' [for Statement "INSERT INTO `table` ( `name`) VALUES ( ? )" with ParamValues: 0='value1'] at lib/Module.pm line 91

When I connect without quote_char, DBIx::Class::ParseError can parse the exceptions.

ParseError dies when it can't parse an error

Hi,

I was wondering what you would think about changing a bit the behavior of this module. When the error from DBI cannot be parsed, it throws an exception. https://github.com/wreis/DBIx-Class-ParseError/blob/master/lib/DBIx/Class/ParseError/Parser.pm#L250.

Since the module is already used in exception handlers, It would be nice that it returned an exception object even if it couldn't parse the message, making the exception handling code simpler.

Would you be interested in a patch in this direction?

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.