PluginProbe
WooCommerce / 11.1.0-beta.2
WooCommerce v11.1.0-beta.2
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / src / Internal / PushNotifications / Traits / ConvertsExceptionsToWpError.php
ConvertsExceptionsToWpError.php
59 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare( strict_types = 1 );
4
5 namespace Automattic\WooCommerce\Internal\PushNotifications\Traits;
6
7 defined( 'ABSPATH' ) || exit;
8
9 use Automattic\WooCommerce\Internal\PushNotifications\PushNotifications;
10 use Automattic\WooCommerce\Proxies\LegacyProxy;
11 use Exception;
12 use WC_Data_Exception;
13 use WP_Error;
14 use WP_Http;
15
16 /**
17 * Shared converter for REST controller catch blocks in the PushNotifications
18 * module.
19 *
20 * Surfaces domain-specific `WC_Data_Exception` details for client-recoverable
21 * failures (non-500 status codes), and logs + returns a generic
22 * internal-error response for 500-level or unrecognized exceptions so that
23 * internal details aren't leaked to API clients.
24 */
25 trait ConvertsExceptionsToWpError {
26 /**
27 * Convert an exception thrown by a service into a WP_Error suitable for
28 * the REST response.
29 *
30 * @param Exception $e The exception to convert.
31 * @return WP_Error
32 */
33 protected function convert_exception_to_wp_error( Exception $e ): WP_Error {
34 // Non-500 WC_Data_Exception: client-recoverable, surface details.
35 if (
36 $e instanceof WC_Data_Exception
37 && $e->getCode() !== WP_Http::INTERNAL_SERVER_ERROR
38 ) {
39 return new WP_Error(
40 $e->getErrorCode(),
41 $e->getMessage(),
42 $e->getErrorData()
43 );
44 }
45
46 // Anything else: log and surface a generic 500.
47 wc_get_container()
48 ->get( LegacyProxy::class )
49 ->call_function( 'wc_get_logger' )
50 ->error( $e->getMessage(), array( 'source' => PushNotifications::FEATURE_NAME ) );
51
52 return new WP_Error(
53 'woocommerce_internal_error',
54 __( 'Internal server error', 'woocommerce' ),
55 array( 'status' => WP_Http::INTERNAL_SERVER_ERROR )
56 );
57 }
58 }
59