Skip to content

Commit

Permalink
more error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
miyagawa committed Apr 8, 2010
1 parent 3638fb2 commit 088d98a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 12 additions & 2 deletions lib/Sunaba/Runner.pm
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ sub to_app {
'X-Forwarded-For' => $env->{REMOTE_ADDR},
};

http_get $uri, headers => $hdrs, sub {
http_get $uri, headers => $hdrs, timeout => 3, sub {
my($body, $hdr) = @_;

if ($hdr->{Status} =~ /^[45]/) {
return $respond->([ 502, ["Content-Type", "text/plain"], [ "Bad gateway: $hdr->{Status}" ] ]);
}

my $json = ($body =~ /^sunaba\((.*)\);$/s)[0];
if ($json) {
my $res = JSON::from_json($json);
Expand All @@ -44,7 +49,12 @@ sub to_app {
} elsif ($res->{error}) {
$respond->([ 500, [ "Content-Type", "text/plain" ], [ $res->{error} ] ]);
} else {
$respond->(Storable::thaw(MIME::Base64::decode_base64($res->{stdout})));
my $res = Storable::thaw(MIME::Base64::decode_base64($res->{stdout}));
if (ref $res eq 'ARRAY') {
$respond->($res);
} else {
$respond->([ 500, [ "Content-Type", "text/plain" ], [ "Bad response: $res" ] ]);
}
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion lib/Sunaba/View.pm
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ my $layout = sub {
a(href=>'http://bulknews.typepad.com/') { "Tatsuhiko Miyagawa" };
outs ". Sandbox perl environment is powered by ";
a(href=>'http://colabv6.dan.co.jp/lleval.html') { "Dan Kogai's lleval Ajax API" };
outs " and all restrictions apply. Services can be interrupted, shutdown or blocked at any time at their own will. ";
outs " and all of its restrictions apply. Services can be interrupted, shutdown or blocked at any time at their own will. ";
outs "NO WARRANTY. Use at your own risk.";
}
}
Expand Down

0 comments on commit 088d98a

Please sign in to comment.