| 1 | package PAGI::Middleware::ErrorHandler; | ||||||||||||||||||||||||||||
| 2 | 1 | $PAGI::Middleware::ErrorHandler::VERSION = '0.002002'; | |||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use parent 'PAGI::Middleware'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | PAGI::Middleware::ErrorHandler - Exception handling middleware | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | use PAGI::Middleware::Builder; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | my $app = builder { | ||||||||||||||||||||||||||||
| 18 | enable 'ErrorHandler', | ||||||||||||||||||||||||||||
| 19 | development => 1, | ||||||||||||||||||||||||||||
| 20 | on_error => sub { | ||||||||||||||||||||||||||||
| 21 | my ($error) = @_; warn "App error: $error" }; | ||||||||||||||||||||||||||||
| 22 | $my_app; | ||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | PAGI::Middleware::ErrorHandler catches exceptions thrown by the inner | ||||||||||||||||||||||||||||
| 28 | application and converts them to appropriate HTTP error responses. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =over 4 | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =item * development (default: 0) | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | If true, include stack trace in error responses. Should be false in production. | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =item * on_error (default: undef) | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | Callback invoked with the error when an exception is caught. Useful for logging. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | on_error => sub { | ||||||||||||||||||||||||||||
| 43 | my ($error) = @_; $logger->error($error) } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =item * content_type (default: 'text/html') | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | Content type for error responses. Supported: 'text/html', 'application/json', 'text/plain' | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =item * status (default: 500) | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | HTTP status code for general exceptions. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =back | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =cut | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 10 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 58 | 10 | my ($self, $config) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 10 | ▶ | $self->{development} = $config->{development} // 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"development"} // 0
MC/DC100%1/1
$config->{"development"} // 0
$config->{"development"}
Truth table100%2/2
A
A= $config->{"development"}
| |||||||||||||||||||||||||||||
| 61 | 10 | $self->{on_error} = $config->{on_error}; | |||||||||||||||||||||||||||
| 62 | 10 | ▶ | $self->{content_type} = $config->{content_type} // 'text/html'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"content_type"} // "text/html"
MC/DC100%1/1
$config->{"content_type"} // "text/html"
$config->{"content_type"}
Truth table100%2/2
A
A= $config->{"content_type"}
| |||||||||||||||||||||||||||||
| 63 | 10 | ▶ | $self->{status} = $config->{status} // 500; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"status"} // 500
MC/DC0%0/1
$config->{"status"} // 500
$config->{"status"}
Truth table50%1/2
A
A= $config->{"status"}
| |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 10 | ▶ | sub wrap { | ||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 10 | my ($self, $app) = @_; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 10 | ▶ | return async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||
| 70 | 10 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||
| 71 | # Only handle HTTP requests | ||||||||||||||||||||||||||||
| 72 | 10 | ▶ | if ($scope->{type} ne 'http') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 1 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 74 | 0 | return; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 9 | my $response_started = 0; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Intercept send to track if response has started | ||||||||||||||||||||||||||||
| 80 | 2 | ▶ | my $wrapped_send = async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 81 | 2 | my ($event) = @_; | |||||||||||||||||||||||||||
| 82 | 2 | ▶ | if ($event->{type} eq 'http.response.start') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 1 | $response_started = 1; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 2 | await $send->($event); | |||||||||||||||||||||||||||
| 86 | 9 | }; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # Try to run the app | ||||||||||||||||||||||||||||
| 89 | 9 | my $error; | |||||||||||||||||||||||||||
| 90 | eval { | ||||||||||||||||||||||||||||
| 91 | 9 | await $app->($scope, $receive, $wrapped_send); | |||||||||||||||||||||||||||
| 92 | 1 | 1; | |||||||||||||||||||||||||||
| 93 | 9 | ▶ | } or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 8 | ▶ | $error = $@ || 'Unknown error'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$@ || 'Unknown error'
MC/DC0%0/1
$@ || 'Unknown error'
$@
Truth table50%1/2
A
A= $@
| |||||||||||||||||||||||||||||
| 95 | }; | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # Handle error if one occurred | ||||||||||||||||||||||||||||
| 98 | 9 | ▶ | if ($error) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | # Call on_error callback if provided | ||||||||||||||||||||||||||||
| 100 | 8 | ▶ | if ($self->{on_error}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 1 | eval { $self->{on_error}->($error) }; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # If response already started, we can't send error page | ||||||||||||||||||||||||||||
| 105 | 8 | ▶ | if ($response_started) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | # Best we can do is log and close | ||||||||||||||||||||||||||||
| 107 | 0 | warn "Error occurred after response started: $error\n"; | |||||||||||||||||||||||||||
| 108 | 0 | return; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # Determine status code | ||||||||||||||||||||||||||||
| 112 | 8 | my $status = $self->{status}; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Check for specific exception types | ||||||||||||||||||||||||||||
| 115 | 8 | ▶ | if (blessed($error) && $error->can('status_code')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($error) and $error->can("status_code")
MC/DC50%1/2
builtin::blessed($error) and $error->can("status_code")
builtin::blessed($error)
$error->can("status_code")
Truth table66%2/3
A and B
A= builtin::blessed($error)B= $error->can("status_code")
| |||||||||||||||||||||||||||||
| 116 | 1 | $status = $error->status_code; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # Generate error response | ||||||||||||||||||||||||||||
| 120 | 8 | my ($body, $content_type) = $self->_generate_error_body($error, $status); | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 8 | await $send->({ | |||||||||||||||||||||||||||
| 123 | type => 'http.response.start', | ||||||||||||||||||||||||||||
| 124 | status => $status, | ||||||||||||||||||||||||||||
| 125 | headers => [ | ||||||||||||||||||||||||||||
| 126 | ['content-type', $content_type], | ||||||||||||||||||||||||||||
| 127 | ['content-length', length($body)], | ||||||||||||||||||||||||||||
| 128 | ], | ||||||||||||||||||||||||||||
| 129 | }); | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 8 | await $send->({ | |||||||||||||||||||||||||||
| 132 | type => 'http.response.body', | ||||||||||||||||||||||||||||
| 133 | body => $body, | ||||||||||||||||||||||||||||
| 134 | more => 0, | ||||||||||||||||||||||||||||
| 135 | }); | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 10 | }; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 8 | ▶ | sub _generate_error_body { | ||||||||||||||||||||||||||
|
Subroutine
_generate_error_body: called CC=10 SCAR=23.3
| |||||||||||||||||||||||||||||
| 141 | 8 | my ($self, $error, $status) = @_; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 8 | my $error_text = "$error"; | |||||||||||||||||||||||||||
| 144 | 8 | my $content_type = $self->{content_type}; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # Clean up error for display | ||||||||||||||||||||||||||||
| 147 | 8 | my $display_error = $error_text; | |||||||||||||||||||||||||||
| 148 | 8 | ▶ | unless ($self->{development}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | # In production, don't reveal internal details | ||||||||||||||||||||||||||||
| 150 | 5 | $display_error = $self->_status_message($status); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 8 | ▶ | if ($content_type eq 'application/json') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 154 | 1 | require JSON::MaybeXS; | |||||||||||||||||||||||||||
| 155 | my $body = JSON::MaybeXS::encode_json({ | ||||||||||||||||||||||||||||
| 156 | error => $display_error, | ||||||||||||||||||||||||||||
| 157 | status => $status, | ||||||||||||||||||||||||||||
| 158 | 1 | ▶ | ($self->{development} ? (stack => $error_text) : ()), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | }); | ||||||||||||||||||||||||||||
| 160 | 1 | return ($body, 'application/json'); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | elsif ($content_type eq 'text/plain') { | ||||||||||||||||||||||||||||
| 163 | 1 | my $body = "Error $status: $display_error"; | |||||||||||||||||||||||||||
| 164 | 1 | ▶ | if ($self->{development} && $error_text ne $display_error) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"development"} and $error_text ne $display_error
MC/DC0%0/2
$self->{"development"} and $error_text ne $display_error
$self->{"development"}
$error_text ne $display_error
Truth table33%1/3
A and B
A= $self->{"development"}B= $error_text ne $display_error
| |||||||||||||||||||||||||||||
| 165 | 0 | $body .= "\n\nStack trace:\n$error_text"; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | 1 | return ($body, 'text/plain; charset=utf-8'); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | else { | ||||||||||||||||||||||||||||
| 170 | # Default to HTML | ||||||||||||||||||||||||||||
| 171 | 6 | my $safe_error = $self->_html_escape($display_error); | |||||||||||||||||||||||||||
| 172 | 6 | ▶ | my $safe_stack = $self->{development} ? $self->_html_escape($error_text) : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 6 | my $body = <<"HTML"; | |||||||||||||||||||||||||||
| 175 | <!DOCTYPE html> | ||||||||||||||||||||||||||||
| 176 | <html> | ||||||||||||||||||||||||||||
| 177 | <head> | ||||||||||||||||||||||||||||
| 178 | <title>Error $status</title> | ||||||||||||||||||||||||||||
| 179 | <style> | ||||||||||||||||||||||||||||
| 180 | body { font-family: -apple-system, BlinkMacSystemFont, sans-serif; margin: 40px; } | ||||||||||||||||||||||||||||
| 181 | h1 { color: #c00; } | ||||||||||||||||||||||||||||
| 182 | .error { background: #fee; padding: 20px; border-radius: 4px; margin: 20px 0; } | ||||||||||||||||||||||||||||
| 183 | pre { background: #f4f4f4; padding: 15px; overflow-x: auto; border-radius: 4px; } | ||||||||||||||||||||||||||||
| 184 | </style> | ||||||||||||||||||||||||||||
| 185 | </head> | ||||||||||||||||||||||||||||
| 186 | <body> | ||||||||||||||||||||||||||||
| 187 | <h1>Error $status</h1> | ||||||||||||||||||||||||||||
| 188 | <div class="error">$safe_error</div> | ||||||||||||||||||||||||||||
| 189 | HTML | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 6 | ▶ | if ($self->{development} && $safe_stack) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"development"} and $safe_stack
MC/DC50%1/2
$self->{"development"} and $safe_stack
$self->{"development"}
$safe_stack
Truth table66%2/3
A and B
A= $self->{"development"}B= $safe_stack
| |||||||||||||||||||||||||||||
| 192 | 2 | $body .= " <h2>Stack Trace</h2>\n <pre>$safe_stack</pre>\n"; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 6 | $body .= "</body>\n</html>\n"; | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 6 | return ($body, 'text/html; charset=utf-8'); | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 8 | ▶ | sub _html_escape { | ||||||||||||||||||||||||||
|
Subroutine
_html_escape: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 202 | 8 | my ($self, $text) = @_; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 8 | $text =~ s/&/&/g; | |||||||||||||||||||||||||||
| 205 | 8 | $text =~ s/</</g; | |||||||||||||||||||||||||||
| 206 | 8 | $text =~ s/>/>/g; | |||||||||||||||||||||||||||
| 207 | 8 | $text =~ s/"/"/g; | |||||||||||||||||||||||||||
| 208 | 8 | return $text; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 5 | ▶ | sub _status_message { | ||||||||||||||||||||||||||
|
Subroutine
_status_message: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 212 | 5 | my ($self, $status) = @_; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 5 | my %messages = ( | |||||||||||||||||||||||||||
| 215 | 400 => 'Bad Request', | ||||||||||||||||||||||||||||
| 216 | 401 => 'Unauthorized', | ||||||||||||||||||||||||||||
| 217 | 403 => 'Forbidden', | ||||||||||||||||||||||||||||
| 218 | 404 => 'Not Found', | ||||||||||||||||||||||||||||
| 219 | 405 => 'Method Not Allowed', | ||||||||||||||||||||||||||||
| 220 | 408 => 'Request Timeout', | ||||||||||||||||||||||||||||
| 221 | 413 => 'Payload Too Large', | ||||||||||||||||||||||||||||
| 222 | 429 => 'Too Many Requests', | ||||||||||||||||||||||||||||
| 223 | 500 => 'Internal Server Error', | ||||||||||||||||||||||||||||
| 224 | 502 => 'Bad Gateway', | ||||||||||||||||||||||||||||
| 225 | 503 => 'Service Unavailable', | ||||||||||||||||||||||||||||
| 226 | 504 => 'Gateway Timeout', | ||||||||||||||||||||||||||||
| 227 | ); | ||||||||||||||||||||||||||||
| 228 | 5 | ▶ | return $messages{$status} // 'Error'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$messages{$status} // "Error"
MC/DC0%0/1
$messages{$status} // "Error"
$messages{$status}
Truth table50%1/2
A
A= $messages{$status}
| |||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 1 | 1; | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | __END__ | ||||||||||||||||||||||||||||