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

(-)a/C4/Letters.pm (-16 / +52 lines)
Lines 38-43 use Koha::SMS::Providers; Link Here
38
use Koha::Email;
38
use Koha::Email;
39
use Koha::DateUtils qw( format_sqldatetime dt_from_string );
39
use Koha::DateUtils qw( format_sqldatetime dt_from_string );
40
40
41
use Scalar::Util qw ( blessed );
42
use Try::Tiny;
43
41
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
44
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
42
45
43
BEGIN {
46
BEGIN {
Lines 1044-1061 sub SendQueuedMessages { Link Here
1044
          if $params->{'verbose'} or $debug;
1047
          if $params->{'verbose'} or $debug;
1045
        # This is just begging for subclassing
1048
        # This is just begging for subclassing
1046
        next MESSAGE if ( lc($message->{'message_transport_type'}) eq 'rss' );
1049
        next MESSAGE if ( lc($message->{'message_transport_type'}) eq 'rss' );
1047
        if ( lc( $message->{'message_transport_type'} ) eq 'email' ) {
1050
        eval {
1048
            _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
1051
            if ( lc( $message->{'message_transport_type'} ) eq 'email' ) {
1049
        }
1050
        elsif ( lc( $message->{'message_transport_type'} ) eq 'sms' ) {
1051
            if ( C4::Context->preference('SMSSendDriver') eq 'Email' ) {
1052
                my $member = C4::Members::GetMember( 'borrowernumber' => $message->{'borrowernumber'} );
1053
                my $sms_provider = Koha::SMS::Providers->find( $member->{'sms_provider_id'} );
1054
                $message->{to_address} .= '@' . $sms_provider->domain();
1055
                _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
1052
                _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
1056
            } else {
1057
                _send_message_by_sms( $message );
1058
            }
1053
            }
1054
            elsif ( lc( $message->{'message_transport_type'} ) eq 'sms' ) {
1055
                if ( C4::Context->preference('SMSSendDriver') eq 'Email' ) {
1056
                    my $member = C4::Members::GetMember( 'borrowernumber' => $message->{'borrowernumber'} );
1057
                    my $sms_provider = Koha::SMS::Providers->find( $member->{'sms_provider_id'} );
1058
                    $message->{to_address} .= '@' . $sms_provider->domain();
1059
                    _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
1060
                } else {
1061
                    _send_message_by_sms( $message );
1062
                }
1063
            }
1064
        };
1065
        if ($@) {
1066
            warn $@;
1059
        }
1067
        }
1060
    }
1068
    }
1061
    return scalar( @$unsent_messages );
1069
    return scalar( @$unsent_messages );
Lines 1416-1427 sub _send_message_by_sms { Link Here
1416
        return;
1424
        return;
1417
    }
1425
    }
1418
1426
1419
    my $success = C4::SMS->send_sms( { destination => $member->{'smsalertnumber'},
1427
    my $success;
1420
                                       message     => $message->{'content'},
1428
    try {
1421
                                     } );
1429
        $success = C4::SMS->send_sms( { destination => $member->{'smsalertnumber'},
1422
    _set_message_status( { message_id => $message->{'message_id'},
1430
                                           message     => $message->{'content'},
1423
                           status     => ($success ? 'sent' : 'failed'),
1431
                                         } );
1424
                           delivery_note => ($success ? '' : 'No notes from SMS driver') } );
1432
        _set_message_status( { message_id => $message->{'message_id'},
1433
                               status     => ($success ? 'sent' : 'failed'),
1434
                               delivery_note => ($success ? '' : 'No notes from SMS driver') } );
1435
1436
    } catch {
1437
        if (blessed($_)){
1438
            if ($_->isa('Koha::Exception::ConnectionFailed')){
1439
                # Keep the message in pending status but
1440
                # add a delivery note explaining what happened
1441
                _set_message_status ( { message_id => $message->{'message_id'},
1442
                                        status     => 'pending',
1443
                                        delivery_note => 'Connection failed. Attempting to resend.' } );
1444
            }
1445
            else {
1446
                # failsafe: if we catch and unknown exception, set message status to failed
1447
                _set_message_status( { message_id => $message->{'message_id'},
1448
                               status     => 'failed',
1449
                               delivery_note => 'Unknown exception.' } );
1450
                $_->rethrow();
1451
            }
1452
        }
1453
        else {
1454
            # failsafe
1455
            _set_message_status( { message_id => $message->{'message_id'},
1456
                               status     => 'failed',
1457
                               delivery_note => 'Unknown non-blessed exception.' } );
1458
            die $_;
1459
        }
1460
    };
