Skip to content

Commit

Permalink
fix: properly show errors from trying to refresh cache
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoo committed Nov 7, 2024
1 parent 84044fc commit 6d7602e
Showing 1 changed file with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,14 @@ private static function update_cache( $list_id, $data ) {
/**
* Clears the cache errors for a given list
*
* @param string $list_id The List ID.
* @param string|null $list_id The List ID, or null for all lists.
* @return void
*/
private static function clear_errors( $list_id ) {
private static function clear_errors( $list_id = null ) {
if ( ! $list_id ) {
delete_option( self::ERRORS_OPTION );
return;
}
$errors = get_option( self::ERRORS_OPTION, [] );
if ( isset( $errors[ $list_id ] ) ) {
unset( $errors[ $list_id ] );
Expand All @@ -303,11 +307,16 @@ private static function clear_errors( $list_id ) {
/**
* Stores the last error for a given list, if the cache is older than self::SURFACE_ERRORS_AFTER
*
* @param string $list_id The List ID.
* @param string $error The error message.
* @param string|null $list_id The List ID, or null for all lists.
* @param string $error The error message.
*/
private static function maybe_add_error( $list_id, $error ) {
Newspack_Newsletters_Logger::log( 'Mailchimp cache: handling error while fetching cache for list ' . $list_id );
private static function maybe_add_error( $list_id = null, $error ) {
Newspack_Newsletters_Logger::log(
sprintf(
'Mailchimp cache: handling error while fetching cache for %s',
$list_id ? 'list ' . $list_id : 'all lists'
)
);
$cache_date = get_option( self::get_cache_date_key( $list_id ) );
if ( $cache_date && ( time() - $cache_date ) > self::SURFACE_ERRORS_AFTER ) {
$errors = get_option( self::ERRORS_OPTION, [] );
Expand Down Expand Up @@ -528,6 +537,7 @@ public static function invalidate_cache() {
Newspack_Newsletters_Logger::log( 'Mailchimp cache: Invalidating cached data' );
delete_option( self::get_cache_date_key() );
delete_option( self::get_lists_cache_key() );
self::clear_errors();
}

/**
Expand All @@ -541,6 +551,7 @@ public static function handle_cron() {
$lists = self::fetch_lists(); // Force a cache refresh.
} catch ( Exception $e ) {
Newspack_Newsletters_Logger::log( 'Mailchimp cache: Error refreshing lists cache: ' . $e->getMessage() );
self::maybe_add_error( null, $e->getMessage() );
return;
}

Expand Down

0 comments on commit 6d7602e

Please sign in to comment.