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

(-)a/Koha/Object.pm (+43 lines)
Lines 28-33 use Try::Tiny; Link Here
28
use Koha::Database;
28
use Koha::Database;
29
use Koha::Exceptions::Object;
29
use Koha::Exceptions::Object;
30
use Koha::DateUtils;
30
use Koha::DateUtils;
31
use Koha::Object::Error;
31
32
32
=head1 NAME
33
=head1 NAME
33
34
Lines 77-82 sub new { Link Here
77
          $schema->resultset( $class->_type() )->new($attributes);
78
          $schema->resultset( $class->_type() )->new($attributes);
78
    }
79
    }
79
80
81
    $self->{_errors} = [];
82
80
    croak("No _type found! Koha::Object must be subclassed!")
83
    croak("No _type found! Koha::Object must be subclassed!")
81
      unless $class->_type();
84
      unless $class->_type();
82
85
Lines 332-337 sub get_from_storage { Link Here
332
    return $object_class->_new_from_dbic($stored_object);
335
    return $object_class->_new_from_dbic($stored_object);
333
}
336
}
334
337
338
=head3 $object->errors
339
340
    my @errors = @{ $object->errors };
341
342
Returns the (probably non-fatal) errors that were recorded on the object.
343
344
=cut
345
346
sub errors {
347
    my ( $self ) = @_;
348
    return $self->{_errors};
349
}
350
351
=head3 $object->add_error
352
353
    try {
354
        <some action that might fail>
355
    }
356
    catch {
357
        if ( <fatal condition> ) {
358
            Koha::Exception->throw...
359
        }
360
361
        # This is a non fatal error, notify the caller
362
        $self->add_error({ error => $error });
363
    }
364
    return $self;
365
366
Adds an error.
367
368
=cut
369
370
sub add_error {
371
    my ( $self, $params ) = @_;
372
373
    push @{ $self->{_errors} }, Koha::Object::Error->new($params);
374
375
    return $self;
376
}
377
335
=head3 $object->TO_JSON
378
=head3 $object->TO_JSON
336
379
337
Returns an unblessed representation of the object, suitable for JSON output.
380
Returns an unblessed representation of the object, suitable for JSON output.
(-)a/t/db_dependent/Koha/Object.t (-2 / +23 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 20;
20
use Test::More tests => 21;
21
use Test::Exception;
21
use Test::Exception;
22
use Test::Warn;
22
use Test::Warn;
23
use DateTime;
23
use DateTime;
Lines 867-869 subtest 'set_or_blank' => sub { Link Here
867
867
868
    $schema->storage->txn_rollback;
868
    $schema->storage->txn_rollback;
869
};
869
};
870
- 
870
871
subtest 'errors() and add_error() tests' => sub {
872
873
    plan tests => 6;
874
875
    my $patron = Koha::Patron->new;
876
877
    my @errors = @{ $patron->errors };
878
    is( scalar @errors, 0, 'No errors' );
879
880
    $patron->add_error({ error => "error_1" });
881
    $patron->add_error({ error => "error_2" });
882
883
884
    @errors = @{ $patron->errors };
885
886
    is( scalar @errors, 2, 'Errors are returned' );
887
    is( ref($errors[0]), 'Koha::Object::Error', 'Right type returned' );
888
    is( ref($errors[1]), 'Koha::Object::Error', 'Right type returned' );
889
    is( $errors[0]->error, 'error_1', 'Right error recorded' );
890
    is( $errors[1]->error, 'error_2', 'Right error recorded' );
891
};

Return to bug 26555