Merge pull request #348 from woocommerce/ppcp-376-fix-logging-error

Handle WP_Error when logging response
This commit is contained in:
Emili Castells 2021-11-08 11:49:09 +01:00 committed by GitHub
commit cb8c12383c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,6 +9,8 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
use WP_Error;
/**
* Trait RequestTrait
*/
@ -20,7 +22,7 @@ trait RequestTrait {
* @param string $url The URL to request.
* @param array $args The arguments by which to request.
*
* @return array|\WP_Error
* @return array|WP_Error
*/
private function request( string $url, array $args ) {
@ -44,12 +46,12 @@ trait RequestTrait {
/**
* Returns request and response information as string.
*
* @param string $url The request URL.
* @param array $args The request arguments.
* @param array $response The response.
* @param string $url The request URL.
* @param array $args The request arguments.
* @param array|WP_Error $response The response.
* @return string
*/
private function request_response_string( string $url, array $args, array $response ): string {
private function request_response_string( string $url, array $args, $response ): string {
$method = $args['method'] ?? '';
$output = $method . ' ' . $url . "\n";
if ( isset( $args['body'] ) ) {
@ -65,6 +67,11 @@ trait RequestTrait {
}
}
if ( $response instanceof WP_Error ) {
$output .= 'Request failed. WP error message: ' . implode( "\n", $response->get_error_messages() ) . "\n";
return $output;
}
if ( isset( $response['headers']->getAll()['paypal-debug-id'] ) ) {
$output .= 'Response Debug ID: ' . $response['headers']->getAll()['paypal-debug-id'] . "\n";
}