Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 37926b5

Browse files
authored
Update API status codes (#2353)
* Update HTTP status codes * Catch JSON parsing errors.
1 parent 525c9c5 commit 37926b5

File tree

9 files changed

+54
-29
lines changed

9 files changed

+54
-29
lines changed

assets/js/data/shared-controls.js

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* External dependencies
33
*/
4+
import { __ } from '@wordpress/i18n';
45
import triggerFetch from '@wordpress/api-fetch';
56

67
/**
@@ -18,6 +19,17 @@ export const apiFetchWithHeaders = ( options ) => {
1819
};
1920
};
2021

22+
/**
23+
* Error thrown when JSON cannot be parsed.
24+
*/
25+
const invalidJsonError = {
26+
code: 'invalid_json',
27+
message: __(
28+
'The response is not a valid JSON response.',
29+
'woo-gutenberg-products-block'
30+
),
31+
};
32+
2133
/**
2234
* Default export for registering the controls with the store.
2335
*
@@ -29,17 +41,30 @@ export const controls = {
2941
return new Promise( ( resolve, reject ) => {
3042
triggerFetch( { ...options, parse: false } )
3143
.then( ( fetchResponse ) => {
32-
fetchResponse.json().then( ( response ) => {
33-
resolve( { response, headers: fetchResponse.headers } );
34-
triggerFetch.setNonce( fetchResponse.headers );
35-
} );
44+
fetchResponse
45+
.json()
46+
.then( ( response ) => {
47+
resolve( {
48+
response,
49+
headers: fetchResponse.headers,
50+
} );
51+
triggerFetch.setNonce( fetchResponse.headers );
52+
} )
53+
.catch( () => {
54+
reject( invalidJsonError );
55+
} );
3656
} )
3757
.catch( ( errorResponse ) => {
3858
if ( typeof errorResponse.json === 'function' ) {
3959
// Parse error response before rejecting it.
40-
errorResponse.json().then( ( error ) => {
41-
reject( error );
42-
} );
60+
errorResponse
61+
.json()
62+
.then( ( error ) => {
63+
reject( error );
64+
} )
65+
.catch( () => {
66+
reject( invalidJsonError );
67+
} );
4368
} else {
4469
reject( errorResponse.message );
4570
}

src/StoreApi/Routes/AbstractRoute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ protected function check_nonce( \WP_REST_Request $request ) {
106106
}
107107

108108
if ( null === $nonce ) {
109-
throw new RouteException( 'woocommerce_rest_missing_nonce', __( 'Missing the X-WC-Store-API-Nonce header. This endpoint requires a valid nonce.', 'woo-gutenberg-products-block' ), 403 );
109+
throw new RouteException( 'woocommerce_rest_missing_nonce', __( 'Missing the X-WC-Store-API-Nonce header. This endpoint requires a valid nonce.', 'woo-gutenberg-products-block' ), 401 );
110110
}
111111

112112
$valid_nonce = wp_verify_nonce( $nonce, 'wc_store_api' );

src/StoreApi/Routes/CartRemoveCoupon.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected function get_route_post_response( \WP_REST_Request $request ) {
6363
$coupon = new \WC_Coupon( $coupon_code );
6464

6565
if ( $coupon->get_code() !== $coupon_code || ! $coupon->is_valid() ) {
66-
throw new RouteException( 'woocommerce_rest_cart_coupon_error', __( 'Invalid coupon code.', 'woo-gutenberg-products-block' ), 403 );
66+
throw new RouteException( 'woocommerce_rest_cart_coupon_error', __( 'Invalid coupon code.', 'woo-gutenberg-products-block' ), 400 );
6767
}
6868

6969
if ( ! $controller->has_coupon( $coupon_code ) ) {

src/StoreApi/Routes/CartSelectShippingRate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected function get_route_post_response( \WP_REST_Request $request ) {
6464
}
6565

6666
if ( ! isset( $request['package_id'] ) || ! is_numeric( $request['package_id'] ) ) {
67-
throw new RouteException( 'woocommerce_rest_cart_missing_package_id', __( 'Invalid Package ID.', 'woo-gutenberg-products-block' ), 403 );
67+
throw new RouteException( 'woocommerce_rest_cart_missing_package_id', __( 'Invalid Package ID.', 'woo-gutenberg-products-block' ), 400 );
6868
}
6969

7070
$controller = new CartController();

src/StoreApi/Utilities/CartController.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function add_to_cart( $request ) {
6464
__( '"%s" is already inside your cart.', 'woo-gutenberg-products-block' ),
6565
$product->get_name()
6666
),
67-
403
67+
400
6868
);
6969
}
7070
wc()->cart->set_quantity( $existing_cart_id, $request['quantity'] + wc()->cart->cart_contents[ $existing_cart_id ]['quantity'], true );
@@ -133,7 +133,7 @@ public function set_cart_item_quantity( $item_id, $quantity = 1 ) {
133133
__( '"%s" is already inside your cart.', 'woo-gutenberg-products-block' ),
134134
$product->get_name()
135135
),
136-
403
136+
400
137137
);
138138
}
139139

@@ -161,7 +161,7 @@ public function validate_add_to_cart( \WC_Product $product, $request ) {
161161
__( 'You cannot add "%s" to the cart because the product is out of stock.', 'woo-gutenberg-products-block' ),
162162
$product->get_name()
163163
),
164-
403
164+
400
165165
);
166166
}
167167

@@ -178,7 +178,7 @@ public function validate_add_to_cart( \WC_Product $product, $request ) {
178178
$product->get_name(),
179179
wc_format_stock_quantity_for_display( $qty_remaining, $product )
180180
),
181-
403
181+
400
182182
);
183183
}
184184
}
@@ -270,7 +270,7 @@ public function validate_cart_item( $cart_item ) {
270270
__( 'There are too many "%s" in the cart. Only 1 can be purchased.', 'woo-gutenberg-products-block' ),
271271
$product->get_name()
272272
),
273-
403
273+
400
274274
);
275275
}
276276

@@ -282,7 +282,7 @@ public function validate_cart_item( $cart_item ) {
282282
__( '"%s" is out of stock and cannot be purchased.', 'woo-gutenberg-products-block' ),
283283
$product->get_name()
284284
),
285-
403
285+
400
286286
);
287287
}
288288

@@ -304,7 +304,7 @@ public function validate_cart_item( $cart_item ) {
304304
wc_format_stock_quantity_for_display( $qty_remaining, $product ),
305305
$product->get_name()
306306
),
307-
403
307+
400
308308
);
309309
}
310310
}
@@ -518,7 +518,7 @@ public function apply_coupon( $coupon_code ) {
518518
__( '"%s" is an invalid coupon code.', 'woo-gutenberg-products-block' ),
519519
esc_html( $coupon_code )
520520
),
521-
403
521+
400
522522
);
523523
}
524524

@@ -530,15 +530,15 @@ public function apply_coupon( $coupon_code ) {
530530
__( 'Coupon code "%s" has already been applied.', 'woo-gutenberg-products-block' ),
531531
esc_html( $coupon_code )
532532
),
533-
403
533+
400
534534
);
535535
}
536536

537537
if ( ! $coupon->is_valid() ) {
538538
throw new RouteException(
539539
'woocommerce_rest_cart_coupon_error',
540540
wp_strip_all_tags( $coupon->get_error_message() ),
541-
403
541+
400
542542
);
543543
}
544544

@@ -561,7 +561,7 @@ function( $code ) {
561561
__( '"%s" has already been applied and cannot be used in conjunction with other coupons.', 'woo-gutenberg-products-block' ),
562562
$code
563563
),
564-
403
564+
400
565565
);
566566
}
567567
}
@@ -654,7 +654,7 @@ protected function get_product_for_cart( $request ) {
654654
throw new RouteException(
655655
'woocommerce_rest_cart_invalid_product',
656656
__( 'This product cannot be added to the cart.', 'woo-gutenberg-products-block' ),
657-
403
657+
400
658658
);
659659
}
660660

@@ -696,7 +696,7 @@ protected function throw_default_product_exception( \WC_Product $product ) {
696696
__( '"%s" is not available for purchase.', 'woo-gutenberg-products-block' ),
697697
$product->get_name()
698698
),
699-
403
699+
400
700700
);
701701
}
702702

@@ -903,7 +903,7 @@ protected function get_variable_product_attributes( $product ) {
903903
throw new RouteException(
904904
'woocommerce_rest_cart_invalid_parent_product',
905905
__( 'This product cannot be added to the cart.', 'woo-gutenberg-products-block' ),
906-
403
906+
400
907907
);
908908
}
909909

src/StoreApi/Utilities/ReserveStock.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function( $item ) {
9090
__( '"%s" is out of stock and cannot be purchased.', 'woo-gutenberg-products-block' ),
9191
$product->get_name()
9292
),
93-
403
93+
400
9494
);
9595
}
9696

@@ -184,7 +184,7 @@ private function reserve_stock_for_product( $product_id, $stock_quantity, \WC_Or
184184
__( 'Not enough units of %s are available in stock to fulfil this order.', 'woo-gutenberg-products-block' ),
185185
$product ? $product->get_name() : '#' . $product_id
186186
),
187-
403
187+
400
188188
);
189189
}
190190
}

src/StoreApi/docs/cart.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ If a cart action cannot be performed, an error response will be returned. This w
259259
"code": "woocommerce_rest_cart_invalid_product",
260260
"message": "This product cannot be added to the cart.",
261261
"data": {
262-
"status": 403
262+
"status": 400
263263
}
264264
}
265265
```

tests/php/StoreApi/Routes/Cart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public function test_remove_coupon() {
332332
);
333333
$response = $this->server->dispatch( $request );
334334
$data = $response->get_data();
335-
$this->assertEquals( 403, $response->get_status() );
335+
$this->assertEquals( 400, $response->get_status() );
336336

337337
// Applied coupon.
338338
$request = new WP_REST_Request( 'POST', '/wc/store/cart/remove-coupon' );

tests/php/StoreApi/Routes/CartItems.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function test_invalid_create_item() {
148148
);
149149
$response = $this->server->dispatch( $request );
150150

151-
$this->assertEquals( 403, $response->get_status() );
151+
$this->assertEquals( 400, $response->get_status() );
152152
}
153153

154154
/**

0 commit comments

Comments
 (0)