Skip to content

Commit

Permalink
Fetch error message description for 400s from exception
Browse files Browse the repository at this point in the history
No more generic errors.
  • Loading branch information
joedolson committed Sep 3, 2023
1 parent 542ba72 commit b8b7dc0
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/wp-to-twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ function wpt_check_recent_tweet( $id, $auth ) {
*/
function wpt_post_to_twitter( $twit, $auth = false, $id = false, $media = false ) {
$http_code = 0;
$notice = '';
// If an ID is set but the post is not currently present or published, ignore.
if ( $id ) {
$status = get_post_status( $id );
Expand Down Expand Up @@ -455,7 +456,7 @@ function wpt_post_to_twitter( $twit, $auth = false, $id = false, $media = false
);
update_option( 'wpt_app_limit', $rate_limit );
} catch ( RequestException $e ) {
// Get Guzzle exception response.
// Get Guzzle exception response.
if ( method_exists( $e, 'getResponse' ) ) {
$response = $e->getResponse();
$headers = $response->getHeaders();
Expand All @@ -469,10 +470,15 @@ function wpt_post_to_twitter( $twit, $auth = false, $id = false, $media = false
$http_code = $response->getStatusCode();
}
} catch ( Exception $e ) {
$http_code = 400;
$notice = __( 'Unhandled response', 'wp-to-twitter' );
if ( method_exists( $e, 'getMessage' ) ) {
$error = json_decode( $e->getMessage() );
$http_code = $e->getCode();
$notice = $error->title . ': ' . $error->detail;
} else {
$http_code = 405;
$notice = __( 'Unhandled response', 'wp-to-twitter' );
}
}
$notice = '';
} else {
$http_code = '000';
$notice = __( 'XPost Canceled by custom filter.', 'wp-to-twitter' );
Expand All @@ -498,26 +504,26 @@ function wpt_post_to_twitter( $twit, $auth = false, $id = false, $media = false
$error = __( '304 Not Modified: There was no new data to return', 'wp-to-twitter' );
break;
case 400:
$error = __( '400 Bad Request: The request was invalid. This is the status code returned during rate limiting.', 'wp-to-twitter' );
$error = sprintf( __( '400: %s', 'wp-to-twitter' ), $notice );
break;
case 401:
$error = __( '401 Unauthorized: Authentication credentials were missing or incorrect.', 'wp-to-twitter' );
$error = sprintf( __( '401: %s', 'wp-to-twitter' ), $notice );
update_option( 'wpt_authentication_missing', "$auth" );
break;
case 403:
$error = __( '403 Forbidden: The request is understood, but has been refused by X.com.', 'wp-to-twitter' );
$error = sprintf( __( '403: %s', 'wp-to-twitter' ), $notice );
break;
case 404:
$error = __( '404 Not Found: The URI requested is invalid or the resource requested does not exist.', 'wp-to-twitter' );
$error = sprintf( __( '404: %s', 'wp-to-twitter' ), $notice );
break;
case 406:
$error = __( '406 Not Acceptable: Invalid Format Specified.', 'wp-to-twitter' );
$error = sprintf( __( '406: %s', 'wp-to-twitter' ), $notice );
break;
case 422:
$error = __( '422 Unprocessable Entity: The image uploaded could not be processed.', 'wp-to-twitter' );
$error = sprintf( __( '422: %s', 'wp-to-twitter' ), $notice );
break;
case 429:
$error = __( '429 Too Many Requests: You have exceeded your rate limits.', 'wp-to-twitter' );
$error = sprintf( __( '429: %s', 'wp-to-twitter' ), $notice );
break;
case 500:
$error = __( '500 Internal Server Error: Something is broken at X.com.', 'wp-to-twitter' );
Expand Down Expand Up @@ -1402,8 +1408,8 @@ function wpt_add_twitter_inner_box( $post ) {
<?php
}
?>
<div class='wpt_log' aria-live='assertive'></div>
</div>
<div class='wpt_log' aria-live='assertive'></div>
<?php
}
if ( current_user_can( 'wpt_twitter_custom' ) || current_user_can( 'manage_options' ) ) {
Expand Down

0 comments on commit b8b7dc0

Please sign in to comment.