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

Catch down network exception and non-object responses in fetch #2492

Merged
merged 5 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/Collections/TerminusCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
use Pantheon\Terminus\Models\TerminusModel;
use Pantheon\Terminus\Request\RequestAwareInterface;
use Pantheon\Terminus\Request\RequestAwareTrait;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;

/**
* Class TerminusCollection
* @package Pantheon\Terminus\Collections
*/
abstract class TerminusCollection implements ContainerAwareInterface, RequestAwareInterface
abstract class TerminusCollection implements ContainerAwareInterface, RequestAwareInterface, LoggerAwareInterface
{
use ContainerAwareTrait;
use RequestAwareTrait;
use LoggerAwareTrait;

/**
* @var array
Expand Down Expand Up @@ -91,6 +94,22 @@ public function all()
public function fetch()
{
foreach ($this->getData() as $id => $model_data) {
if (!is_object($model_data)) {
// For some reason I can't replicate, occasionally $model_data is just a string here.
$trace = debug_backtrace();
$error_message = "Fetch failed {file}:{line} model_data expected as object but returned as {type}.";
$context = [
'file' => $trace[0]['file'],
'line' => $trace[0]['line'],
'type' => gettype($model_data),
];
if (is_string($model_data)) {
jms-pantheon marked this conversation as resolved.
Show resolved Hide resolved
$error_message .= " String value: {string}";
namespacebrian marked this conversation as resolved.
Show resolved Hide resolved
namespacebrian marked this conversation as resolved.
Show resolved Hide resolved
$context['string'] = strlen($model_data) > 250 ? substr($model_data, 0, 250) . '...' : $model_data;
}
$this->logger->error($error_message, $context);
jms-pantheon marked this conversation as resolved.
Show resolved Hide resolved
break;
jms-pantheon marked this conversation as resolved.
Show resolved Hide resolved
}
if (!isset($model_data->id)) {
$model_data->id = $id;
}
Expand Down
10 changes: 9 additions & 1 deletion src/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,20 @@ private function createRetryDecider(): callable
}
}

if (is_object($response) && is_object($response->getBody()) && $response->getBody()->getContents() !== '') {
jms-pantheon marked this conversation as resolved.
Show resolved Hide resolved
$error = $response->getBody()->getContents();
} elseif (null !== $exception && '' != $exception->getMessage()) {
$error = $exception->getMessage();
} else {
$error = "Undefined";
}

$this->logger->error(
"HTTP request {method} {uri} has failed with error {error}.",
[
'method' => $request->getMethod(),
'uri' => $request->getUri(),
'error' => $response->getBody()->getContents(),
'error' => $error,
]
);

Expand Down