From a333a13da57c461baa5278ba7669795a93141789 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 28 Apr 2015 10:59:42 +0200 Subject: [PATCH] Bug 8007: (QA followup) Add error handling when generating the pdf If error occurs when generating the pdf, it would be better to get an encapsulated error instead of the "software error" message in the pdf file. To test this patch I added this change: b/Koha/Borrower/Discharge.pm -115,6 +115,7 @@ sub generate_as_pdf { say $html_fh $html_content; close $html_fh; my $pdf = PDF::FromHTML->new( encoding => 'utf-8' ); + $html_path .= "poeut"; $pdf->load_file( $html_path ); $pdf->convert; Signed-off-by: Tomas Cohen Arazi --- .../prog/en/modules/members/discharge.tt | 8 +++++ .../bootstrap/en/modules/opac-discharge.tt | 11 +++++- members/discharge.pl | 35 +++++++++++-------- opac/opac-discharge.pl | 35 +++++++++++-------- 4 files changed, 60 insertions(+), 29 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/discharge.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/discharge.tt index c487506234..9e1f4fcc51 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/discharge.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/discharge.tt @@ -18,6 +18,14 @@

Discharge

+[% FOR message IN messages %] +
+ [% IF message.code == "unable_to_generate_pdf" %] + An error occurs when generating the pdf file. + Please contact the administrator to resolve this problem. + [% END %] +
+[% END %] [% UNLESS can_be_discharged %]

Cannot edit discharge: borrower has issues.

[% ELSE %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-discharge.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-discharge.tt index c77533633d..d8f5648c81 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-discharge.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-discharge.tt @@ -23,6 +23,15 @@

Discharge

+ [% FOR message IN messages %] +
+ [% IF message.code == "unable_to_generate_pdf" %] + An error occurs when generating the pdf file. + Please contact the staff to resolve this problem. + [% END %] +
+ [% END %] + [% IF success %]

Your discharge request has been sent. Your discharge will be available on this page within a few days.

[% ELSIF available %] @@ -31,7 +40,7 @@

Your discharge will be available on this page within a few days.

[% ELSIF has_issues %]

You cannot be discharged, you have issues. Please return items before asking for a discharge.

- [% ELSE %] + [% ELSIF not messages %]

What is a discharge?

This document certifies that you have returned all borrowed items. It is sometimes asked during a file transfer from a school to another. The discharge is sent by us to your school. You will also find it available on your reader account.

Warning: This request is only valid if you are in good standing with the library. Once the application is made, you can not borrow library materials.

diff --git a/members/discharge.pl b/members/discharge.pl index e2f703d141..2144af9e51 100755 --- a/members/discharge.pl +++ b/members/discharge.pl @@ -28,6 +28,7 @@ Allows librarian to edit and/or manage borrowers' discharges =cut use Modern::Perl; +use Carp; use CGI qw( -utf8 ); use C4::Auth; @@ -75,20 +76,26 @@ if ( $input->param('borrowernumber') ) { borrowernumber => $borrowernumber }); } - my $pdf_path = Koha::Borrower::Discharge::generate_as_pdf( - { borrowernumber => $borrowernumber, } ); - - binmode(STDOUT); - print $input->header( - -type => 'application/pdf', - -charset => 'utf-8', - -attachment => "discharge_$borrowernumber.pdf", - ); - open my $fh, '<', $pdf_path; - my @lines = <$fh>; - close $fh; - print @lines; - exit; + eval { + my $pdf_path = Koha::Borrower::Discharge::generate_as_pdf( + { borrowernumber => $borrowernumber, } ); + + binmode(STDOUT); + print $input->header( + -type => 'application/pdf', + -charset => 'utf-8', + -attachment => "discharge_$borrowernumber.pdf", + ); + open my $fh, '<', $pdf_path; + my @lines = <$fh>; + close $fh; + print @lines; + exit; + }; + if ( $@ ) { + carp $@; + $template->param( messages => [ {type => 'error', code => 'unable_to_generate_pdf'} ] ); + } } $template->param( diff --git a/opac/opac-discharge.pl b/opac/opac-discharge.pl index e6eca73512..800222cd4b 100755 --- a/opac/opac-discharge.pl +++ b/opac/opac-discharge.pl @@ -18,6 +18,7 @@ # along with Koha; if not, see . use Modern::Perl; +use Carp; use C4::Auth qw(:DEFAULT get_session); use CGI qw( -utf8 ); @@ -55,21 +56,27 @@ if ( $op eq 'request' ) { } } elsif ( $op eq 'get' ) { - my $pdf_path = Koha::Borrower::Discharge::generate_as_pdf({ - borrowernumber => $loggedinuser - }); + eval { + my $pdf_path = Koha::Borrower::Discharge::generate_as_pdf({ + borrowernumber => $loggedinuser + }); - binmode(STDOUT); - print $input->header( - -type => 'application/pdf', - -charset => 'utf-8', - -attachment => "discharge_$loggedinuser.pdf", - ); - open my $fh, '<', $pdf_path; - my @lines = <$fh>; - close $fh; - print @lines; - exit; + binmode(STDOUT); + print $input->header( + -type => 'application/pdf', + -charset => 'utf-8', + -attachment => "discharge_$loggedinuser.pdf", + ); + open my $fh, '<', $pdf_path; + my @lines = <$fh>; + close $fh; + print @lines; + exit; + }; + if ( $@ ) { + carp $@; + $template->param( messages => [ {type => 'error', code => 'unable_to_generate_pdf'} ] ); + } } else { my $pending = Koha::Borrower::Discharge::count({ -- 2.39.5