Skip to content

Commit

Permalink
[Confirm] Log GraphQL errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
neprune committed Dec 3, 2024
1 parent c7a0236 commit 8112149
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
7 changes: 6 additions & 1 deletion perllib/Integrations/Confirm.pm
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,18 @@ sub perform_request_graphql {
my $body = {
query => $query,
};
my $encoded_body = encode_json($body);

$request->content(encode_json($body));
$request->content($encoded_body);

my $response = $self->ua->request($request);

my $content = decode_json($response->content);

if ($content->{errors} && @{$content->{errors}}) {
$self->logger->warn("Got errors in response to GraphQL query $encoded_body: " . $response->content);
}

return $content;
}

Expand Down
43 changes: 43 additions & 0 deletions t/integrations/confirm.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package Integrations::Confirm::Dummy;
use Path::Tiny;
use Moo;
extends 'Integrations::Confirm';
sub _build_config_file { path(__FILE__)->sibling("confirm.yml")->stringify }

package main;

use strict;
use warnings;

use Test::More;
use Test::MockModule;
use Test::Output;
use HTTP::Response;
use JSON::MaybeXS;

BEGIN { $ENV{TEST_MODE} = 1; }

my $lwp = Test::MockModule->new('LWP::UserAgent');

$lwp->mock(request => sub {
return HTTP::Response->new(200, 'OK', [], encode_json({
data => {},
errors => [
{
message => "Uh-oh spaghettio!",
}
]
})
);
});

my $integration = Integrations::Confirm::Dummy->new;

subtest "GraphQL errors are logged " => sub {
local $ENV{TEST_LOGGER} = 'warn';
stderr_like {
$integration->perform_request_graphql(type => 'job_types');
} qr/.*Uh-oh spaghettio!.*/, 'Got expected warning log for errors.';
};

done_testing;
2 changes: 2 additions & 0 deletions t/integrations/confirm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
graphql_url: "http://example.org/graphql"
graphql_key: "key"

0 comments on commit 8112149

Please sign in to comment.