View | Details | Raw Unified | Return to bug 19828
Collapse All | Expand All

(-)a/Koha/Exceptions/Object.pm (+10 lines)
Lines 7-12 use Exception::Class ( Link Here
7
    'Koha::Exceptions::Object' => {
7
    'Koha::Exceptions::Object' => {
8
        description => 'Something went wrong!',
8
        description => 'Something went wrong!',
9
    },
9
    },
10
    'Koha::Exceptions::Object::DuplicateID' => {
11
        isa         => 'Koha::Exceptions::Object',
12
        description => "Duplicate ID passed",
13
        fields      =>  ['duplicate_id']
14
    },
15
    'Koha::Exceptions::Object::FKConstraint' => {
16
        isa         => 'Koha::Exceptions::Object',
17
        description => "Foreign key constraint broken",
18
        fields      =>  ['broken_fk']
19
    },
10
    'Koha::Exceptions::Object::MethodNotFound' => {
20
    'Koha::Exceptions::Object::MethodNotFound' => {
11
        isa => 'Koha::Exceptions::Object',
21
        isa => 'Koha::Exceptions::Object',
12
        description => "Invalid method",
22
        description => "Invalid method",
(-)a/Koha/Object.pm (-2 / +27 lines)
Lines 22-27 use Modern::Perl; Link Here
22
22
23
use Carp;
23
use Carp;
24
use Mojo::JSON;
24
use Mojo::JSON;
25
use Try::Tiny;
25
26
26
use Koha::Database;
27
use Koha::Database;
27
use Koha::Exceptions::Object;
28
use Koha::Exceptions::Object;
Lines 119-125 Returns: Link Here
119
sub store {
120
sub store {
120
    my ($self) = @_;
121
    my ($self) = @_;
121
122
122
    return $self->_result()->update_or_insert() ? $self : undef;
123
    try {
124
        return $self->_result()->update_or_insert() ? $self : undef;
125
    }
126
    catch {
127
        # Catch problems and raise relevant exceptions
128
        if (ref($_) eq 'DBIx::Class::Exception') {
129
            if ( $_->{msg} =~ /Cannot add or update a child row: a foreign key constraint fails/ ) {
130
                # FK constraints
131
                # FIXME: MySQL error, if we support more DB engines we should implement this for each
132
                if ( $_->{msg} =~ /FOREIGN KEY \(`(?<column>.*?)`\)/ ) {
133
                    Koha::Exceptions::Object::FKConstraint->throw(
134
                        error     => 'Broken FK constraint',
135
                        broken_fk => $+{column}
136
                    );
137
                }
138
            }
139
            elsif( $_->{msg} =~ /Duplicate entry '(.*?)' for key '(?<key>.*?)'/ ) {
140
                Koha::Exceptions::Object::DuplicateID->throw(
141
                    error => 'Duplicate ID',
142
                    duplicate_id => $+{key}
143
                );
144
            }
145
            # Catch-all for foreign key breakages. It will help find other use cases
146
            $->rethrow();
147
        }
148
    }
123
}
149
}
124
150
125
=head3 $object->delete();
151
=head3 $object->delete();
126
- 

Return to bug 19828