1425
1461
1426
    return $success;
1462
    return $success;
1427
}
1463
}
(-)a/C4/SMS.pm (-8 / +14 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 105-111 sub send_sms { Link Here
105
        %args = map { q{_} . $_ => $conf->{$_} } keys %$conf;
107
        %args = map { q{_} . $_ => $conf->{$_} } keys %$conf;
106
    }
108
    }
107
109
108
    eval {
110
111
    #We might die because SMS::Send $driver is not defined or the sms-number has a bad format
112
    #Catch those errors and fail the sms-sending gracefully.
113
    try {
109
        # Create a sender
114
        # Create a sender
110
        $sender = SMS::Send->new(
115
        $sender = SMS::Send->new(
111
            $driver,
116
            $driver,
Lines 119-133 sub send_sms { Link Here
119
            to   => $params->{destination},
124
            to   => $params->{destination},
120
            text => $params->{message},
125
            text => $params->{message},
121
        );
126
        );
127
        return $sent;
128
    } catch {
129
        if (blessed($_) && $_->can('rethrow')) {
130
            $_->rethrow();
131
        }
132
        else {
133
            die $_;
134
        }
122
    };
135
    };
123
136
124
    #We might die because SMS::Send $driver is not defined or the sms-number has a bad format
125
    #Catch those errors and fail the sms-sending gracefully.
126
    if ($@) {
127
        warn $@;
128
        return;
129
    }
130
    # warn 'failure' unless $sent;
131
    return $sent;
137
    return $sent;
132
}
138
}
133
139
(-)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 (-2 / +13 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 => 82;
21
use Test::More tests => 85;
22
use Test::MockModule;
22
use Test::MockModule;
23
use Test::Warn;
23
use Test::Warn;
24
24
Lines 153-158 is( $resent, undef, 'ResendMessage should return undef if not message_id given' Link Here
153
is($messages->[0]->{delivery_note}, 'Missing SMS number',
153
is($messages->[0]->{delivery_note}, 'Missing SMS number',
154
   'Delivery note for no smsalertnumber correctly set');
154
   'Delivery note for no smsalertnumber correctly set');
155
155
156
# Test connectivity Exception (Bug 14791)
157
t::lib::Mocks::mock_preference('SMSSendDriver', 'Example::ExceptionExample');
158
ModMember(borrowernumber => $borrowernumber, smsalertnumber => "+1234567890");
159
warning_is { $messages_processed = C4::Letters::SendQueuedMessages(); }
160
    "Fake SMS driver",
161
    "SMS sent using the mocked SMS::Send driver subroutine send_sms";
162
$messages = C4::Letters::GetQueuedMessages();
163
is( $messages->[0]->{status}, 'pending',
164
    'Message is still pending after SendQueuedMessages() because of network failure (bug 14791)' );
165
is( $messages->[0]->{delivery_note}, 'Connection failed. Attempting to resend.',
166
    'Message has correct delivery note about resending' );
167
156
168
157
# GetLetters
169
# GetLetters
158
my $letters = C4::Letters::GetLetters();
170
my $letters = C4::Letters::GetLetters();
159
- 

Return to bug 14791