From a341d427fc51dc387c53ad13372599484a56bd44 Mon Sep 17 00:00:00 2001 From: Alex P Date: Thu, 4 Nov 2021 10:29:10 +0200 Subject: [PATCH] Handle WP_Error when logging response --- .../src/Endpoint/RequestTrait.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/modules/ppcp-api-client/src/Endpoint/RequestTrait.php b/modules/ppcp-api-client/src/Endpoint/RequestTrait.php index 187f7c89a..8c05e27db 100644 --- a/modules/ppcp-api-client/src/Endpoint/RequestTrait.php +++ b/modules/ppcp-api-client/src/Endpoint/RequestTrait.php @@ -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"; }