Browse Source

Bug 26048: Use ErrorDocument middleware for Plack HTTP errors

This patch uses the ErrorDocument middleware to use Koha's
custom error pages instead of generic Plack error responses

Test plan:
0. Apply patch
1. vi /usr/sbin/koha-plack (and change "development" to "deployment")
2. vi ./opac/opac-main.pl
3. Add "die" to line 2
4. vi ./mainpage.pl
5. Add "die" to line 2
6. cp ./debian/templates/plack.psgi /etc/koha/sites/kohadev/plack.psgi
7. koha-plack --restart kohadev
8. Go to http://localhost:8080/cgi-bin/koha/opac-main.pl
9. See a beautiful OPAC 500 error instead of "Internal Server Error"
10. Go to http://localhost:8080/cgi-bin/koha/blah.pl
11. See a beautiful OPAC 404 error instead of "not found"
12. Go to http://localhost:8081/cgi-bin/koha/mainpage.pl
13. See a beautiful Staff interface 500 error instead of "Internal Server Error"
14. Go to http://localhost:8081/cgi-bin/koha/blah.pl
15. See a beautiful Staff interface 404 error instead of "not found"

For bonus points:
16. koha-plack --disable kohadev
17. koha-plack --stop kohadev
18. service apache restart
19. Repeat the above test plan to show CGI still works for 404 (although 500
will show "Software Error" due to C4::Context needing some improvements)
20. Using the "Network" tab on your developer tools, make sure 404 and 500
are returned by the appropriate error pages

Signed-off-by: Eden Bacani <eden.bacani@gmail.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
tags/v21.05.00
David Cook 4 months ago
parent
commit
9fdceaa3b5
13 changed files with 112 additions and 12 deletions
  1. +24
    -0
      debian/templates/plack.psgi
  2. +6
    -1
      errors/400.pl
  3. +6
    -1
      errors/401.pl
  4. +6
    -1
      errors/402.pl
  5. +6
    -1
      errors/403.pl
  6. +22
    -1
      errors/404.pl
  7. +6
    -1
      errors/500.pl
  8. +6
    -1
      opac/errors/400.pl
  9. +6
    -1
      opac/errors/401.pl
  10. +6
    -1
      opac/errors/402.pl
  11. +6
    -1
      opac/errors/403.pl
  12. +6
    -1
      opac/errors/404.pl
  13. +6
    -1
      opac/errors/500.pl

+ 24
- 0
debian/templates/plack.psgi View File

