Improve error handling otherwise lots of empty client entries

Created on 13 April 2023, over 1 year ago

Problem/Motivation

      if ($response->getStatusCode() !== 200) {
        $this->logger->error('Invalid connection with client site');
      }
      else {
        $json = $response->getBody()->getContents();
        $values = json_decode($json, TRUE);
       // ...

Steps to reproduce

Enable https://www.drupal.org/project/require_login β†’ , but don't have the /status_dashboard/check route whitelisted.
It results in a 302 redirect into a 200 HTML response.

And results in lots of empty client entries because $values['url'] and $values['sitename'] would be unset and thus returning NULL.

Proposed resolution

Add appropriate checks on the $value returned and as well as checking that the HTTP header response being valid JSON.

e.g. with something along the lines of:

      // Using stripos to account for response headers with the charset directive set, such as "application/json; charset=utf-8"
      if ($status !== 200 || 0 !== stripos($res->getHeaderLine('Content-Type') ?? '', 'application/json')) {
        \Drupal::logger('status_dashboard')
          ->error('Invalid connection with client site');
      }
      else {
        $json = $res->getBody()->getContents();
        $response = json_decode($json, TRUE);
        $values = (array) $response;

        if (empty($values['sitename']) || empty($values['url'])) {
          \Drupal::logger('status_dashboard')
            ->error('Could not retrieve the site name or url from the client site');
          continue;
        }

Remaining tasks

Provide issue fork/patch.

User interface changes

N/A

API changes

N/A

Data model changes

N/A

πŸ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024