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

(-)a/C4/Letters.pm (-11 / +39 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 1269-1280 sub _send_message_by_sms { Link Here
1269
        return;
1277
        return;
1270
    }
1278
    }
1271
1279
1272
    my $success = C4::SMS->send_sms( { destination => $member->{'smsalertnumber'},
1280
    my $success;
1273
                                       message     => $message->{'content'},
1281
    try {
1274
                                     } );
1282
        $success = C4::SMS->send_sms( { destination => $member->{'smsalertnumber'},
1275
    _set_message_status( { message_id => $message->{'message_id'},
1283
                                           message     => $message->{'content'},
1276
                           status     => ($success ? 'sent' : 'failed'),
1284
                                         } );
1277
                           delivery_note => ($success ? '' : 'No notes from SMS driver') } );
1285
        _set_message_status( { message_id => $message->{'message_id'},
1286
                               status     => ($success ? 'sent' : 'failed'),
1287
                               delivery_note => ($success ? '' : 'No notes from SMS driver') } );
1288
1289
    } catch {
1290
        if (blessed($_)){
1291
            if ($_->isa('Koha::Exception::ConnectionFailed')){
1292
                # Keep the message in pending status but
1293
                # add a delivery note explaining what happened
1294
                _set_message_status ( { message_id => $message->{'message_id'},
1295
                                        status     => 'pending',
1296
                                        delivery_note => 'Connection failed. Attempting to resend.' } );
1297
            }
1298
            else {
1299
                $_->rethrow();
1300
            }
1301
        }
1302
        else {
1303
            die $_;
1304
        }
1305
    };
1278
1306
1279
    return $success;
1307
    return $success;
1280
}
1308
}
(-)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 / +25 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 => 67;
21
use Test::More tests => 71;
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 139-144 is( Link Here
139
# Delivery notes
148
# Delivery notes
140
is($messages->[0]->{delivery_note}, 'Missing SMS number', 'Delivery note for no smsalertnumber correctly set');
149
is($messages->[0]->{delivery_note}, 'Missing SMS number', 'Delivery note for no smsalertnumber correctly set');
141
150
151
# Test connectivity Exception (Bug 14791)
152
$message_id = C4::Letters::EnqueueLetter($my_message);
153
$messages = C4::Letters::GetQueuedMessages();
154
is( @$messages, 2, 'second message stored for the borrower' );
155
ModMember(borrowernumber => $borrowernumber, smsalertnumber => "+1234567890");
156
warning_is { $messages_processed = C4::Letters::SendQueuedMessages(); }
157
    "Fake SMS driver",
158
    "SMS sent using the mocked SMS::Send driver subroutine send_sms";
159
$messages = C4::Letters::GetQueuedMessages();
160
is( $messages->[1]->{status}, 'pending',
161
    'Message is still pending after SendQueuedMessages() because of network failure (bug 14791)' );
162
is( $messages->[1]->{delivery_note}, 'Connection failed. Attempting to resend.',
163
    'Message has correct delivery note about resending' );
164
142
# GetLetters
165
# GetLetters
143
my $letters = C4::Letters::GetLetters();
166
my $letters = C4::Letters::GetLetters();
144
is( @$letters, 0, 'GetLetters returns the correct number of letters' );
167
is( @$letters, 0, 'GetLetters returns the correct number of letters' );
145
- 

Return to bug 14791