Skip to content
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

throwMailchimpError resulting in php error when $errorResponse['body'] cannot be encoded #20

Closed
johnpez opened this issue Jun 19, 2018 · 3 comments

Comments

@johnpez
Copy link

johnpez commented Jun 19, 2018

I'm getting an inconsistent fatal error. I wish I could provide you the response back from MailChimp but I haven't been able to catch it yet, this has only happened to me a few times and I cannot reproduce consistently.

It occurs at line 517 of ListRepository.php, in "throwMailchimpError". It appears that occasionally the $errorResponse['body'] cannot be decoded into an array.

Here's the stack trace:

ErrorException: Warning: array_key_exists() expects parameter 2 to be array, null given
 at Welp.MailchimpBundle.Subscriber.ListRepository.throwMailchimpError(ListRepository.php:517)
 at Welp.MailchimpBundle.Subscriber.ListRepository.putSubscriberInList(ListRepository.php:85)
 at Welp.MailchimpBundle.Subscriber.ListRepository.subscribe(ListRepository.php:99)
 at Welp.MailchimpBundle.Event.SubscriberListener.onSubscribe(SubscriberListener.php:35)
 at Symfony.Component.EventDispatcher.EventDispatcher.doDispatch(EventDispatcher.php:212)
 at Symfony.Component.EventDispatcher.EventDispatcher.dispatch(EventDispatcher.php:44)

And, here's what I think an appropriate fix would be:

diff --git a/src/Subscriber/ListRepository.php b/src/Subscriber/ListRepository.php
index eff3b59..f092ac4 100644
--- a/src/Subscriber/ListRepository.php
+++ b/src/Subscriber/ListRepository.php
@@ -514,7 +514,7 @@ class ListRepository
     private function throwMailchimpError(array $errorResponse)
     {
         $errorArray = json_decode($errorResponse['body'], true);
-        if (array_key_exists('errors', $errorArray)) {
+        if (is_array($errorArray) && array_key_exists('errors', $errorArray)) {
             throw new MailchimpException(
                 $errorArray['status'],
                 $errorArray['detail'],

@Nightbr
Copy link
Member

Nightbr commented Jun 20, 2018

Hey, it seems mailchimp changed its api a little bit. It's ok, just make a PR and I will create a new release.

@mhouriet
Copy link

I'm afraid this fix would only make the request crash differently. In my case at least, the underlying problem is that the API wrapper fails to detect SSL validation error from cURL, see https://github.com/drewm/mailchimp-api/issues/200

@Nightbr
Copy link
Member

Nightbr commented Dec 5, 2018

I have merged #21 for now. I think it could be great to handle errors differently with try/catch in order to be more resilient. But you can also use try/catch in your app if you don't want to stop your code if there is an error.

I will close this, feel free to open another issue on the error handling subject if you want to work on it.

Cheers!

@Nightbr Nightbr closed this as completed Dec 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants