| 1 | # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*- | ||||||||||||||||||||||||||||||
| 2 | # vim:ts=8:sw=2:et:sta:sts=2 | ||||||||||||||||||||||||||||||
| 3 | ######### | ||||||||||||||||||||||||||||||
| 4 | # Author: rmp | ||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||
| 6 | package ClearPress::view::error; | ||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use base qw(ClearPress::view Class::Accessor); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use English qw(-no_match_vars); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Template; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use Readonly; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | 6 | __PACKAGE__->mk_accessors(qw(errstr)); | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||
| 17 | 6 | our $VERSION = q[2026.07.25]; | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||
| 19 | 6 | Readonly::Scalar our $CODEMAP => { | |||||||||||||||||||||||||||||
| 20 | 300 => q[Multiple Choices], | ||||||||||||||||||||||||||||||
| 21 | 301 => q[Moved Permanently], | ||||||||||||||||||||||||||||||
| 22 | 302 => q[Found], | ||||||||||||||||||||||||||||||
| 23 | 303 => q[See Other], | ||||||||||||||||||||||||||||||
| 24 | 304 => q[Not Modified], | ||||||||||||||||||||||||||||||
| 25 | 306 => q[Switch Proxy], | ||||||||||||||||||||||||||||||
| 26 | 307 => q[Temporary Redirect], | ||||||||||||||||||||||||||||||
| 27 | 308 => q[Resume Incomplete], | ||||||||||||||||||||||||||||||
| 28 | 400 => q[Bad Request], | ||||||||||||||||||||||||||||||
| 29 | 401 => q[Unauthorised], | ||||||||||||||||||||||||||||||
| 30 | 402 => q[Payment Required], | ||||||||||||||||||||||||||||||
| 31 | 403 => q[Forbidden], | ||||||||||||||||||||||||||||||
| 32 | 404 => q[Not Found], | ||||||||||||||||||||||||||||||
| 33 | 405 => q[Method Not Allowed], | ||||||||||||||||||||||||||||||
| 34 | 406 => q[Not Acceptable], | ||||||||||||||||||||||||||||||
| 35 | 407 => q[Proxy Authentication Required], | ||||||||||||||||||||||||||||||
| 36 | 408 => q[Request Timeout], | ||||||||||||||||||||||||||||||
| 37 | 409 => q[Conflict], | ||||||||||||||||||||||||||||||
| 38 | 410 => q[Gone], | ||||||||||||||||||||||||||||||
| 39 | 411 => q[Length Required], | ||||||||||||||||||||||||||||||
| 40 | 412 => q[Precondition Failed], | ||||||||||||||||||||||||||||||
| 41 | 413 => q[Request Entity Too Large], | ||||||||||||||||||||||||||||||
| 42 | 414 => q[Request-URI Too Long], | ||||||||||||||||||||||||||||||
| 43 | 415 => q[Unsupported Media Type], | ||||||||||||||||||||||||||||||
| 44 | 416 => q[Requested Range Not Satisfiable], | ||||||||||||||||||||||||||||||
| 45 | 417 => q[Expectation Failed], | ||||||||||||||||||||||||||||||
| 46 | 500 => q[Internal Server Error], | ||||||||||||||||||||||||||||||
| 47 | 501 => q[Not Implemented], | ||||||||||||||||||||||||||||||
| 48 | 502 => q[Bad Gateway], | ||||||||||||||||||||||||||||||
| 49 | 503 => q[Service Unavailable], | ||||||||||||||||||||||||||||||
| 50 | 504 => q[Gateway Timeout], | ||||||||||||||||||||||||||||||
| 51 | 505 => q[HTTP Version Not Supported], | ||||||||||||||||||||||||||||||
| 52 | 511 => q[Network Authentication Required], | ||||||||||||||||||||||||||||||
| 53 | }; | ||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||
| 55 | 2 | ▶ | sub safe_errors { | ||||||||||||||||||||||||||||
|
Subroutine
safe_errors: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 56 | 2 | return 1; | |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | sub init { | ||||||||||||||||||||||||||||
|
Subroutine
init: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 60 | 2 | my $self = shift; | |||||||||||||||||||||||||||||
| 61 | 2 | my $util = $self->util; | |||||||||||||||||||||||||||||
| 62 | 2 | my $cgi = $util->cgi; | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||
| 64 | 2 | ▶ | $self->{errstr} = $cgi->unescape($cgi->param('errstr') || q[]) || q[]; | ||||||||||||||||||||||||||||
|
Condition100%4/4
$cgi->unescape($cgi->param('errstr') || '') || ''
$cgi->param('errstr') || ''
MC/DC100%1/1
$cgi->unescape($cgi->param('errstr') || '') || ''
$cgi->unescape($cgi->param('errstr') || '')
MC/DC100%1/1
$cgi->param('errstr') || ''
$cgi->param('errstr')
Truth table100%2/2
A
A= $cgi->unescape($cgi->param('errstr') || '')
Truth table100%2/2
A
A= $cgi->param('errstr')
| |||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||
| 66 | 2 | return $self->SUPER::init(); | |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||
| 69 | 2 | ▶ | sub render { | ||||||||||||||||||||||||||||
|
Subroutine
render: called CC=13 SCAR=28.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 70 | 2 | my $self = shift; | |||||||||||||||||||||||||||||
| 71 | 2 | my $util = $self->util; | |||||||||||||||||||||||||||||
| 72 | 2 | my $cgi = $util->cgi; | |||||||||||||||||||||||||||||
| 73 | 2 | ▶ | my $aspect = $self->aspect() || q[]; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$self->aspect || ''
MC/DC0%0/1
$self->aspect || ''
$self->aspect
Truth table50%1/2
A
A= $self->aspect
| |||||||||||||||||||||||||||||||
| 74 | 2 | my $errstr = $self->errstr; | |||||||||||||||||||||||||||||
| 75 | 2 | ▶ | my $pi = $ENV{PATH_INFO} || q[]; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{'PATH_INFO'} || ''
MC/DC0%0/1
$ENV{'PATH_INFO'} || ''
$ENV{'PATH_INFO'}
Truth table50%1/2
A
A= $ENV{'PATH_INFO'}
| |||||||||||||||||||||||||||||||
| 76 | 2 | my ($code) = $pi =~ m{(\d+)}smix; # Requires Apache ErrorDocument /<application>/<errorcode>. mod_perl can use $ENV{REDIRECT_STATUS} but doesn't work under cgi | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||
| 78 | 2 | ▶ | $errstr ||= $CODEMAP->{$code||q[]}; | ||||||||||||||||||||||||||||
|
Condition60%3/5
$errstr ||= $$ClearPress::view::error::CODEMAP{$code or ""}
$code || ''
MC/DC50%1/2
$errstr ||= $$ClearPress::view::error::CODEMAP{$code or ""}
$errstr
$$ClearPress::view::error::CODEMAP{$code or ""}
MC/DC0%0/1
$code || ''
$code
Truth table66%2/3
A ||= B
A= $errstrB= $$ClearPress::view::error::CODEMAP{$code or ""}
Truth table50%1/2
A
A= $code
| |||||||||||||||||||||||||||||||
| 79 | 2 | ▶ | $errstr ||= q[]; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$errstr ||= ""
MC/DC100%1/1
$errstr ||= ""
$errstr
Truth table100%2/2
A
A= $errstr
| |||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||
| 81 | 2 | ▶ | if(Template->error()) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 82 | 0 | $errstr .= q(Template Error: ) . Template->error(); | |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||
| 85 | 2 | ▶ | if($self->safe_errors) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 86 | 2 | ▶ | print {*STDERR} "Serving error: $errstr\n" or croak $ERRNO; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 87 | 2 | $errstr =~ s/[ ]at[ ]\S+[ ]line[ ][[:digit:]]+//smxg; | |||||||||||||||||||||||||||||
| 88 | 2 | $errstr =~ s/\s+[.]?$//smx; | |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||
| 91 | ######### | ||||||||||||||||||||||||||||||
| 92 | # initialise tt_filters by resetting tt | ||||||||||||||||||||||||||||||
| 93 | # | ||||||||||||||||||||||||||||||
| 94 | 2 | delete $util->{tt}; | |||||||||||||||||||||||||||||
| 95 | 2 | my $tt = $self->tt; | |||||||||||||||||||||||||||||
| 96 | 2 | my $content = q[]; | |||||||||||||||||||||||||||||
| 97 | 2 | my $decor = $self->decorator; | |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||
| 99 | # carp qq[$self view::error: handling error response]; | ||||||||||||||||||||||||||||||
| 100 | 2 | ▶ | if($aspect =~ /(?:ajax|xml|rss|atom)$/smx) { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 101 | 0 | my $escaped = $self->tt_filters->{xml_entity}->($errstr); | |||||||||||||||||||||||||||||
| 102 | 0 | $content = qq[<?xml version='1.0'?>\n<error>Error: $escaped</error>]; | |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||
| 104 | } elsif($aspect =~ /json$/smx) { | ||||||||||||||||||||||||||||||
| 105 | 0 | my $escaped = $self->tt_filters->{js_string}->($errstr); | |||||||||||||||||||||||||||||
| 106 | 0 | $content = qq[{"error":"Error: $escaped"}]; | |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||
| 108 | } else { | ||||||||||||||||||||||||||||||
| 109 | 2 | my $escaped = $self->tt_filters->{xml_entity}->($errstr); | |||||||||||||||||||||||||||||
| 110 | 2 | ▶ | my $message = $CODEMAP->{$code||q[]} || q[An Error Occurred]; | ||||||||||||||||||||||||||||
|
Condition75%3/4
$$ClearPress::view::error::CODEMAP{$code or ''} || 'An Error Occurred'
$code || ''
MC/DC0%0/1
$$ClearPress::view::error::CODEMAP{$code or ''} || 'An Error Occurred'
$$ClearPress::view::error::CODEMAP{$code or ''}
MC/DC100%1/1
$code || ''
$code
Truth table50%1/2
A
A= $$ClearPress::view::error::CODEMAP{$code or ''}
Truth table100%2/2
A
A= $code
| |||||||||||||||||||||||||||||||
| 111 | 2 | $content = sprintf <<'EOT', $message, $self->actions(), $escaped; | |||||||||||||||||||||||||||||
| 112 | <div id="main"> | ||||||||||||||||||||||||||||||
| 113 | <h2 class="error">%s</h2> | ||||||||||||||||||||||||||||||
| 114 | %s | ||||||||||||||||||||||||||||||
| 115 | <p class="error">Error: %s</p> | ||||||||||||||||||||||||||||||
| 116 | </div> | ||||||||||||||||||||||||||||||
| 117 | EOT | ||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||
| 120 | ######### | ||||||||||||||||||||||||||||||
| 121 | # render should return content for non-streamed responses | ||||||||||||||||||||||||||||||
| 122 | # | ||||||||||||||||||||||||||||||
| 123 | 2 | return $content; | |||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||
| 126 | 6 | 1; | |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||
| 128 | __END__ | ||||||||||||||||||||||||||||||