-
Notifications
You must be signed in to change notification settings - Fork 22
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
fix(mailchimp): fatal error with invalid API keys #1690
Conversation
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 was not able to complete the testing steps until I've emptied the cache (newspack_nl_mailchimp_cache_lists
option). With the wrong key, the list retrieval doesn't update the lists and fails silently, keeping the cached lists.
includes/service-providers/mailchimp/class-newspack-newsletters-mailchimp.php
Outdated
Show resolved
Hide resolved
@adekbadek aha, you're correct. 41db9b6 should fix this by returning a proper error in this scenario. So now, if you run |
…s-mailchimp.php Co-authored-by: Adam Cassis <adam@adamcassis.com>
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 was not able to complete the testing steps until I've emptied the cache (
newspack_nl_mailchimp_cache_lists
option). With the wrong key, the list retrieval doesn't update the lists and fails silently, keeping the cached lists.@adekbadek aha, you're correct. 41db9b6 should fix this by returning a proper error in this scenario. So now, if you run
wp cron event run newspack_nl_mailchimp_refresh_cache
or wait 20 minutes for the cache to refresh, the cached data will no longer be returned if we fail to refresh the cache, and the error message should appear in the UIs.
Wait! That's by design!
From the docs:
If the cache refresh fails, we will store the error in a separate option, and will only surface it to the user after 20 minutes.
In every admin page we will display a generic Warning message, telling the user to go to Newsletters > Settings to see the errors.
In Newsletters > Settings we will output the errors details.
Maybe what we can do is clear the cache if the API key changes
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## trunk #1690 +/- ##
============================================
- Coverage 20.38% 20.35% -0.04%
- Complexity 2664 2670 +6
============================================
Files 48 48
Lines 10619 10640 +21
============================================
+ Hits 2165 2166 +1
- Misses 8454 8474 +20 ☔ View full report in Codecov by Sentry. |
Thanks for clarifying, @leogermani! I'll spin up another commit with that change |
@leogermani 84044fc and 6d7602e implement the cache clear on API key change, and also add an error message to be shown when the cron job to refresh the cache fails. This will result in the following error being shown in both the Newsletters > Settings and Engagement > Newsletters pages 20 minutes after the cache refresh fails: It will also avoid a fatal error in the newsletter editor. Instead, errors will be shown at the top of the editor and in the Newsletter Campaign sidebar: Testing instructions remain the same. |
*/ | ||
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 ) { |
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.
Having an optional parameter before a required paramater throws a PHP deprecated error
private static function maybe_add_error( $list_id = null, $error ) { | |
private static function maybe_add_error( $list_id, $error ) { |
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.
Ah, $list_id
needs to be optional because it's null
if we're adding an error about refreshing the cache for audience data. 017efcc should fix it by making $error
also optional.
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.
All looks good to me. The addition to clear the cache on api key change is a good one. ( I left one minor suggestion )
There is just one problem not related to this PR.
I think we need to revisit what we did here (cc @adekbadek):
I've changed the api key, invalidated the cache, and then added an invalid key.
I then triggered the cron job to refresh the cache but I never saw an error message being stored and displayed (I modified the code to skip that time before showing errors)
The reason was this.
With invalid API keys, fetch_lists
simply returns an empty array and an error is never triggered.
If you look at the code in that PR, all the time after instantiating the api, we call the validate
method, which throws in case of errors. That's why these methods are always invoked in a try catch statement. We should let the API instantiation throw as well, otherwise it won't trigger errors when it should
Update: Here's a PR to fix this -> #1698
Thanks, @leogermani! Originally this PR tried to avoid returning an empty array, but that meant that a workflow-blocking error would be thrown for temporary Mailchimp API hiccups, too. Glad you figured out a less obtrusive solution in #1698. |
Hey @dkoo, good job getting this PR merged! 🎉 Now, the Please check if this PR needs to be included in the "Upcoming Changes" and "Release Notes" doc. If it doesn't, simply remove the label. If it does, please add an entry to our shared document, with screenshots and testing instructions if applicable, then remove the label. Thank you! ❤️ |
# [3.5.0-alpha.1](v3.4.0...v3.5.0-alpha.1) (2024-11-15) ### Bug Fixes * better handle error responses from AC api ([#1707](#1707)) ([17a21e2](17a21e2)) * **mailchimp:** fatal error with invalid API keys ([#1690](#1690)) ([b7fd7e6](b7fd7e6)) ### Features * **cli:** add command to synchronize premium lists ([#1699](#1699)) ([21d15a7](21d15a7))
🎉 This PR is included in version 3.5.0-alpha.1 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
# [3.5.0](v3.4.0...v3.5.0) (2024-11-25) ### Bug Fixes * better handle error responses from AC api ([#1707](#1707)) ([17a21e2](17a21e2)) * **mailchimp:** fatal error with invalid API keys ([#1690](#1690)) ([b7fd7e6](b7fd7e6)) ### Features * **cli:** add command to synchronize premium lists ([#1699](#1699)) ([21d15a7](21d15a7))
🎉 This PR is included in version 3.5.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
All Submissions:
Changes proposed in this Pull Request:
Fixes a fatal error when trying to fetch audience/list data using an invalid Mailchimp API key. The fatal was happening because our Mailchimp integration's API response validation logic only considered status codes of
400
or404
to be errors, but an invalid API key error is returned with a status code of401
, and unhandled WP errors are returned with status codes of500
. So this PR will handle any response with a status code of400
or greater as an error.How to test the changes in this Pull Request:
trunk
, connect your site to Mailchimp using a valid API key.wp cron event run newspack_nl_mailchimp_refresh_cache
Other information: