Skip to content

Commit

Permalink
harden error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
felixdorn committed Oct 4, 2022
1 parent 51c6bed commit 356eeef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
21 changes: 15 additions & 6 deletions src/Exceptions/TwitterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@

class TwitterException extends Exception
{
public function __construct(string $message, public array $raw = [])
public array $raw;

public function __construct(string $message, array $raw = [])
{
parent::__construct($message);

$this->raw = $raw;
}

public static function fromResponse(ResponseInterface $response): TwitterException
Expand All @@ -29,12 +33,18 @@ public static function fromResponse(ResponseInterface $response): TwitterExcepti
return new self('Too many requests (reset in: ' . $reset . ').', $decoded);
},
401 => fn () => new self('Unauthorized.', $decoded),
default => fn () => new self('Should not happen: please open an issue at https://github.com/felixdorn/twitter-stream-api/issues', $decoded)
default => fn () => new self('Should not happen: please open an issue at https://github.com/felixdorn/twitter-stream-api/issues (include the payload)', $decoded)
})();
}

/* @phpstan-ignore-next-line */
return new self(json_encode($decoded['errors'][0]), $decoded);
if (!array_key_exists('errors', $decoded) || count($decoded['errors']) < 1) {
return new self('Should not happen: please open an issue at https://github.com/felixdorn/twitter-stream-api/issues (include the payload)', $decoded);
}

// 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($decoded['errors'][0], JSON_THROW_ON_ERROR), $decoded);
}

public static function fromPayload(array $payload): TwitterException
Expand All @@ -48,7 +58,6 @@ public static function fromPayload(array $payload): TwitterException
return new self('Too many requests (reset in: unknown).', $payload);
}

/* @phpstan-ignore-next-line */
return new self(json_encode($payload), $payload);
return new self(json_encode($payload, JSON_THROW_ON_ERROR), $payload);
}
}
14 changes: 7 additions & 7 deletions src/TwitterStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ public function listen(TwitterConnection $connection, callable $callback): void
$this->createdAt = Clock::now();

$this->parser = new Parser(
DataStream::get($this->response),
new Listener(
function (object $item) use ($callback) {
stream: DataStream::get($this->response),
listener: new Listener(
callback: function (object $item) use ($callback) {
$this->received++;

$callback($item, $this);
Expand All @@ -88,11 +88,11 @@ function (object $item) use ($callback) {
$this->stopListening();
}
},
false
assoc: false
),
"\r\n",
false,
$this->bufferSize
lineEnding: "\r\n",
emitWhitespace: false,
bufferSize: $this->bufferSize
);

$this->parser->parse();
Expand Down

0 comments on commit 356eeef

Please sign in to comment.