@@ -78,6 +78,18 @@ builder {
enable "+Koha::Middleware::RealIP";

mount '/opac' => builder {
#NOTE: it is important that these are relative links
enable 'ErrorDocument',
400 => 'errors/400.pl',
401 => 'errors/401.pl',
402 => 'errors/402.pl',
403 => 'errors/403.pl',
404 => 'errors/404.pl',
500 => 'errors/500.pl',
subrequest => 1;
#NOTE: Without this middleware to catch fatal errors, ErrorDocument won't be able to render a 500 document
#NOTE: This middleware must be closer to the PSGI app than ErrorDocument
enable "HTTPExceptions";
if ( Log::Log4perl->get_logger('plack-opac')->has_appenders ){
enable 'Log4perl', category => 'plack-opac';
enable 'LogWarn';
@@ -85,6 +97,18 @@ builder {
$opac;
};
mount '/intranet' => builder {
#NOTE: it is important that these are relative links
enable 'ErrorDocument',
400 => 'errors/400.pl',
401 => 'errors/401.pl',
402 => 'errors/402.pl',
403 => 'errors/403.pl',
404 => 'errors/404.pl',
500 => 'errors/500.pl',
subrequest => 1;
#NOTE: Without this middleware to catch fatal errors, ErrorDocument won't be able to render a 500 document
#NOTE: This middleware must be closer to the PSGI app than ErrorDocument
enable "HTTPExceptions";
if ( Log::Log4perl->get_logger('plack-intranet')->has_appenders ){
enable 'Log4perl', category => 'plack-intranet';
enable 'LogWarn';


+ 6
- 1
errors/400.pl View File

@@ -21,6 +21,7 @@ use CGI qw ( -utf8 );
use C4::Auth;
use C4::Output;
use C4::Context;
use List::MoreUtils qw(any);

my $query = CGI->new;
my $admin = C4::Context->preference('KohaAdminEmailAddress');
@@ -37,4 +38,8 @@ $template->param (
admin => $admin,
errno => 400,
);
output_with_http_headers $query, $cookie, $template->output, 'html', '400 Bad Request';
my $status = '400 Bad Request';
if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
$status = '200 OK';
}
output_with_http_headers $query, $cookie, $template->output, 'html', $status;

+ 6
- 1
errors/401.pl View File

@@ -20,6 +20,7 @@ use CGI qw ( -utf8 );
use C4::Auth;
use C4::Output;
use C4::Context;
use List::MoreUtils qw(any);

my $query = CGI->new;
my $admin = C4::Context->preference('KohaAdminEmailAddress');
@@ -36,4 +37,8 @@ $template->param (
admin => $admin,
errno => 401,
);
output_with_http_headers $query, $cookie, $template->output, 'html', '401 Unauthorized';
my $status = '401 Unauthorized';
if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
$status = '200 OK';
}
output_with_http_headers $query, $cookie, $template->output, 'html', $status;

+ 6
- 1
errors/402.pl View File

@@ -21,6 +21,7 @@ use CGI qw ( -utf8 );
use C4::Auth;
use C4::Output;
use C4::Context;
use List::MoreUtils qw(any);

my $query = CGI->new;
my $admin = C4::Context->preference('KohaAdminEmailAddress');
@@ -37,4 +38,8 @@ $template->param (
admin => $admin,
errno => 402,
);
output_with_http_headers $query, $cookie, $template->output, 'html', '402 Payment Required';
my $status = '402 Payment Required';
if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
$status = '200 OK';
}
output_with_http_headers $query, $cookie, $template->output, 'html', $status;

+ 6
- 1
errors/403.pl View File

@@ -21,6 +21,7 @@ use CGI qw ( -utf8 );
use C4::Auth;
use C4::Output;
use C4::Context;
use List::MoreUtils qw(any);

my $query = CGI->new;
my $admin = C4::Context->preference('KohaAdminEmailAddress');
@@ -37,4 +38,8 @@ $template->param (
admin => $admin,
errno => 403,
);
output_with_http_headers $query, $cookie, $template->output, 'html', '403 Forbidden';
my $status = '403 Forbidden';
if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
$status = '200 OK';
}
output_with_http_headers $query, $cookie, $template->output, 'html', $status;

+ 22
- 1
errors/404.pl View File

@@ -18,7 +18,28 @@

use Modern::Perl;
use CGI qw ( -utf8 );
use C4::Auth;
use C4::Output;
use C4::Context;
use List::MoreUtils qw(any);

my $query = CGI->new;
output_error( $query, '404' );
my $admin = C4::Context->preference('KohaAdminEmailAddress');
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
{
template_name => 'errors/errorpage.tt',
query => $query,
type => 'intranet',
authnotrequired => 1,
debug => 1,
}
);
$template->param (
admin => $admin,
errno => 404,
);
my $status = '404 Not Found';
if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
$status = '200 OK';
}
output_with_http_headers $query, $cookie, $template->output, 'html', $status;

+ 6
- 1
errors/500.pl View File

@@ -21,6 +21,7 @@ use CGI qw ( -utf8 );
use C4::Auth;
use C4::Output;
use C4::Context;
use List::MoreUtils qw(any);

my $query = CGI->new;
my $admin = C4::Context->preference('KohaAdminEmailAddress');
@@ -37,4 +38,8 @@ $template->param (
admin => $admin,
errno => 500,
);
output_with_http_headers $query, $cookie, $template->output, 'html', '500 Internal Server Error';
my $status = '500 Internal Server Error';
if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
$status = '200 OK';
}
output_with_http_headers $query, $cookie, $template->output, 'html', $status;

