diff --git a/bin/ngcp-credit-warning b/bin/ngcp-credit-warning index 9f6eedc..f2bb211 100755 --- a/bin/ngcp-credit-warning +++ b/bin/ngcp-credit-warning @@ -118,7 +118,7 @@ sub get_data { my @result = (); while (my $res = $client->next_page($uri)) { - die "$res->result\n" unless $res->is_success; + return [] unless check_api_error($res); my $res_hash = $res->as_hash; my $data = $res_hash->{_embedded}{'ngcp:' . $link}; if ('ARRAY' eq ref $data) { @@ -134,6 +134,19 @@ sub get_data { return \@result; } +sub check_api_error { + my $res = shift; + if ($res->is_success) { + return 1; + } else { + die($res->result . "\n") if ( + $res->is_client_error + or ($res->is_server_error and not scalar grep { $_ == $res->code; } qw(502 503 504)) + ); + return 0; + } +} + sub get_email_template { my $templates_data = get_data('/api/emailtemplates/', 'emailtemplates'); foreach my $template (@{$templates_data}) { diff --git a/bin/ngcp-fraud-notifier b/bin/ngcp-fraud-notifier index 7ef96d1..2108f29 100755 --- a/bin/ngcp-fraud-notifier +++ b/bin/ngcp-fraud-notifier @@ -88,7 +88,7 @@ sub get_data { my @result = (); while (my $res = $client->next_page($uri)) { - die "$res->result\n" unless $res->is_success; + return [] unless check_api_error($res); my $res_hash = $res->as_hash; my $data = $res_hash->{_embedded}{'ngcp:' . $link}; if ('ARRAY' eq ref $data) { @@ -104,6 +104,19 @@ sub get_data { return \@result; } +sub check_api_error { + my $res = shift; + if ($res->is_success) { + return 1; + } else { + die($res->result . "\n") if ( + $res->is_client_error + or ($res->is_server_error and not scalar grep { $_ == $res->code; } qw(502 503 504)) + ); + return 0; + } +} + sub get_email_template { my $event = shift;