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

(-)a/Koha/Exceptions/Patron/Attribute.pm (-4 / +57 lines)
Lines 8-20 use Exception::Class ( Link Here
8
        description => 'Something went wrong'
8
        description => 'Something went wrong'
9
    },
9
    },
10
    'Koha::Exceptions::Patron::Attribute::NonRepeatable' => {
10
    'Koha::Exceptions::Patron::Attribute::NonRepeatable' => {
11
        isa => 'Koha::Exceptions::Patron::Attribute',
11
        isa         => 'Koha::Exceptions::Patron::Attribute',
12
        description => "repeatable not set for attribute type and tried to add a new attribute for the same code"
12
        description => "repeatable not set for attribute type and tried to add a new attribute for the same code",
13
        fields      => [ "attribute" ]
13
    },
14
    },
14
    'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint' => {
15
    'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint' => {
15
        isa => 'Koha::Exceptions::Patron::Attribute',
16
        isa         => 'Koha::Exceptions::Patron::Attribute',
16
        description => "unique_id set for attribute type and tried to add a new with the same code and value"
17
        description => "unique_id set for attribute type and tried to add a new with the same code and value",
18
        fields      => [ "attribute" ]
17
    }
19
    }
18
);
20
);
19
21
22
sub full_message {
23
    my $self = shift;
24
25
    my $msg = $self->message;
26
27
    unless ( $msg) {
28
        if ( $self->isa('Koha::Exceptions::Patron::Attribute::NonRepeatable') ) {
29
            $msg = sprintf(
30
                "Tried to add more than one non-repeatable attributes. code=%s attribute=%s",
31
                $self->attribute->code,
32
                $self->attribute->attribute
33
            );
34
        }
35
        elsif ( $self->isa('Koha::Exceptions::Patron::Attribute::UniqueIDConstraint') ) {
36
            $msg = sprintf(
37
                "Your action breaks a unique constraint on the attribute. code=%s attribute=%s",
38
                $self->attribute->code,
39
                $self->attribute->attribute
40
            );
41
        }
42
    }
43
44
    return $msg;
45
}
46
47
=head1 NAME
48
49
Koha::Exceptions::Patron::Attribute - Base class for patron attribute exceptions
50
51
=head1 Exceptions
52
53
=head2 Koha::Exceptions::Patron::Attribute
54
55
Generic patron attribute exception
56
57
=head2 Koha::Exceptions::Patron::Attribute::NonRepeatable
58
59
Exception to be used trying to add more than one non-repeatable attribute.
60
61
=head2 Koha::Exceptions::Patron::Attribute::UniqueIDConstraint
62
63
Exception to be used when trying to add an attribute that breaks its type unique constraint.
64
65
=head1 Class methods
66
67
=head2 full_message
68
69
Overloaded method for exception stringifying.
70
71
=cut
72
20
1;
73
1;
(-)a/t/Koha/Exceptions.t (-2 / +30 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 6;
20
use Test::More tests => 7;
21
use Test::MockObject;
21
use Test::MockObject;
22
use Test::Exception;
22
use Test::Exception;
23
23
Lines 197-199 subtest 'Koha::Exceptions::Object::NotInstantiated tests' => sub { Link Here
197
        'Exception is thrown :-D';
197
        'Exception is thrown :-D';
198
    is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
198
    is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
199
};
199
};
200
- 
200
201
subtest 'Koha::Exceptions::Patron::Attribute::* tests' => sub {
202
203
    plan tests => 5;
204
205
    use_ok("Koha::Exceptions::Patron::Attribute");
206
207
    my $code      = 'CODE';
208
    my $attribute = 'ATTRIBUTE';
209
210
    my $mocked_attribute = Test::MockObject->new();
211
    $mocked_attribute->mock('code', sub { return $code } );
212
    $mocked_attribute->mock('attribute', sub { return $attribute } );
213
214
    throws_ok
215
        { Koha::Exceptions::Patron::Attribute::NonRepeatable->throw(
216
            attribute => $mocked_attribute ); }
217
        'Koha::Exceptions::Patron::Attribute::NonRepeatable',
218
        'Exception is thrown :-D';
219
220
    # stringify the exception
221
    is( "$@", "Tried to add more than one non-repeatable attributes. code=$code attribute=$attribute", 'Exception stringified correctly' );
222
223
    throws_ok
224
        { Koha::Exceptions::Patron::Attribute::NonRepeatable->throw( "Manual message exception" ) }
225
        'Koha::Exceptions::Patron::Attribute::NonRepeatable',
226
        'Exception is thrown :-D';
227
    is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
228
};

Return to bug 27833