From cca1477f9a81f2c65f220e367cbbf8112da014d9 Mon Sep 17 00:00:00 2001 From: Kirill Solomko Date: Mon, 24 Feb 2025 14:18:04 +0100 Subject: [PATCH] MT#62223 improve error response handling when log_response is off * log_response: 0 is reworked, it now always logs the response but in case if it's off the body is not logged. * adjust error logging, the error in the body is correctly logged in the response even if log_response: 0 Change-Id: I6dfddeccffb5464b830133235bf63c5e82ae6073 (cherry picked from commit e8f41aba5078f91176aa7a80cb22c4d7adac81e4) --- lib/NGCP/Panel/Role/API.pm | 26 ++++++++++++++++++++------ lib/NGCP/Panel/Role/Entities.pm | 4 +--- lib/NGCP/Panel/Role/EntitiesItem.pm | 4 +--- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/lib/NGCP/Panel/Role/API.pm b/lib/NGCP/Panel/Role/API.pm index 82b76aa6ae..5f499be400 100644 --- a/lib/NGCP/Panel/Role/API.pm +++ b/lib/NGCP/Panel/Role/API.pm @@ -1065,9 +1065,11 @@ sub log_response { $c->forward(qw(Controller::Root render)); $c->response->content_type('') if $c->response->content_type =~ qr'text/html'; # stupid RenderView getting in the way - my $rc = ''; my $errors = ''; + my $has_errors = 0; if ($c->has_errors) { + $has_errors = 1; + $errors = $c->stash->{is_api_error_response} ? join ', ', splice @{$c->error}, 1 : join ', ', @{$c->error}; @@ -1083,19 +1085,31 @@ sub log_response { ); } + # to avoid html content response $c->clear_errors; } - my ($response_body, $params_data) = $self->filter_log_response( + + my ($response_body, $params_data) = + ( + ($has_errors || $self->get_config('log_response')) + ? $c->response->body + : undef, + $c->request->parameters + ); + + my ($filtered_response_body, $filtered_params_data) = + $self->filter_log_response( $c, - $c->response->body, - $c->request->parameters, + $response_body, + $params_data ); + NGCP::Panel::Utils::Message::info( c => $c, type => 'api_response', - desc => $c->qs($response_body), + desc => $c->qs($filtered_response_body), log => $c->qs($errors // ''), - data => $params_data, + data => $filtered_params_data, ); } diff --git a/lib/NGCP/Panel/Role/Entities.pm b/lib/NGCP/Panel/Role/Entities.pm index ae98833eb1..28baa941f6 100644 --- a/lib/NGCP/Panel/Role/Entities.pm +++ b/lib/NGCP/Panel/Role/Entities.pm @@ -33,9 +33,7 @@ sub end :Private { } } - if ($self->get_config('log_response')) { - $self->log_response($c); - } + $self->log_response($c); return 1; } diff --git a/lib/NGCP/Panel/Role/EntitiesItem.pm b/lib/NGCP/Panel/Role/EntitiesItem.pm index 93875e0969..9e4ba3a9b6 100644 --- a/lib/NGCP/Panel/Role/EntitiesItem.pm +++ b/lib/NGCP/Panel/Role/EntitiesItem.pm @@ -38,9 +38,7 @@ sub end :Private { } } - if ($self->get_config('log_response')) { - $self->log_response($c); - } + $self->log_response($c); return 1; }