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

(-)a/C4/Letters.pm (-11 / +47 lines)
Lines 35-40 use Date::Calc qw( Add_Delta_Days ); Link Here
35
use Encode;
35
use Encode;
36
use Carp;
36
use Carp;
37
use Koha::Email;
37
use Koha::Email;
38
use Scalar::Util qw( blessed );
39
use Try::Tiny;
38
40
39
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
41
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
40
42
Lines 948-953 sub SendQueuedMessages { Link Here
948
950
949
    my $unsent_messages = _get_unsent_messages();
951
    my $unsent_messages = _get_unsent_messages();
950
    MESSAGE: foreach my $message ( @$unsent_messages ) {
952
    MESSAGE: foreach my $message ( @$unsent_messages ) {
953
951
        # warn Data::Dumper->Dump( [ $message ], [ 'message' ] );
954
        # warn Data::Dumper->Dump( [ $message ], [ 'message' ] );
952
        warn sprintf( 'sending %s message to patron: %s',
955
        warn sprintf( 'sending %s message to patron: %s',
953
                      $message->{'message_transport_type'},
956
                      $message->{'message_transport_type'},
Lines 955-965 sub SendQueuedMessages { Link Here
955
          if $params->{'verbose'} or $debug;
958
          if $params->{'verbose'} or $debug;
956
        # This is just begging for subclassing
959
        # This is just begging for subclassing
957
        next MESSAGE if ( lc($message->{'message_transport_type'}) eq 'rss' );
960
        next MESSAGE if ( lc($message->{'message_transport_type'}) eq 'rss' );
958
        if ( lc( $message->{'message_transport_type'} ) eq 'email' ) {
961
        eval {
959
            _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
962
            if ( lc( $message->{'message_transport_type'} ) eq 'email' ) {
960
        }
963
                _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
961
        elsif ( lc( $message->{'message_transport_type'} ) eq 'sms' ) {
964
            }
962
            _send_message_by_sms( $message );
965
            elsif ( lc( $message->{'message_transport_type'} ) eq 'sms' ) {
966
                _send_message_by_sms( $message );
967
            }
968
        };
969
        if ($@) {
970
            warn $@;
963
        }
971
        }
964
    }
972
    }
965
    return scalar( @$unsent_messages );
973
    return scalar( @$unsent_messages );
Lines 1314-1325 sub _send_message_by_sms { Link Here
1314
        return;
1322
        return;
1315
    }
1323
    }
1316
1324
1317
    my $success = C4::SMS->send_sms( { destination => $member->{'smsalertnumber'},
1325
    my $success;
1318
                                       message     => $message->{'content'},
1326
    try {
1319
                                     } );
1327
        $success = C4::SMS->send_sms( { destination => $member->{'smsalertnumber'},
1320
    _set_message_status( { message_id => $message->{'message_id'},
1328
                                           message     => $message->{'content'},
1321
                           status     => ($success ? 'sent' : 'failed'),
1329
                                         } );
1322
                           delivery_note => ($success ? '' : 'No notes from SMS driver') } );
1330
        _set_message_status( { message_id => $message->{'message_id'},
1331
                               status     => ($success ? 'sent' : 'failed'),
1332
                               delivery_note => ($success ? '' : 'No notes from SMS driver') } );
1333
1334
    } catch {
1335
        if (blessed($_)){
1336
            if ($_->isa('Koha::Exception::ConnectionFailed')){
1337
                # Keep the message in pending status but
1338
                # add a delivery note explaining what happened
1339
                _set_message_status ( { message_id => $message->{'message_id'},
1340
                                        status     => 'pending',
1341
                                        delivery_note => 'Connection failed. Attempting to resend.' } );
1342
            }
1343
            else {
1344
                # failsafe: if we catch and unknown exception, set message status to failed
1345
                _set_message_status( { message_id => $message->{'message_id'},
1346
                               status     => 'failed',
1347
                               delivery_note => 'Unknown exception.' } );
1348
                $_->rethrow();
1349
            }
1350
        }
1351
        else {
1352
            # failsafe
1353
            _set_message_status( { message_id => $message->{'message_id'},
1354
                               status     => 'failed',
1355
                               delivery_note => 'Unknown non-blessed exception.' } );
1356
            die $_;
1357
        }
1358
    };
1323
1359
1324
    return $success;
1360
    return $success;
1325
}
1361
}
(-)a/C4/SMS.pm (-8 / +15 lines)
Lines 36-41 use strict; Link Here
36
use warnings;
36
use warnings;
37
37
38
use C4::Context;
38
use C4::Context;
39
use Try::Tiny;
40
use Scalar::Util qw ( blessed );
39
41
40
use vars qw( $VERSION );
42
use vars qw( $VERSION );
41
43
Lines 77-83 sub send_sms { Link Here
77
    # warn "using driver: $driver to send message to $params->{'destination'}";
79
    # warn "using driver: $driver to send message to $params->{'destination'}";
78
80
79
    my ($sent, $sender);
81
    my ($sent, $sender);
80
    eval {
82
83
    #We might die because SMS::Send $driver is not defined or the sms-number has a bad format
84
    #Catch those errors and fail the sms-sending gracefully.
85
    try {
81
        # Create a sender
86
        # Create a sender
82
        $sender = SMS::Send->new( $driver,
87
        $sender = SMS::Send->new( $driver,
83
                                 _login    => C4::Context->preference('SMSSendUsername'),
88
                                 _login    => C4::Context->preference('SMSSendUsername'),
Lines 88-101 sub send_sms { Link Here
88
        $sent = $sender->send_sms( to   => $params->{'destination'},
93
        $sent = $sender->send_sms( to   => $params->{'destination'},
89
                                  text => $params->{'message'},
94
                                  text => $params->{'message'},
90
                             );
95
                             );
96
        return $sent;
97
    } catch {
98
        if (blessed($_) && $_->can('rethrow')) {
99
            $_->rethrow();
100
        }
101
        else {
102
            die $_;
103
        }
91
    };
104
    };
92
    #We might die because SMS::Send $driver is not defined or the sms-number has a bad format
105
93
    #Catch those errors and fail the sms-sending gracefully.
94
    if ($@) {
95
        warn $@;
96
        return;
97
    }
98
    # warn 'failure' unless $sent;
99
    return $sent;
106
    return $sent;
100
}
107
}
101
108
(-)a/SMS/Send/Example/ExceptionExample.pm (+87 lines)
Line 0 Link Here
1
package SMS::Send::Example::ExceptionExample;
2
3
=pod
4
5
=head1 NAME
6
7
SMS::Send::Example::ExceptionExample
8
9
=head1 SYNOPSIS
10
11
  use Try::Tiny;
12
  use Scalar::Util qw( blessed );
13
14
  # Create a testing sender
15
  my $send = SMS::Send->new( 'Example::ExceptionExample' );
16
17
  # Send a message
18
  try {
19
    $send->send_sms(
20
      text => 'Hi there',
21
      to   => '+61 (4) 1234 5678',
22
   );
23
  } catch {
24
    if (blessed($_) && $_->can('rethrow')){
25
        # handle exception
26
    } else {
27
        die $_;
28
    }
29
  }
30
31
=head1 DESCRIPTION
32
33
    This SMS::Send module provides an example for how
34
    to throw Koha::Exceptions in case of an error.
35
36
    Exceptions will be caught outside this module by
37
    try-catch block.
38
39
=cut
40
41
use strict;
42
use SMS::Send::Driver ();
43
use Koha::Exception::ConnectionFailed;
44
45
use vars qw{$VERSION @ISA};
46
BEGIN {
47
        $VERSION = '0.06';
48
        @ISA     = 'SMS::Send::Driver';
49
}
50
51
52
53
54
55
#####################################################################
56
# Constructor
57
58
sub new {
59
        my $class = shift;
60
61
        my $self = bless {}, $class;
62
63
        $self->{_login} = "ned";
64
        $self->{_password} = "flanders";
65
66
        return $self;
67
}
68
69
sub send_sms {
70
    # ...
71
    # ... our imaginary cURL implementation of sending sms messages to gateway
72
    #  $curl = sendMessageWithcURL("http://url.com/send", {
73
    #                           destination => $params->{to},
74
    #                           text        => $params->{text}
75
    #                          });
76
    # my $errorCode = $curl->{'retcode'};
77
    # Using cURL, our request produced error code 6, CURLE_COULDNT_RESOLVE_HOST
78
    my $errorCode = 6;
79
80
    if ($errorCode == 6) {
81
        Koha::Exception::ConnectionFailed->throw(error => "Connection failed");
82
    }
83
84
    return 1;
85
}
86
87
1;
(-)a/t/db_dependent/Letters.t (-3 / +22 lines)
Lines 18-24 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 => 71;
21
use Test::More tests => 74;
22
use Test::MockModule;
22
use Test::MockModule;
23
use Test::Warn;
23
use Test::Warn;
24
24
Lines 33-39 $module->mock( Link Here
33
        %mail = @_;
33
        %mail = @_;
34
    }
34
    }
35
);
35
);
36
37
use_ok('C4::Context');
36
use_ok('C4::Context');
38
use_ok('C4::Members');
37
use_ok('C4::Members');
39
use_ok('C4::Branch');
38
use_ok('C4::Branch');
Lines 46-51 use Koha::DateUtils qw( dt_from_string output_pref ); Link Here
46
use Koha::Acquisition::Order;
45
use Koha::Acquisition::Order;
47
use Koha::Acquisition::Bookseller;
46
use Koha::Acquisition::Bookseller;
48
use Koha::Database;
47
use Koha::Database;
48
use Koha::Exception::ConnectionFailed;
49
50
my $c4sms = new Test::MockModule('C4::SMS');
51
$c4sms->mock(
52
    'driver' =>
53
    sub {
54
        warn "Fake SMS driver";
55
        return "Example::ExceptionExample";
56
    }
57
);
49
58
50
my $dbh = C4::Context->dbh;
59
my $dbh = C4::Context->dbh;
51
60
Lines 149-154 is( $resent, undef, 'ResendMessage should return undef if not message_id given' Link Here
149
# Delivery notes
158
# Delivery notes
150
is($messages->[0]->{delivery_note}, 'Missing SMS number', 'Delivery note for no smsalertnumber correctly set');
159
is($messages->[0]->{delivery_note}, 'Missing SMS number', 'Delivery note for no smsalertnumber correctly set');
151
160
161
# Test connectivity Exception (Bug 14791)
162
ModMember(borrowernumber => $borrowernumber, smsalertnumber => "+1234567890");
163
warning_is { $messages_processed = C4::Letters::SendQueuedMessages(); }
164
    "Fake SMS driver",
165
    "SMS sent using the mocked SMS::Send driver subroutine send_sms";
166
$messages = C4::Letters::GetQueuedMessages();
167
is( $messages->[0]->{status}, 'pending',
168
    'Message is still pending after SendQueuedMessages() because of network failure (bug 14791)' );
169
is( $messages->[0]->{delivery_note}, 'Connection failed. Attempting to resend.',
170
    'Message has correct delivery note about resending' );
171
152
# GetLetters
172
# GetLetters
153
my $letters = C4::Letters::GetLetters();
173
my $letters = C4::Letters::GetLetters();
154
is( @$letters, 0, 'GetLetters returns the correct number of letters' );
174
is( @$letters, 0, 'GetLetters returns the correct number of letters' );
155
- 

Return to bug 14791