+ 6
- 1
opac/errors/400.pl View File

@@ -21,6 +21,7 @@ use CGI qw ( -utf8 );
use C4::Auth;
use C4::Output;
use C4::Context;
use List::MoreUtils qw(any);

my $query = CGI->new;
my $admin = C4::Context->preference('KohaAdminEmailAddress');
@@ -37,4 +38,8 @@ $template->param (
admin => $admin,
errno => 400,
);
output_with_http_headers $query, $cookie, $template->output, 'html', '400 Bad Request';
my $status = '400 Bad Request';
if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
$status = '200 OK';
}
output_with_http_headers $query, $cookie, $template->output, 'html', $status;

+ 6
- 1
opac/errors/401.pl View File

@@ -21,6 +21,7 @@ use CGI qw ( -utf8 );
use C4::Auth;
use C4::Output;
use C4::Context;
use List::MoreUtils qw(any);

my $query = CGI->new;
my $admin = C4::Context->preference('KohaAdminEmailAddress');
@@ -37,4 +38,8 @@ $template->param (
admin => $admin,
errno => 401,
);
output_with_http_headers $query, $cookie, $template->output, 'html', '401 Unauthorized';
my $status = '401 Unauthorized';
if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
$status = '200 OK';
}
output_with_http_headers $query, $cookie, $template->output, 'html', $status;

+ 6
- 1
opac/errors/402.pl View File

@@ -21,6 +21,7 @@ use CGI qw ( -utf8 );
use C4::Auth;
use C4::Output;
use C4::Context;
use List::MoreUtils qw(any);

my $query = CGI->new;
my $admin = C4::Context->preference('KohaAdminEmailAddress');
@@ -37,4 +38,8 @@ $template->param (
admin => $admin,
errno => 402,
);
output_with_http_headers $query, $cookie, $template->output, 'html', '402 Payment Required';
my $status = '402 Payment Required';
if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
$status = '200 OK';
}
output_with_http_headers $query, $cookie, $template->output, 'html', $status;

+ 6
- 1
opac/errors/403.pl View File

@@ -21,6 +21,7 @@ use CGI qw ( -utf8 );
use C4::Auth;
use C4::Output;
use C4::Context;
use List::MoreUtils qw(any);

my $query = CGI->new;
my $admin = C4::Context->preference('KohaAdminEmailAddress');
@@ -37,4 +38,8 @@ $template->param (
admin => $admin,
errno => 403,
);
output_with_http_headers $query, $cookie, $template->output, 'html', '403 Forbidden';
my $status = '403 Forbidden';
if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
$status = '200 OK';
}
output_with_http_headers $query, $cookie, $template->output, 'html', $status;

+ 6
- 1
opac/errors/404.pl View File

@@ -21,6 +21,7 @@ use CGI qw ( -utf8 );
use C4::Auth;
use C4::Output;
use C4::Context;
use List::MoreUtils qw(any);

my $query = CGI->new;
my $admin = C4::Context->preference('KohaAdminEmailAddress');
@@ -37,4 +38,8 @@ $template->param (
admin => $admin,
errno => 404,
);
output_html_with_http_headers $query, $cookie, $template->output, '404 Not Found';
my $status = '404 Not Found';
if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
$status = '200 OK';
}
output_with_http_headers $query, $cookie, $template->output, 'html', $status;

+ 6
- 1
opac/errors/500.pl View File

@@ -21,6 +21,7 @@ use CGI qw ( -utf8 );
use C4::Auth;
use C4::Output;
use C4::Context;
use List::MoreUtils qw(any);

my $query = CGI->new;
my $admin = C4::Context->preference('KohaAdminEmailAddress');
@@ -37,4 +38,8 @@ $template->param (
admin => $admin,
errno => 500,
);
output_with_http_headers $query, $cookie, $template->output, 'html', '500 Internal Server Error';
my $status = '500 Internal Server Error';
if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
$status = '200 OK';
}
output_with_http_headers $query, $cookie, $template->output, 'html', $status;

Loading…
Cancel
Save