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

(-)a/Koha/Item.pm (-38 / +2 lines)
Lines 2333-2375 sub add_to_bundle { Link Here
2333
        );
2333
        );
2334
    } catch {
2334
    } catch {
2335
2335
2336
        # FIXME: See if we can move the below copy/paste from Koha::Object::store into it's own class and catch at a lower level in the Schema instantiation, take inspiration from DBIx::Error
2336
        # Use centralized exception translation instead of duplicated code
2337
        if ( ref($_) eq 'DBIx::Class::Exception' ) {
2337
        $schema->translate_exception($_);
2338
            if ( $_->{msg} =~ /Cannot add or update a child row: a foreign key constraint fails/ ) {
2339
2340
                # FK constraints
2341
                # FIXME: MySQL error, if we support more DB engines we should implement this for each
2342
                if ( $_->{msg} =~ /FOREIGN KEY \(`(?<column>.*?)`\)/ ) {
2343
                    Koha::Exceptions::Object::FKConstraint->throw(
2344
                        error     => 'Broken FK constraint',
2345
                        broken_fk => $+{column}
2346
                    );
2347
                }
2348
            } elsif ( $_->{msg} =~ /Duplicate entry '(.*?)' for key '(?<key>.*?)'/ ) {
2349
                Koha::Exceptions::Object::DuplicateID->throw(
2350
                    error        => 'Duplicate ID',
2351
                    duplicate_id => $+{key}
2352
                );
2353
            } elsif ( $_->{msg} =~ /Incorrect (?<type>\w+) value: '(?<value>.*)' for column \W?(?<property>\S+)/ )
2354
            {    # The optional \W in the regex might be a quote or backtick
2355
                my $type     = $+{type};
2356
                my $value    = $+{value};
2357
                my $property = $+{property};
2358
                $property =~ s/['`]//g;
2359
                Koha::Exceptions::Object::BadValue->throw(
2360
                    type     => $type,
2361
                    value    => $value,
2362
                    property => $property =~ /(\w+\.\w+)$/
2363
                    ? $1
2364
                    : $property,    # results in table.column without quotes or backtics
2365
                );
2366
            }
2367
2368
            # Catch-all for foreign key breakages. It will help find other use cases
2369
            $_->rethrow();
2370
        } else {
2371
            $_->rethrow();
2372
        }
2373
    };
2338
    };
2374
}
2339
}
2375
2340
2376
- 

Return to bug 19871