Skip to content

Instantly share code, notes, and snippets.

@hisaichi5518
Created March 14, 2012 06:10
Show Gist options
  • Select an option

  • Save hisaichi5518/2034515 to your computer and use it in GitHub Desktop.

Select an option

Save hisaichi5518/2034515 to your computer and use it in GitHub Desktop.
こういう感じでMaltsのto_appを上書きすればいい気がする。
package MyApp::Web;
use 5.10.1;
use strict;
use warnings;
use parent qw/Malts Malts::Web/;
use Log::Minimal;
# ...略
sub to_app {
my ($self) = @_;
return sub {
my ($env) = @_;
local $Log::Minimal::DIE = \&_throw;
my $res = eval { $self->SUPER::to_app->($env) };
return _catch_error($@) if $@;
return $res;
};
}
sub _throw {
my ($time, $type, $message, $trace, $raw_message) = @_;
die {
code => 500,
message => 'Unknown Error!',
time => $time,
type => $type,
trace => $trace,
(ref $raw_message ne 'HASH'
? (message => $raw_message)
: %$raw_message),
};
}
sub _catch_error {
my ($e) = @_;
if (ref $e ne 'HASH') {
$e = {
code => 500,
message => $e,
};
}
my $c = Malts->context;
my $res = ...; # レスポンスオブジェクトを作る。
return $res->finalize;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment