Skip to content

Commit

Permalink
Handle newly added error fields as of v2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
oklahomer committed Sep 8, 2019
1 parent 1822169 commit af12808
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 36 deletions.
14 changes: 10 additions & 4 deletions lib/Facebook/OpenGraph/Response.pm
Expand Up @@ -92,13 +92,16 @@ sub is_success {
sub error_string {
my $self = shift;

# When error occurs, response should be given in a form of below:
# When an error occurs, the response should be given in a form below:
#{
# "error": {
# "message": "Message describing the error",
# "type": "OAuthException",
# "code": 190 ,
# "error_subcode": 460
# "code": 190,
# "error_subcode": 460,
# "error_user_title": "A title",
# "error_user_msg": "A message",
# "fbtrace_id": "EJplcsCHuLu"
# }
#}
my $error = eval { $self->as_hashref->{error}; };
Expand All @@ -110,11 +113,14 @@ sub error_string {
else {
# sometimes error_subcode is not given
$err_str = sprintf(
'%s:%s %s:%s',
qq{%s:%s\t%s:%s\t%s\t%s:%s},
$error->{code},
$error->{error_subcode} || '-',
$error->{type},
$error->{message},
$error->{fbtrace_id},
$error->{error_user_title} || '-',
$error->{error_user_msg} || '-',
);
}

Expand Down
21 changes: 11 additions & 10 deletions t/002_retrieve/03_fetch.t
Expand Up @@ -32,7 +32,7 @@ subtest 'user' => sub {
},
'args'
);

return (
1,
200,
Expand All @@ -51,7 +51,7 @@ subtest 'user' => sub {
};

subtest 'with fields' => sub {

my $datum_ref = +{
id => 4, # id is always returned even if it's not specified in fields parameter
picture => +{ # returns is_silhouette and url after October 2012 Breaking Changes
Expand All @@ -61,7 +61,7 @@ subtest 'with fields' => sub {
},
},
};

$Mock_furl_http->mock(
request => sub {
my ($mock, %args) = @_;
Expand All @@ -76,7 +76,7 @@ subtest 'with fields' => sub {
},
'args'
);

return (
1,
200,
Expand All @@ -86,7 +86,7 @@ subtest 'with fields' => sub {
);
},
);

my $fb = Facebook::OpenGraph->new;
my $user = $fb->fetch('zuck', +{fields => 'picture'});

Expand All @@ -106,23 +106,24 @@ subtest 'not found' => sub {
['Content-Type' => 'text/javascript; charset=UTF-8'],
encode_json(+{
error => +{
code => 803,
type => 'OAuthException',
message => '(#803) Some of the aliases you requested do not exist: hhhhhhhhhhsssssssss',
code => 803,
type => 'OAuthException',
message => '(#803) Some of the aliases you requested do not exist: hhhhhhhhhhsssssssss',
error_subcode => '',
fbtrace_id => 'EJplcsCHuLu',
},
}),
);

},
);

my $fb = Facebook::OpenGraph->new;
throws_ok(
sub {
my $user = $fb->fetch('hhhhhhhhhhsssssssss');
},
qr/803:- OAuthException:\(#803\) Some of the aliases you requested do not exist: hhhhhhhhhhsssssssss/,
qr/803:-\tOAuthException:\(#803\) Some of the aliases you requested do not exist: hhhhhhhhhhsssssssss\tEJplcsCHuLu\t-:-/,
'user not found',
);

Expand Down
40 changes: 22 additions & 18 deletions t/002_retrieve/10_check_object.t
Expand Up @@ -47,7 +47,7 @@ subtest 'good' => sub {
},
'args',
);

return (
1,
200,
Expand All @@ -67,12 +67,13 @@ subtest 'good' => sub {
};

subtest 'bad app id' => sub {

my $target = 'https://developers.facebook.com/tools/debug/examples/bad_app_id';

my $error_code = 1611016;
my $error_type = 'Exception';
my $error_message = "Object at URL 'https://developers.facebook.com/tools/debug/examples/bad_app_id' of type 'website' is invalid because the given value 'Paul is Awesome' for property 'fb:app_id' could not be parsed as type 'fbid'.";
my $trace_id = 'EJplcsCHuLu';

$Mock_furl_http->mock(
request => sub {
Expand All @@ -90,25 +91,26 @@ subtest 'bad app id' => sub {
},
'args'
);

return (
1,
500,
'Internal Server Error',
['Content-Type' => 'text/javascript; charset=UTF-8'],
encode_json(+{
error => +{
type => $error_type,
message => $error_message,
code => $error_code,
type => $error_type,
message => $error_message,
code => $error_code,
fbtrace_id => $trace_id,
},
}),
);
},
);

my $fb = Facebook::OpenGraph->new;
throws_ok sub { $fb->check_object($target) }, qr/$error_code:- $error_type:$error_message/, 'exception';
throws_ok sub { $fb->check_object($target) }, qr/$error_code:-\t$error_type:$error_message\t$trace_id\t-:-/, 'exception';

};

Expand All @@ -133,7 +135,7 @@ subtest 'bad domain' => sub {
$Mock_furl_http->mock(
request => sub {
my ($self, %args) = @_;

is_deeply(
\%args,
+{
Expand All @@ -147,7 +149,7 @@ subtest 'bad domain' => sub {
},
'args'
);

return (
1,
200, # Isn't it weird that they give 500 for bad app_id and now give us 200?
Expand All @@ -173,6 +175,7 @@ subtest 'bad type' => sub {
my $error_code = 1611007;
my $error_type = 'Exception';
my $error_message = "Object at URL 'https://developers.facebook.com/tools/debug/examples/bad_type' is invalid because the configured 'og:type' of 'paul isn't a type' is invalid.";
my $trace_id = 'EJplcsCHuLu';

$Mock_furl_http->mock(
request => sub {
Expand All @@ -191,31 +194,32 @@ subtest 'bad type' => sub {
},
'args'
);

return (
1,
500,
'Internal Server Error',
['Content-Type' => 'text/javascript; charset=UTF-8'],
encode_json(+{
error => +{
code => $error_code,
type => $error_type,
message => $error_message,
code => $error_code,
type => $error_type,
message => $error_message,
fbtrace_id => $trace_id,
},
}),
);

},
);

my $fb = Facebook::OpenGraph->new;
throws_ok sub { $fb->check_object($target) }, qr/$error_code:- $error_type:$error_message/, 'exception';
throws_ok sub { $fb->check_object($target) }, qr/$error_code:-\t$error_type:$error_message\t$trace_id\t-:-/, 'exception';

};

subtest 'missing property' => sub {

my $target = 'https://developers.facebook.com/tools/debug/examples/missing_property';

my $val = +{
Expand Down Expand Up @@ -243,7 +247,7 @@ subtest 'missing property' => sub {
},
'args'
);

return (
1,
200,
Expand All @@ -263,7 +267,7 @@ subtest 'missing property' => sub {
};

subtest 'invalid property' => sub {

my $target = 'https://developers.facebook.com/tools/debug/examples/invalid_property';

my $val = +{
Expand Down
9 changes: 5 additions & 4 deletions t/002_retrieve/11_appsecret_proof.t
Expand Up @@ -12,7 +12,7 @@ subtest 'w/o appsecret_proof' => sub {
request => sub {
my ($mock, %args) = @_;
is $args{appsecret_proof}, undef, 'appsecret_proof not given';

return (
1,
400,
Expand All @@ -24,6 +24,7 @@ subtest 'w/o appsecret_proof' => sub {
type => 'GraphMethodException',
message => 'API calls from the server require an appsecret_proof argument',
error_subcode => '',
fbtrace_id => 'EJplcsCHuLu',
},
}),
);
Expand All @@ -40,14 +41,14 @@ subtest 'w/o appsecret_proof' => sub {
sub {
my $user = $fb->get('/me');
},
qr/100:- GraphMethodException:API calls from the server require an appsecret_proof argument/,
qr/100:-\tGraphMethodException:API calls from the server require an appsecret_proof argument\tEJplcsCHuLu\t-:-/,
'appsecret_proof is required',
);

};

subtest 'w/ appsecret_proof' => sub {

my $datum_ref = +{
name => 'Mark Zuckerberg', # full name
id => 4, # id 1-3 were test users
Expand Down Expand Up @@ -81,7 +82,7 @@ subtest 'w/ appsecret_proof' => sub {
secret => 'TheKingHasEarsShapedLikeADonkey',
});
my $user = $fb->get('/me');

is_deeply $datum_ref, $user, 'datum';
};

Expand Down
43 changes: 43 additions & 0 deletions t/004_response/01_basic.t
Expand Up @@ -2,6 +2,7 @@ use strict;
use warnings;
use Test::More;
use Facebook::OpenGraph::Response;
use JSON 2 qw(encode_json);

subtest 'initialize' => sub {
my $res = Facebook::OpenGraph::Response->new;
Expand Down Expand Up @@ -128,4 +129,46 @@ subtest 'is_api_version_eq_or_later_than' => sub {
ok(!$res->is_api_version_eq_or_later_than('v3.3'));
};

subtest 'error_string' => sub {
# Sample value is taken from https://developers.facebook.com/docs/graph-api/using-graph-api/error-handling
my $message = 'Message describing the error';
my $type = 'OAuthException';
my $code = 190;
my $subcode = 460;
my $user_title = 'A title';
my $user_msg = 'A message';
my $trace_id = 'EJplcsCHuLu';

my $error = +{
message => $message,
type => $type,
code => $code,
error_subcode => $subcode,
error_user_title => $user_title,
error_user_msg => $user_msg,
fbtrace_id => $trace_id,
};
my $res = Facebook::OpenGraph::Response->new(+{
code => 500,
message => 'Internal Server Error',
headers => [],
req_headers => [],
req_content => '',
content => encode_json(+{ error => $error }),
});

my $expected_str = sprintf(
qq{%s:%s\t%s:%s\t%s\t%s:%s},
$code,
$subcode,
$type,
$message,
$trace_id,
$user_title,
$user_msg,
);

is($res->error_string, $expected_str);
};

done_testing;

0 comments on commit af12808

Please sign in to comment.