guzzle: PhpStorm warns about unhandled GuzzleException
Q | A |
---|---|
Bug? | no |
New Feature? | no |
Version | 6.3.3 |
Not a bug, just inconvenience in development. The PhpStorm IDE has built-in code analysis and it warns on my code which uses (new GuzzleHttp())->send()
method without catching GuzzleException
exeption. But I’m catching ClientException
and RequestException
instead and I think this should be enough. Here’s my code:
/**
* @param Request $request
* @return Response
*/
protected function executeRequest(Request $request): Response
{
$this->lastRequest = $request;
try {
$this->lastResponse = (new GuzzleHttp())->send($request, $this->getRequestOptions());
} catch (ClientException $e) {
$this->lastResponse = $e->getResponse();
throw new FatalException($this->getExceptionMessage($e));
} catch (RequestException $e) {
$this->lastResponse = $e->getResponse();
throw new Exception($this->getExceptionMessage($e));
}
return $this->lastResponse;
}
PhpStorm wants me to add yet another catch section with GuzzleException (which is not exception actually, just interface) or to add:
* @throws \GuzzleHttp\Exception\GuzzleException
in PhpDoc of my method.
The problem is going from ClientInterface where PhpDoc describes that it @throws GuzzleException
:
/**
* Send an HTTP request.
*
* @param RequestInterface $request Request to send
* @param array $options Request options to apply to the given
* request and to the transfer.
*
* @return ResponseInterface
* @throws GuzzleException
*/
public function send(RequestInterface $request, array $options = []);
and concrete implementation Client doesn’t have PhpDoc at all:
public function send(RequestInterface $request, array $options = [])
{
$options[RequestOptions::SYNCHRONOUS] = true;
return $this->sendAsync($request, $options)->wait();
}
This is why PhpStorm takes throwing exceptions from interface. Please add PhpDoc to Client::send()
method. This will override @throws
tag in the interface and fix the problem.
I think that declaring in PhpDoc @throws
tag the class which is not descendant of \Exception
class is anti-pattern and bad practice. It forces developers to catch this exception which is not exception. Make GuzzleException
abstract class extending from \RuntimeException
and inherit from it all other classes if needed or not use it in @throws
tags.
Actually you don’t need to declare @throws
tags since all your exceptions are inherited from \RuntimeException
. This kind of exceptions that can happen almost anywhere and you can but don’t have to catch it everywhere. This is why you don’t need to add it in PhpDoc. This logic is taken from Java. In the Java only RuntimeException can be not catched. Otherwise the code will not compile.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 16
- Comments: 17 (1 by maintainers)
Extending Throwable is not enough. We still need to catch both \Exception and GuzzleException.
GuzzleException should have been a class extending \Exception instead of an interface extending \Throwable
Can’t extend throwable because guzzle needs to support php 5 still.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 2 weeks if no further activity occurs. Thank you for your contributions.
It would probably be enough to extend
\Exception
What’s the status of this? Searches show nothing helpful and this is still happening for me in PhpStorm 2020.3.2
Did I miss the fix somewhere, have I done something wrong?
+1
There is already a fix, that prevents the problem with PHP 5.x (if you use this version, you are not a PHP developer, dude):
Ticket: https://github.com/guzzle/guzzle/issues/2184 With the commit: https://github.com/guzzle/guzzle/commit/e21a98265ccca8a92b67997baf0cec21b78ba65d
Now keep focus on this ticket.
Cant we lock a version for PHP 5.6 support and then drop support and extended
\Exception
?According to the packagist 2019.1 PHP Versions Stats, PHP 5 usage across all versions is now around 10%.
+1 - so tired of “Unhandled exception” warnings and
@throws \Throwable