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

(-)a/C4/Letters.pm (-11 / +36 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 947-952 sub SendQueuedMessages { Link Here
947
949
948
    my $unsent_messages = _get_unsent_messages();
950
    my $unsent_messages = _get_unsent_messages();
949
    MESSAGE: foreach my $message ( @$unsent_messages ) {
951
    MESSAGE: foreach my $message ( @$unsent_messages ) {
952
        
950
        # warn Data::Dumper->Dump( [ $message ], [ 'message' ] );
953
        # warn Data::Dumper->Dump( [ $message ], [ 'message' ] );
951
        warn sprintf( 'sending %s message to patron: %s',
954
        warn sprintf( 'sending %s message to patron: %s',
952
                      $message->{'message_transport_type'},
955
                      $message->{'message_transport_type'},
Lines 954-964 sub SendQueuedMessages { Link Here
954
          if $params->{'verbose'} or $debug;
957
          if $params->{'verbose'} or $debug;
955
        # This is just begging for subclassing
958
        # This is just begging for subclassing
956
        next MESSAGE if ( lc($message->{'message_transport_type'}) eq 'rss' );
959
        next MESSAGE if ( lc($message->{'message_transport_type'}) eq 'rss' );
957
        if ( lc( $message->{'message_transport_type'} ) eq 'email' ) {
960
        eval {
958
            _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
961
            if ( lc( $message->{'message_transport_type'} ) eq 'email' ) {
959
        }
962
                _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
960
        elsif ( lc( $message->{'message_transport_type'} ) eq 'sms' ) {
963
            }
961
            _send_message_by_sms( $message );
964
            elsif ( lc( $message->{'message_transport_type'} ) eq 'sms' ) {
965
                _send_message_by_sms( $message );
966
            }
967
        };
968
        if ($@) {
969
            warn $@;
962
        }
970
        }
963
    }
971
    }
964
    return scalar( @$unsent_messages );
972
    return scalar( @$unsent_messages );
Lines 1262-1273 sub _send_message_by_sms { Link Here
1262
        return;
1270
        return;
1263
    }
1271
    }
1264
1272
1265
    my $success = C4::SMS->send_sms( { destination => $member->{'smsalertnumber'},
1273
    try {
1266
                                       message     => $message->{'content'},
1274
        my $success = C4::SMS->send_sms( { destination => $member->{'smsalertnumber'},
1267
                                     } );
1275
                                           message     => $message->{'content'},
1268
    _set_message_status( { message_id => $message->{'message_id'},
1276
                                         } );
1269
                           status     => ($success ? 'sent' : 'failed') } );
1277
        _set_message_status( { message_id => $message->{'message_id'},
1270
    return $success;
1278
                               status     => ($success ? 'sent' : 'failed') } );
1279
        return $success;
1280
    } catch {
1281
        if (blessed($_)){
1282
            if ($_->isa('Koha::Exception::ConnectionFailed')){
1283
                # Don't do anything, the message will stay in
1284
                # 'pending' status and will be resent again later.
1285
            }
1286
            else {
1287
                $_->rethrow();
1288
            }
1289
        }
1290
        else {
1291
            die $_;
1292
        }
1293
    };
1294
1295
    return;
1271
}
1296
}
1272
1297
1273
sub _update_message_to_address {
1298
sub _update_message_to_address {
(-)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 / +30 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 => 65;
21
use Test::More tests => 68;
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 136-141 is( Link Here
136
    'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
145
    'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
137
);
146
);
138
147
148
# Test connectivity Exception (Bug 14791)
149
$message_id = C4::Letters::EnqueueLetter($my_message);
150
$messages = C4::Letters::GetQueuedMessages();
151
is( @$messages, 2, 'second message stored for the borrower' );
152
ModMember(borrowernumber => $borrowernumber, smsalertnumber => "+1234567890");
153
154
{
155
warning_is {
156
    $messages_processed = C4::Letters::SendQueuedMessages(); }
157
    "Fake SMS driver",
158
    "SMS sent using the mocked SMS::Send driver subroutine send_sms";
159
160
is(
161
    $messages->[1]->{status},
162
    'pending',
163
    'Message is still pending after SendQueuedMessages() because of network failure (bug 14791)'
164
);
165
}
166
139
# GetLetters
167
# GetLetters
140
my $letters = C4::Letters::GetLetters();
168
my $letters = C4::Letters::GetLetters();
141
is( @$letters, 0, 'GetLetters returns the correct number of letters' );
169
is( @$letters, 0, 'GetLetters returns the correct number of letters' );
142
- 

Return to bug 14791