blib/lib/Yukki/Error.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/Yukki/Settings.pm ↑ Summary blib/lib/Yukki/Web.pm »
1 package Yukki::Error;
2 {
3 $Yukki::Error::VERSION = '0.140290';
4 }
5 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 with qw( Throwable StackTrace::Auto HTTP::Throwable MooseX::Traits );
8  
9 8 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 exports => {
11 http_throw => \&http_throw,
12 http_exception => \&http_exception,
13 },
14 8 };
15  
16 0 ▶use Yukki::Web::View;
Subroutine BEGIN: called CC=1 SCAR=2.6
17  
18 # ABSTRACT: Yukki's exception class
19  
20  
21 {
22 package Yukki::Error::Fixup;
23 {
24 $Yukki::Error::Fixup::VERSION = '0.140290';
25 }
26 use Moose::Role;
27  
28 around as_psgi => sub {
29 my $next = shift; # not used
30 my ($self, $env) = @_;
31 my $body = $self->body($env);
32 my $headers = $self->build_headers($body, $env);
33 [ $self->status_code, $headers, [ defined $body ? $body : () ] ];
34 };
35 }
36  
37  
38 sub http_exception {
39 my ($class, $name, $args) = @_;
40  
41 return sub {
42 my ($message, $params) = @_;
43 $params //= {};
44  
45 my $status = 'InternalServerError';
46 $status = $params->{status} if defined $params->{status};
47  
48 return $class->with_traits(
49 "HTTP::Throwable::Role::Status::$status",
50 'Yukki::Error::Fixup',
51 )->new($message, $params);
52 };
53 }
54  
55  
56 sub http_throw {
57 my ($class, $name, $args) = @_;
58  
59 my $new_exception = http_exception($class, $name, $args);
60  
61 return sub {
62 my $self = $new_exception->(@_);
63 $self->throw;
64 };
65 }
66  
67  
68 has status => (
69 is => 'ro',
70 isa => 'Str',
71 required => 1,
72 default => 'InternalServerError',
73 );
74  
75  
76 has '+status_code' => ( lazy => 1 );
77 has '+reason' => ( lazy => 1 );
78  
79  
80 sub BUILDARGS {
81 my ($class, $message, $args) = @_;
82 $args //= {};
83  
84 return {
85 %$args,
86 message => $message,
87 };
88 }
89  
90  
91 sub body {
92 my ($self, $env) = @_;
93  
94 my $app = $env->{'yukki.app'};
95 my $view = Yukki::Web::View->new(app => $app);
96 my $ctx = Yukki::Web::Context->new(env => $env);
97  
98 $ctx->response->page_title($self->reason);
99  
100 return $view->render_page(
101 template => 'error.html',
102 context => $ctx,
103 vars => {
104 '#error-page' => $self->message,
105 },
106 );
107 }
108  
109  
110 sub body_headers {
111 my ($self, $body) = @_;
112  
113 return [
114 'Content-type' => 'text/html',
115 'Content-length' => length $body,
116 ];
117 }
118  
119  
120 sub as_string {
121 my $self = shift;
122 return $self->message;
123 }
124  
125 1;
126  
127 __END__
« blib/lib/Yukki/Settings.pm ↑ Summary blib/lib/Yukki/Web.pm »