Skip to content

Commit

Permalink
use Try::Tiny
Browse files Browse the repository at this point in the history
  • Loading branch information
nothingmuch committed Oct 14, 2009
1 parent 5232abc commit 6c65859
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 19 deletions.
1 change: 1 addition & 0 deletions Makefile.PL
Expand Up @@ -16,6 +16,7 @@ requires 'URI';
requires 'HTTP::Request::AsCGI', 0.9;
requires 'Pod::Usage'; # plackup
requires 'File::ShareDir'; # Plack::Test::Suite
requires 'Try::Tiny';

feature 'Fast XS-based HTTP header parsing',
'HTTP::Parser::XS', 0.03;
Expand Down
3 changes: 2 additions & 1 deletion lib/HTTP/Message/PSGI.pm
Expand Up @@ -6,14 +6,15 @@ our @EXPORT = qw( req_to_psgi res_from_psgi );

use Carp ();
use URI::Escape ();
use Try::Tiny;

my $TRUE = (1 == 1);
my $FALSE = !$TRUE;

sub req_to_psgi {
my $req = shift;

unless (eval { $req->isa('HTTP::Request') }) {
unless (try { $req->isa('HTTP::Request') }) {
Carp::croak("Request is not HTTP::Request: $req");
}

Expand Down
13 changes: 11 additions & 2 deletions lib/Plack/Builder.pm
Expand Up @@ -16,8 +16,17 @@ sub add_middleware {

if (ref $mw ne 'CODE') {
my $mw_class = $mw;
eval "use $mw_class";
die $@ if $@;

my ( $failed, $error );

do {
local $@;
$failed = not eval "use $mw_class; 1";
$error = $@;
};

die $error if $failed;

$mw = sub { $mw_class->wrap($_[0], @args) };
}

Expand Down
5 changes: 3 additions & 2 deletions lib/Plack/HTTPParser.pm
Expand Up @@ -4,9 +4,10 @@ use base qw(Exporter);

our @EXPORT = qw( parse_http_request );

use Try::Tiny;

{
local $@;
if (!$ENV{PLACK_HTTP_PARSER_PP} && eval { require HTTP::Parser::XS; 1 }) {
if (!$ENV{PLACK_HTTP_PARSER_PP} && try { require HTTP::Parser::XS; 1 }) {
*parse_http_request = \&HTTP::Parser::XS::parse_http_request;
} else {
require Plack::HTTPParser::PP;
Expand Down
8 changes: 3 additions & 5 deletions lib/Plack/Middleware/StackTrace.pm
Expand Up @@ -6,11 +6,12 @@ use Plack;
use Plack::Util;
use Devel::StackTrace;
use Devel::StackTrace::AsHTML;
use Try::Tiny;

our $StackTraceClass = "Devel::StackTrace";

# Optional since it needs PadWalker
if (eval { require Devel::StackTrace::WithLexicals; 1 }) {
if (try { require Devel::StackTrace::WithLexicals; 1 }) {
$StackTraceClass = "Devel::StackTrace::WithLexicals";
}

Expand All @@ -23,10 +24,7 @@ sub call {
die @_;
};

my $res = do {
local $@;
eval { $self->app->($env) };
};
my $res = try { $self->app->($env) };

if (!$res && $trace) {
my $body = $trace->as_html;
Expand Down
6 changes: 2 additions & 4 deletions lib/Plack/Server/Standalone.pm
Expand Up @@ -13,14 +13,12 @@ use Plack::Middleware::ContentLength;
use POSIX qw(EINTR);
use Socket qw(IPPROTO_TCP TCP_NODELAY);
use Time::HiRes qw(alarm time);
use Try::Tiny;

use constant MAX_REQUEST_SIZE => 131072;
use constant MSWin32 => $^O eq 'MSWin32';

our $HasSendFile = !$ENV{PLACK_NO_SENDFILE} && do {
local $@;
eval { require Sys::Sendfile; 1 };
};
our $HasSendFile = !$ENV{PLACK_NO_SENDFILE} && try { require Sys::Sendfile; 1 };

sub new {
my($class, %args) = @_;
Expand Down
3 changes: 2 additions & 1 deletion lib/Plack/Test/Suite.pm
Expand Up @@ -11,8 +11,9 @@ use Test::TCP;
use Plack::Loader;
use Plack::Middleware::Lint;
use Plack::Util;
use Try::Tiny;

my $share_dir = eval { File::ShareDir::dist_dir('Plack') } || 'share';
my $share_dir = try { File::ShareDir::dist_dir('Plack') } || 'share';

# 0: test name
# 1: request generator coderef.
Expand Down
8 changes: 4 additions & 4 deletions lib/Plack/Util.pm
Expand Up @@ -3,6 +3,7 @@ use strict;
use Carp ();
use Scalar::Util;
use IO::Handle;
use Try::Tiny;

sub TRUE() { 1==1 }
sub FALSE() { !TRUE }
Expand Down Expand Up @@ -104,14 +105,13 @@ sub load_psgi {
sub run_app($$) {
my($app, $env) = @_;

local $@; my $res = eval { $app->($env) };
if ($@) {
return try {
$app->($env);
} catch {
my $body = "Internal Server Error";
$env->{'psgi.errors'}->print($@);
return [ 500, [ 'Content-Type' => 'text/plain', 'Content-Length' => length($body) ], [ $body ] ];
}

return $res;
}

sub headers {
Expand Down

0 comments on commit 6c65859

Please sign in to comment.