Handle WP_Error when logging response

This commit is contained in:
Alex P 2021-11-04 10:29:10 +02:00
parent 57549e445b
commit a341d427fc

View file

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