From 2fc9436b09eb7f4653877d5bc0541e8801b18072 Mon Sep 17 00:00:00 2001 From: Justin Foell <630830+jrfoell@users.noreply.github.com> Date: Fri, 24 Apr 2020 10:25:11 -0500 Subject: [PATCH] Added exception handling to authorization process --- readme.txt | 2 ++ src/WPStrava/Auth.php | 6 +++++- src/WPStrava/Exception.php | 10 +++++----- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/readme.txt b/readme.txt index 2d290da..ee931fb 100755 --- a/readme.txt +++ b/readme.txt @@ -105,6 +105,8 @@ On the WP-Strava settings page you cannot currently remove and add another athle = 2.3.0 = Renamed LatestActivities classes to ActivitiesList. +Added exception handling to authorization process. + = 2.2.0 = Added rudimentary gutenberg block for single Activity. diff --git a/src/WPStrava/Auth.php b/src/WPStrava/Auth.php index d286040..f2d0d76 100644 --- a/src/WPStrava/Auth.php +++ b/src/WPStrava/Auth.php @@ -118,7 +118,11 @@ abstract class WPStrava_Auth { $data = $this->add_initial_params( $data ); - $strava_info = $this->token_request( $data ); + try { + $strava_info = $this->token_request( $data ); + } catch ( WPStrava_Exception $e ) { + wp_die( $e->to_html() ); // phpcs:ignore -- Debug only. + } if ( isset( $strava_info->access_token ) ) { $settings->add_id( $client_id ); diff --git a/src/WPStrava/Exception.php b/src/WPStrava/Exception.php index 325c4e4..def4c4d 100644 --- a/src/WPStrava/Exception.php +++ b/src/WPStrava/Exception.php @@ -1,10 +1,8 @@ -=' ) ) { /* * Exception class for error handling/display. + * + * For creation use `new WPStrava_Exception()` or `WPStrava_Exception::from_wp_error( $error )`. + * For display use $e->to_html(), echo/(s)printf $e, or strval( $e ). */ class WPStrava_Exception extends WPStrava_Abstract_Exception { @@ -80,7 +81,7 @@ class WPStrava_Exception extends WPStrava_Abstract_Exception { * @author Justin Foell * @since 1.6.0 */ - public function get_formatted_message( $exception ) { + private function get_formatted_message( $exception ) { $code = $exception->getCode(); if ( $exception->getPrevious() ) { @@ -100,4 +101,3 @@ class WPStrava_Exception extends WPStrava_Abstract_Exception { return sprintf( __( 'WP Strava ERROR %1$s', 'wp-strava' ), $exception->getMessage() ); } } -// phpcs:enable