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

(-)a/C4/Letters.pm (-6 / +36 lines)
Lines 26-31 use Encode; Link Here
26
use Carp;
26
use Carp;
27
use Template;
27
use Template;
28
use Module::Load::Conditional qw(can_load);
28
use Module::Load::Conditional qw(can_load);
29
use Scalar::Util qw ( blessed );
30
use Try::Tiny;
29
31
30
use C4::Members;
32
use C4::Members;
31
use C4::Members::Attributes qw(GetBorrowerAttributes);
33
use C4::Members::Attributes qw(GetBorrowerAttributes);
Lines 1392-1403 sub _send_message_by_sms { Link Here
1392
        return;
1394
        return;
1393
    }
1395
    }
1394
1396
1395
    my $success = C4::SMS->send_sms( { destination => $patron->smsalertnumber,
1397
    my $success;
1396
                                       message     => $message->{'content'},
1398
    try {
1397
                                     } );
1399
        $success = C4::SMS->send_sms(
1398
    _set_message_status( { message_id => $message->{'message_id'},
1400
            { destination => $patron->smsalertnumber,
1399
                           status     => ($success ? 'sent' : 'failed'),
1401
            message     => $message->{'content'},
1400
                           delivery_note => ($success ? '' : 'No notes from SMS driver') } );
1402
        } );
1403
        _set_message_status( { message_id => $message->{'message_id'},
1404
                               status     => ($success ? 'sent' : 'failed'),
1405
                               delivery_note => ($success ? '' : 'No notes from SMS driver') } );
1406
1407
    } catch {
1408
        if (blessed($_)){
1409
            if ($_->isa('Koha::Exceptions::ConnectionFailed')){
1410
                # Keep the message in pending status but
1411
                # add a delivery note explaining what happened
1412
                _set_message_status ( { message_id => $message->{'message_id'},
1413
                                        status     => 'pending',
1414
                                        delivery_note => 'Connection failed. Attempting to resend.' } );
1415
            }
1416
            else {
1417
                # failsafe: if we catch and unknown exception, set message status to failed
1418
                _set_message_status( { message_id => $message->{'message_id'},
1419
                               status     => 'failed',
1420
                               delivery_note => ref($_) } );
1421
                $_->rethrow();
1422
            }
1423
        }
1424
        else {
1425
            _set_message_status( { message_id => $message->{'message_id'},
1426
                               status     => 'failed',
1427
                               delivery_note => 'Unknown exception.' } );
1428
            die $_;
1429
        }
1430
    };
1401
1431
1402
    return $success;
1432
    return $success;
1403
}
1433
}
(-)a/C4/SMS.pm (-8 / +13 lines)
Lines 56-61 use warnings; Link Here
56
use C4::Context;
56
use C4::Context;
57
use File::Spec;
57
use File::Spec;
58
58
59
use Try::Tiny;
60
use Scalar::Util qw ( blessed );
59
61
60
62
61
=head1 METHODS
63
=head1 METHODS
Lines 107-113 sub send_sms { Link Here
107
        %args = map { q{_} . $_ => $conf->{$_} } keys %$conf;
109
        %args = map { q{_} . $_ => $conf->{$_} } keys %$conf;
108
    }
110
    }
