Skip to content

Handled unknown response objects that contain json body #10

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

Merged
merged 1 commit into from
Dec 10, 2018
Merged
Changes from all commits
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
51 changes: 48 additions & 3 deletions src/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,15 @@ public function profileMe($name, \Closure $action)
protected function updateResponse(Request $request, Response $response)
{
if ($this->needToUpdateResponse($response)) {
$data = $response->getData(true) ?: [];
$data = $this->getResponseData($response);

if ($data === false) {
return;
}

$data[$this->responseKey] = $this->storage->getData();

$response->setData($data);
$this->setResponseData($response, $data);
}
}

Expand All @@ -122,7 +127,47 @@ protected function updateResponse(Request $request, Response $response)
*/
protected function needToUpdateResponse(Response $response)
{
return $response instanceof JsonResponse && ! $this->storage->isEmpty();
$isJsonResponse = $response instanceof JsonResponse || $response->headers->contains('content-type',
'application/json');

return $isJsonResponse && !$this->storage->isEmpty();
}

/**
* Fetches the contents of the response and parses them to an assoc array
*
* @param Response $response
* @return array|bool
*/
protected function getResponseData(Response $response)
{
if ($response instanceof JsonResponse) {
/** @var $response JsonResponse */
return $response->getData(true) ?: [];
}

$content = $response->getContent();

return json_decode($content, true) ?: false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add json_last_error() check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The goal here is to 'try' to decode the content, if it fails, we don't need to know the reason, just that it's not a valid json content (this is just to try and guess if it's actually a response with a json content).

If there is no error we do our thing, otherwise, just let it go

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please merge this! I'm using dingo and it didn't work until I updated debugger.php with this commit.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done )

}

/**
* Updates the response content
*
* @param Response $response
* @param array $data
* @return JsonResponse|Response
*/
protected function setResponseData(Response $response, array $data)
{
if ($response instanceof JsonResponse) {
/** @var $response JsonResponse */
return $response->setData($data);
}

$content = json_encode($data, JsonResponse::DEFAULT_ENCODING_OPTIONS);

return $response->setContent($content);
}

/**
Expand Down