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

(-)a/Koha/Exceptions/Object.pm (+5 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::FKConstraint' => {
11
        isa         => 'Koha::Exceptions::Object',
12
        description => "Foreign key constraint broken",
13
        fields      =>  ['broken_fk']
14
    },
10
    'Koha::Exceptions::Object::MethodNotFound' => {
15
    'Koha::Exceptions::Object::MethodNotFound' => {
11
        isa => 'Koha::Exceptions::Object',
16
        isa => 'Koha::Exceptions::Object',
12
        description => "Invalid method",
17
        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
                else {
139
                    # Catch-all for foreign key breakages. It will help find other use cases
140
                    $->rethrow();
141
                }
142
            }
143
            else {
144
                # TODO: find other exceptions we could handle
145
                $->rethrow();
146
            }
147
        }
148
    }
123
}
149
}
124
150
125
=head3 $object->delete();
151
=head3 $object->delete();
126
- 

Return to bug 19828