sendTemplate() can return a ServerException

Created on 1 February 2023, over 1 year ago

If you have an invalid API key or anything else that may cause a server error when using the API, in sendTemplate(), the $result returned is actually an instance of GuzzleHTTP/ServerException, not an array, which is not allowed in PHP 8.1.

It's a bit weird, as in Mailchimp's transactional API in vendor/mailchimp/transactional/lib/Configuration.php, in the post() function the sendTemplate() is using, it's designed in a way that it either returns the response body as an array, or if there's a RequestException, it returns the exception, so we cannot catch the RequestException in our code either...

        // send request
        try {
            $client = $this->getRequestClient();
            $response = $client->request('POST', $postUrl, $options);
            $resp = $response->getBody();
            $contentType = $response->getHeaderLine('content-type');

            if (strpos($contentType, 'application/json') !== false) {
                return json_decode($resp);
            }

            return $resp;
        } catch (RequestException $e) {
            return $e;
        }

To resolve, we can either change the sendTemplate() function to return "mixed", or check if the result is an exception and log an error. I've done the latter, and will just add my code, but this may need some more thought.

This relates to issue: https://www.drupal.org/project/mailchimp_transactional/issues/3279838 πŸ› TypeError: Drupal\mailchimp_transactional\API::send(): Return value must be of type array, ConnectException returned Needs review

πŸ› Bug report
Status

Closed: duplicate

Version

1.0

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom littlepixiez

Live updates comments and jobs are added and updated live.
  • PHP 8.1

    The issue particularly affects sites running on PHP version 8.1.0 or later.

Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024