Skip to content

Commit

Permalink
unescape slashes/unicode when p-printing JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
felixdorn committed Apr 21, 2023
1 parent 6e6a354 commit 83ca836
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Exceptions/TwitterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

namespace Felix\TwitterStream\Exceptions;

use Exception;
use JsonException;
use Psr\Http\Message\ResponseInterface;

class TwitterException extends \Exception
class TwitterException extends Exception
{
private const PRETTY_PRINT_FLAGS = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;

protected function __construct(string $message)
{
parent::__construct($message);
}

/**
* @throws \JsonException
* @throws JsonException
*/
public static function fromResponse(ResponseInterface $response): TwitterException
{
Expand All @@ -22,7 +26,7 @@ public static function fromResponse(ResponseInterface $response): TwitterExcepti

try {
$body = json_decode($response->getBody()->getContents(), true, 512, flags: JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
} catch (JsonException $e) {
return TwitterException::sprintf(
'Twitter response was not valid JSON: %s (error while parsing: %s)',
$response->getBody()->getContents(),
Expand All @@ -33,7 +37,7 @@ public static function fromResponse(ResponseInterface $response): TwitterExcepti
if ($response->getStatusCode() == 401) {
return self::sprintf(
'Unauthorized. Is your bearer token correct? (error: %s)',
json_encode($body, JSON_PRETTY_PRINT)
json_encode($body, self::PRETTY_PRINT_FLAGS)
);
}

Expand All @@ -45,14 +49,14 @@ public static function fromResponse(ResponseInterface $response): TwitterExcepti
if (!array_key_exists('errors', $body) || count($body['errors']) < 1) {
return TwitterException::sprintf(
'Twitter returns an error unknown to us, please open an issue at https://github.com/felixdorn/twitter-stream-api/issues with the following: ' .
json_encode(['payload' => $body, 'status' => $response->getStatusCode()], JSON_PRETTY_PRINT),
json_encode(['payload' => $body, 'status' => $response->getStatusCode()], self::PRETTY_PRINT_FLAGS),
);
}

// Encoding to JSON here as $decoded['errors'][0] contains an
// inconsistent object, in the sense that its properties may
// change from one request to another.
return new self(json_encode($body['errors'][0], JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT));
return new self(json_encode($body['errors'][0], self::PRETTY_PRINT_FLAGS));
}

private static function handleTooManyRequests(ResponseInterface $response): TwitterException
Expand All @@ -73,7 +77,7 @@ public static function sprintf(string $message, mixed ...$args): self

private static function handleTitleDetailErrors(array $body): self
{
$title = $body['title'];
$title = $body['title'];
$detail = $body['detail'];

// Remove the detail if it's the same as the title
Expand Down

0 comments on commit 83ca836

Please sign in to comment.