Bugzilla – Attachment 70098 Details for
Bug 19828
Koha::Object->store should catch DBIC exceptions and raise Koha::Exceptions
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 19828: Make Koha::Object->store translate DBIC exceptions into Koha exceptions
Bug-19828-Make-KohaObject-store-translate-DBIC-exc.patch (text/plain), 3.23 KB, created by
Josef Moravec
on 2017-12-22 07:46:36 UTC
(
hide
)
Description:
Bug 19828: Make Koha::Object->store translate DBIC exceptions into Koha exceptions
Filename:
MIME Type:
Creator:
Josef Moravec
Created:
2017-12-22 07:46:36 UTC
Size:
3.23 KB
patch
obsolete
>From 031860f9bd0bca2b980c6b8b20af338853e6e508 Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Mon, 18 Dec 2017 13:48:56 -0300 >Subject: [PATCH] Bug 19828: Make Koha::Object->store translate DBIC exceptions > into Koha exceptions > >This patch introduces a try/catch block in store() and parses the error >when an exceptional situation takes place. > >It only deals with FK constraint violations and duplicate IDs. The rest of the DBIC exceptions >are rethrown. > >To test: > >- Apply this patch >- Run: > $ kshell > k$ prove t/db_dependent/Koha/Object.t >=> SUCCESS: Tests pass! >- Sign off :-D > >Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> > >Signed-off-by: Josef Moravec <josef.moravec@gmail.com> >--- > Koha/Exceptions/Object.pm | 10 ++++++++++ > Koha/Object.pm | 28 +++++++++++++++++++++++++++- > 2 files changed, 37 insertions(+), 1 deletion(-) > >diff --git a/Koha/Exceptions/Object.pm b/Koha/Exceptions/Object.pm >index 2ffa1ec..4cad568 100644 >--- a/Koha/Exceptions/Object.pm >+++ b/Koha/Exceptions/Object.pm >@@ -7,6 +7,16 @@ use Exception::Class ( > 'Koha::Exceptions::Object' => { > description => 'Something went wrong!', > }, >+ 'Koha::Exceptions::Object::DuplicateID' => { >+ isa => 'Koha::Exceptions::Object', >+ description => "Duplicate ID passed", >+ fields => ['duplicate_id'] >+ }, >+ 'Koha::Exceptions::Object::FKConstraint' => { >+ isa => 'Koha::Exceptions::Object', >+ description => "Foreign key constraint broken", >+ fields => ['broken_fk'] >+ }, > 'Koha::Exceptions::Object::MethodNotFound' => { > isa => 'Koha::Exceptions::Object', > description => "Invalid method", >diff --git a/Koha/Object.pm b/Koha/Object.pm >index 0956cca..17609f2 100644 >--- a/Koha/Object.pm >+++ b/Koha/Object.pm >@@ -22,6 +22,7 @@ use Modern::Perl; > > use Carp; > use Mojo::JSON; >+use Try::Tiny; > > use Koha::Database; > use Koha::Exceptions::Object; >@@ -119,7 +120,32 @@ Returns: > sub store { > my ($self) = @_; > >- return $self->_result()->update_or_insert() ? $self : undef; >+ try { >+ return $self->_result()->update_or_insert() ? $self : undef; >+ } >+ catch { >+ # Catch problems and raise relevant exceptions >+ if (ref($_) eq 'DBIx::Class::Exception') { >+ if ( $_->{msg} =~ /Cannot add or update a child row: a foreign key constraint fails/ ) { >+ # FK constraints >+ # FIXME: MySQL error, if we support more DB engines we should implement this for each >+ if ( $_->{msg} =~ /FOREIGN KEY \(`(?<column>.*?)`\)/ ) { >+ Koha::Exceptions::Object::FKConstraint->throw( >+ error => 'Broken FK constraint', >+ broken_fk => $+{column} >+ ); >+ } >+ } >+ elsif( $_->{msg} =~ /Duplicate entry '(.*?)' for key '(?<key>.*?)'/ ) { >+ Koha::Exceptions::Object::DuplicateID->throw( >+ error => 'Duplicate ID', >+ duplicate_id => $+{key} >+ ); >+ } >+ # Catch-all for foreign key breakages. It will help find other use cases >+ $->rethrow(); >+ } >+ } > } > > =head3 $object->delete(); >-- >2.1.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 19828
:
69857
|
69858
|
69907
|
69908
|
69963
|
69964
|
69965
|
70048
|
70049
|
70050
|
70097
| 70098 |
70099
|
70100