Skip to content

Commit

Permalink
fix(OutputConditions): Improve content checking in isHtmlResponse and…
Browse files Browse the repository at this point in the history
… isJsonResponse

- Update the condition in isHtmlResponse and isJsonResponse to check if the dispatcher content is not false before continuing
- Add additional check in isJsonResponse to handle empty content and JSON decoding errors
- Fix #55
  • Loading branch information
guanguans committed Jun 17, 2024
1 parent e307489 commit 9a3b0f8
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/Outputs/Concerns/OutputConditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ protected function isResponse($dispatcher): bool
protected function isHtmlResponse($dispatcher): bool
{
return $this->isResponse($dispatcher)
&& Str::contains($dispatcher->headers->get('Content-Type'), 'text/html')
&& ! $this->isJsonResponse($dispatcher);
&& false !== $dispatcher->getContent()
&& Str::contains($dispatcher->headers->get('Content-Type'), 'text/html')
&& ! $this->isJsonResponse($dispatcher);
}

/**
Expand All @@ -51,19 +52,20 @@ protected function isHtmlResponse($dispatcher): bool
protected function isJsonResponse($dispatcher): bool
{
return $dispatcher instanceof JsonResponse
&& Str::contains($dispatcher->headers->get('Content-Type'), 'application/json')
&& transform($dispatcher, static function (JsonResponse $dispatcher): bool {
if ('' === ($content = $dispatcher->getContent())) {
return false;
}
&& false !== $dispatcher->getContent()
&& Str::contains($dispatcher->headers->get('Content-Type'), 'application/json')
&& transform($dispatcher, static function (JsonResponse $dispatcher): bool {
if ('' === ($content = $dispatcher->getContent())) {
return false;
}

try {
json_decode($content, false, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $jsonException) {
return false;
}
try {
json_decode($content, false, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $jsonException) {
return false;
}

return true;
});
return true;
});
}
}

0 comments on commit 9a3b0f8

Please sign in to comment.