Skip to content

Commit

Permalink
catch decoding excpetions
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Dec 4, 2011
1 parent ab4eed6 commit 39a14db
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions lib/Plack/Middleware/OAuth/Handler/AccessTokenV2.pm
Expand Up @@ -4,6 +4,7 @@ use URI;
use URI::Query;
use LWP::UserAgent;
use Plack::Middleware::OAuth::AccessToken;
use Try::Tiny;
use JSON::Any;
use warnings;
use strict;
Expand Down Expand Up @@ -42,33 +43,38 @@ sub get_access_token {
# process response content...
my $response_content = $ua_response->content;
my $content_type = $ua_response->header('Content-Type');
my $atkn;

if( $content_type =~ m{json} || $content_type =~ m{javascript} || $response_content =~ m{^\{.*?\}}s ) {

my $params = JSON::Any->new->decode( $response_content );
$atkn = Plack::Middleware::OAuth::AccessToken->new(
version => $config->{version}, # oauth version
provider => $provider,
params => {
%$params,
code => $code,
}
);

} else {
my $qq = URI::Query->new( $ua_response->content );
my %params = $qq->hash;
$atkn = Plack::Middleware::OAuth::AccessToken->new(
version => $config->{version}, # oauth version
provider => $provider,
params => {
%params,
code => $code
}
);
my %params;

# we are pretty sure, the response is json format
if( $content_type =~ m{json}
|| $content_type =~ m{javascript}
|| $response_content =~ m{^\{.*?\}\s*$}s )
{
try {
%params = %{ JSON::Any->new->decode( $response_content ) }; # should be hashref.
} catch {
# XXX: show exception page for this.
die "Can not decode json: " . $_;
};
}
else {
try {
my $qq = URI::Query->new( $ua_response->content );
%params = $qq->hash;
} catch {
# XXX: show exception page for this.
die "Can not decode params: " . $_;
}
}
return $atkn;

return Plack::Middleware::OAuth::AccessToken->new(
version => $config->{version}, # oauth version
provider => $provider,
params => {
%params,
code => $code,
}
);
}

sub run {
Expand Down

0 comments on commit 39a14db

Please sign in to comment.