line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::TextExceptions; |
2
|
2
|
|
|
2
|
|
1370
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
14
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
sub register { |
7
|
2
|
|
|
2
|
1
|
76
|
my ($self, $app, $config) = @_; |
8
|
2
|
|
66
|
|
|
14
|
my $ua_re = $config->{ua_re} || qr{^(?:Mojolicious|curl|Wget)}; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
$app->hook( |
11
|
|
|
|
|
|
|
before_render => sub { |
12
|
12
|
|
|
12
|
|
171629
|
my ($c, $args) = @_; |
13
|
12
|
|
100
|
|
|
35
|
my $format = $c->stash('format') // 'html'; |
14
|
|
|
|
|
|
|
|
15
|
12
|
100
|
|
|
|
161
|
return unless $format eq 'html'; # Do not want to take over API responses |
16
|
10
|
50
|
|
|
|
31
|
return unless my $template = $args->{template}; |
17
|
10
|
100
|
|
|
|
29
|
return unless $template eq 'exception'; |
18
|
|
|
|
|
|
|
|
19
|
4
|
100
|
|
|
|
19
|
if ($c->req->headers->user_agent =~ $ua_re) { |
20
|
2
|
|
|
|
|
100
|
@$args{qw(text format)} = ($c->stash->{exception}, 'txt'); |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
} |
23
|
2
|
|
|
|
|
19
|
); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
1; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=encoding utf8 |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 NAME |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
Mojolicious::Plugin::TextExceptions - Render exceptions as text in command line user agents |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 SYNOPSIS |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
use Mojolicious::Lite; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Only enable this plugin when running tests |
39
|
|
|
|
|
|
|
plugin 'TextExceptions' if $ENV{HARNESS_ACTIVE}; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Only enable this plugin when developing |
42
|
|
|
|
|
|
|
plugin 'TextExceptions' if app->mode eq 'development'; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Always enabling the plugin can leak sensitive information |
45
|
|
|
|
|
|
|
# to the end user |
46
|
|
|
|
|
|
|
plugin 'TextExceptions'; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
plugin 'TextExceptions', ua_re => qr{^LWP}; # Override the default regex for user agent |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 DESCRIPTION |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
This plugin looks for curl/wget/mojo user agent and renders exceptions as text instead of html. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head1 METHODS |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head2 register |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
Sets up a before_render hook to look for text based user agents and render exceptions as text. |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Currently supports Mojo::UserAgent, curl and wget |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 SEE ALSO |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
L |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Copyright (C) 2019, Marcus Ramberg |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
This program is free software, you can redistribute it and/or modify it under |
71
|
|
|
|
|
|
|
the terms of the Artistic License version 2.0. |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head1 AUTHOR |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Marcus Ramberg |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 CONTRIBUTORS |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Jan Henning Thorsen |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=cut |