109
111
110
    eval {
112
    #We might die because SMS::Send $driver is not defined or the sms-number has a bad format
113
    #Catch those errors and fail the sms-sending gracefully.
114
    try {
111
        # Create a sender
115
        # Create a sender
112
        $sender = SMS::Send->new(
116
        $sender = SMS::Send->new(
113
            $driver,
117
            $driver,
Lines 121-135 sub send_sms { Link Here
121
            to   => $params->{destination},
125
            to   => $params->{destination},
122
            text => $params->{message},
126
            text => $params->{message},
123
        );
127
        );
128
        return $sent;
129
    } catch {
130
        if (blessed($_) && $_->can('rethrow')) {
131
            $_->rethrow();
132
        }
133
        else {
134
            die $_;
135
        }
124
    };
136
    };
125
137
126
    #We might die because SMS::Send $driver is not defined or the sms-number has a bad format
127
    #Catch those errors and fail the sms-sending gracefully.
128
    if ($@) {
129
        warn $@;
130
        return;
131
    }
132
    # warn 'failure' unless $sent;
133
    return $sent;
138
    return $sent;
134
}
139
}
135
140
(-)a/Koha/Exceptions.pm (+4 lines)
Lines 22-27 use Exception::Class ( Link Here
22
        isa => 'Koha::Exceptions::Exception',
22
        isa => 'Koha::Exceptions::Exception',
23
        description => 'The default value cannot be deleted'
23
        description => 'The default value cannot be deleted'
24
    },
24
    },
25
    'Koha::Exceptions::ConnectionFailed' => {
26
        isa => 'Koha::Exceptions::Exception',
27
        description => 'Connecting to host failed',
28
    },
25
    'Koha::Exceptions::MissingParameter' => {
29
    'Koha::Exceptions::MissingParameter' => {
26
        isa => 'Koha::Exceptions::Exception',
30
        isa => 'Koha::Exceptions::Exception',
27
        description => 'A required parameter is missing'
31
        description => 'A required parameter is missing'
(-)a/t/db_dependent/Letters.t (-2 / +31 lines)
Lines 18-28 Link Here
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
use Test::More tests => 69;
21
use Test::More tests => 70;
22
use Test::MockModule;
22
use Test::MockModule;
23
use Test::Warn;
23
use Test::Warn;
24
24
25
use MARC::Record;
25
use MARC::Record;
26
use Module::Load::Conditional qw/check_install/;
26
27
27
my %mail;
28
my %mail;
28
my $module = new Test::MockModule('Mail::Sendmail');
29
my $module = new Test::MockModule('Mail::Sendmail');
Lines 160-165 is( $resent, undef, 'ResendMessage should return undef if not message_id given' Link Here
160
is($messages->[0]->{delivery_note}, 'Missing SMS number',
161
is($messages->[0]->{delivery_note}, 'Missing SMS number',
161
   'Delivery note for no smsalertnumber correctly set');
162
   'Delivery note for no smsalertnumber correctly set');
162
163
164
subtest 'Test SMS::Send driver connectivity exception' => sub {
165
    if ( check_install( module => 'SMS::Send::Test') ) {
166
        plan tests => 3;
167
    } else {
168
        plan skip_all => 'SMS::Send not installed';
169
    }
170
171
172
    my $sms_send_module = new Test::MockModule('SMS::Send::Test');
173
    $sms_send_module->mock(
174
        'send_sms',
175
        sub {
176
            warn "Fake SMS driver";
177
            Koha::Exceptions::ConnectionFailed->throw(error => "Connection failed");
178
        }
179
    );
180
    t::lib::Mocks::mock_preference('SMSSendDriver', 'Test');
181
    Koha::Patrons->find($borrowernumber)->set({ smsalertnumber => "+1234567890" })->store;
182
    warning_is { $messages_processed = C4::Letters::SendQueuedMessages(); }
183
        "Fake SMS driver",
184
        "SMS sent using the mocked SMS::Send driver subroutine send_sms";
185
    $messages = C4::Letters::GetQueuedMessages();
186
    is( $messages->[0]->{status}, 'pending',
187
        'Message is still pending after SendQueuedMessages() because of network failure (bug 14791)' );
188
    is( $messages->[0]->{delivery_note}, 'Connection failed. Attempting to resend.',
189
        'Message has correct delivery note about resending' );
190
};
191
192
163
193
164
# GetLetters
194
# GetLetters
165
my $letters = C4::Letters::GetLetters();
195
my $letters = C4::Letters::GetLetters();
166
- 

Return to bug 14791