Enums
7 months ago
Messages
7 months ago
AgenticCheckoutSession.php
7 months ago
CheckoutSessions.php
4 months ago
CheckoutSessionsComplete.php
4 months ago
CheckoutSessionsUpdate.php
4 months ago
Error.php
7 months ago
Error.php
148 lines
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\StoreApi\Routes\V1\Agentic; |
| 5 | |
| 6 | use Automattic\WooCommerce\Internal\Agentic\Enums\Specs\ErrorType; |
| 7 | use WP_REST_Response; |
| 8 | |
| 9 | /** |
| 10 | * Error class. |
| 11 | * |
| 12 | * Represents an error object as defined in the Agentic Commerce Protocol. |
| 13 | * This class handles API-level errors with type, code, message, and optional param. |
| 14 | */ |
| 15 | class Error { |
| 16 | /** |
| 17 | * The error type. |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | private $type; |
| 22 | |
| 23 | /** |
| 24 | * Implementation-defined error code. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | private $code; |
| 29 | |
| 30 | /** |
| 31 | * Human-readable error message. |
| 32 | * |
| 33 | * @var string |
| 34 | */ |
| 35 | private $message; |
| 36 | |
| 37 | /** |
| 38 | * RFC 9535 JSONPath to the problematic parameter (optional). |
| 39 | * |
| 40 | * @var string|null |
| 41 | */ |
| 42 | private $param; |
| 43 | |
| 44 | /** |
| 45 | * Constructor. |
| 46 | * |
| 47 | * @param string $type Error type from ErrorType enum. |
| 48 | * @param string $code Implementation-defined error code. |
| 49 | * @param string $message Human-readable error message. |
| 50 | * @param string|null $param RFC 9535 JSONPath (optional). |
| 51 | */ |
| 52 | private function __construct( $type, $code, $message, $param = null ) { |
| 53 | $this->type = $type; |
| 54 | $this->code = $code; |
| 55 | $this->message = $message; |
| 56 | $this->param = $param; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Create an invalid request error. |
| 61 | * |
| 62 | * @param string $code Implementation-defined error code. |
| 63 | * @param string $message Human-readable error message. |
| 64 | * @param string|null $param RFC 9535 JSONPath (optional). |
| 65 | * @return Error |
| 66 | */ |
| 67 | public static function invalid_request( $code, $message, $param = null ) { |
| 68 | return new self( ErrorType::INVALID_REQUEST, $code, $message, $param ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Create a request not idempotent error. |
| 73 | * |
| 74 | * @param string $code Implementation-defined error code. |
| 75 | * @param string $message Human-readable error message. |
| 76 | * @param string|null $param RFC 9535 JSONPath (optional). |
| 77 | * @return Error |
| 78 | */ |
| 79 | public static function request_not_idempotent( $code, $message, $param = null ) { |
| 80 | return new self( ErrorType::REQUEST_NOT_IDEMPOTENT, $code, $message, $param ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Create a processing error. |
| 85 | * |
| 86 | * @param string $code Implementation-defined error code. |
| 87 | * @param string $message Human-readable error message. |
| 88 | * @param string|null $param RFC 9535 JSONPath (optional). |
| 89 | * @return Error |
| 90 | */ |
| 91 | public static function processing_error( $code, $message, $param = null ) { |
| 92 | return new self( ErrorType::PROCESSING_ERROR, $code, $message, $param ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Create a service unavailable error. |
| 97 | * |
| 98 | * @param string $code Implementation-defined error code. |
| 99 | * @param string $message Human-readable error message. |
| 100 | * @param string|null $param RFC 9535 JSONPath (optional). |
| 101 | * @return Error |
| 102 | */ |
| 103 | public static function service_unavailable( $code, $message, $param = null ) { |
| 104 | return new self( ErrorType::SERVICE_UNAVAILABLE, $code, $message, $param ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Convert the error to a WP_REST_Response. |
| 109 | * |
| 110 | * @return WP_REST_Response WordPress REST API response object |
| 111 | */ |
| 112 | public function to_rest_response() { |
| 113 | $data = array( |
| 114 | 'type' => $this->type, |
| 115 | 'code' => $this->code, |
| 116 | 'message' => $this->message, |
| 117 | ); |
| 118 | |
| 119 | if ( null !== $this->param ) { |
| 120 | $data['param'] = $this->param; |
| 121 | } |
| 122 | |
| 123 | $status_code = $this->get_http_status_code(); |
| 124 | |
| 125 | return new WP_REST_Response( $data, $status_code ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Determine HTTP status code based on error type. |
| 130 | * |
| 131 | * @return int HTTP status code |
| 132 | */ |
| 133 | private function get_http_status_code() { |
| 134 | switch ( $this->type ) { |
| 135 | case ErrorType::INVALID_REQUEST: |
| 136 | return 400; |
| 137 | case ErrorType::REQUEST_NOT_IDEMPOTENT: |
| 138 | return 409; |
| 139 | case ErrorType::PROCESSING_ERROR: |
| 140 | return 500; |
| 141 | case ErrorType::SERVICE_UNAVAILABLE: |
| 142 | return 503; |
| 143 | default: |
| 144 | return 500; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 |