-
Notifications
You must be signed in to change notification settings - Fork 20
Tweaks to generic API error messaging #1339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
3624a2c
bda979e
5e6fae0
687251d
7dfe3a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -372,11 +372,14 @@ protected function request( $method, $path, $body = array() ) { | |
// If the received response is not JSON, return the raw response | ||
$content_type = wp_remote_retrieve_header( $response, 'content-type' ); | ||
if ( false === strpos( $content_type, 'application/json' ) ) { | ||
if ( 200 != $response_code ) { | ||
return new WP_Error( | ||
'wcc_server_error', | ||
sprintf( 'Error: The WooCommerce Services server returned HTTP code: %d', $response_code ) | ||
); | ||
if ( ! $response_code ) { | ||
return new WP_Error( 'wcc_server_error', 'The WooCommerce Services API could not be reached.' ); | ||
} elseif ( 500 == $response_code ) { | ||
return new WP_Error( 'wcc_server_error', 'The WooCommerce Services API encountered an error. Please try again.' ); | ||
} elseif ( 500 < $response_code ) { | ||
return new WP_Error( 'wcc_server_error', 'The WooCommerce Services API is unavailable. Please try again in just a moment.' ); | ||
} elseif ( 400 <= $response_code ) { | ||
return new WP_Error( 'wcc_server_error', 'The WooCommerce Services API could not process the request.' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain your reasoning for these error messages? (and the ones below) At a glance it looks like all of these could be one of two messages - either "could not be reached" or "encountered an error". Why do some not prompt the user to try again? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is definitely wide open to revision but here's the understanding on which I based it:
I'm actually a little bit uneasy about showing "Please try again." for a 500 error over and over each time they try, since it could well be that it will never work. I think — ideally — this response would indicate a very exceptional case, in which contacting support would be justified. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok so how about we:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jeffstieler This had fallen off my radar – sorry about that. I'm good with those changes except that I think "The WooCommerce Services API encountered an error." could be a little bit misleading, at least for the It seems worth communicating that it has nothing to do with content of the request itself. For 502 responses, proposing "The WooCommerce Services API is momentarily unavailable." – what do you think? If 👎, I'll go ahead with the change as you'd described. |
||
} | ||
return $response; | ||
} | ||
|
@@ -386,31 +389,22 @@ protected function request( $method, $path, $body = array() ) { | |
$response_body = json_decode( $response_body ); | ||
} | ||
|
||
if ( 200 != $response_code ) { | ||
if ( 400 <= $response_code ) { | ||
if ( empty( $response_body ) ) { | ||
return new WP_Error( | ||
'wcc_server_empty_response', | ||
sprintf( | ||
'Error: The WooCommerce Services server returned ( %d ) and an empty response body.', | ||
$response_code | ||
) | ||
); | ||
return new WP_Error( 'wcc_server_empty_response', 'The WooCommerce Services API could not process the request.' ); | ||
} | ||
|
||
$error = property_exists( $response_body, 'error' ) ? $response_body->error : ''; | ||
$message = property_exists( $response_body, 'message' ) ? $response_body->message : ''; | ||
$data = property_exists( $response_body, 'data' ) ? $response_body->data : ''; | ||
|
||
return new WP_Error( | ||
'wcc_server_error_response', | ||
sprintf( | ||
'Error: The WooCommerce Services server returned: %s %s ( %d )', | ||
$error, | ||
$message, | ||
$response_code | ||
), | ||
$data | ||
); | ||
if ( 500 == $response_code ) { | ||
return new WP_Error( 'wcc_server_error_response', 'The WooCommerce Services API encountered an error. Please try again.' ); | ||
} elseif ( 500 < $response_code ) { | ||
return new WP_Error( 'wcc_server_error_response', 'The WooCommerce Services API is unavailable. Please try again in just a moment.' ); | ||
} | ||
|
||
return new WP_Error( 'wcc_server_error_response', $message, $data ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we keep this DRY? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to be sure, are you referring to the repetition of the code, the message, or both? |
||
} | ||
|
||
return $response_body; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know how common/possible it would be in non-development environments, but I see 404s a lot on the order details page when switching between my local WCS server and staging/production.
This change removes the meaningful "not found" I saw before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Will bring back that string for
4xx
responses at least.