FileClient: Expose HTTP status as exception code#905
FileClient: Expose HTTP status as exception code#905pe1uca wants to merge 2 commits intosimplepie:masterfrom
Conversation
jtojnar
left a comment
There was a problem hiding this comment.
Thanks for the pull request, including tests.
Would you mind changing the commit messages to something like the following so that the context is clearer in the commit log:
- FileClient: Expose HTTP status as exception code
- SimplePie: Propagate HTTP status code from exception
| } catch (HttpException $th) { | ||
| $this->check_modified = false; | ||
| $this->status_code = 0; | ||
| $this->status_code = $th->getCode(); |
There was a problem hiding this comment.
I think this makes sense for genuine HTTP errors but we sometimes promoted a random Exception to HTTPException, even when there is no meaningful error code.
So I wonder if we should add a 1000 to the error code of promoted exceptions like this:
- IIUC,
Filewill never throw an exception, unless some warning gets promoted toExceptionby the library user’s custom error handler:
simplepie/src/HTTP/FileClient.php
Line 69 in 72b4293
- HTTP Errors do not throw exceptions so this error code cannot be a HTTP status code:
simplepie/src/HTTP/Psr18Client.php
Line 117 in 72b4293
- Definitely not a HTTP status code:
simplepie/src/HTTP/Psr18Client.php
Line 154 in 72b4293
simplepie/src/HTTP/Psr18Client.php
Line 158 in 72b4293
and then only change the status code when it does not:
| $this->status_code = $th->getCode(); | |
| $this->status_code = $th->getCode() < 1000 ? $th->getCode() : 0; |
Though of course this will be easy to forget if we ad more HTTPException promotions.
|
Actually, thinking about it some more, it seems to me like
And our Similarly, |
|
Actually, we do enable Lines 139 to 142 in 72b4293 So that means |
1e1ebfa to
8cd76e9
Compare
|
I'm guessing all the changes needed to improve the flows and make them consistent should be addressed in another PR, right? My main fix was in the class The change to |
|
I have some fundamental issues with this PR. The HttpException is not made to represent a response, but to tell you that there was a problem making the request. If you have a response status code (even 500) the pure http request was successful, so no HttpException will be thrown. (Please note that the File/FileClient will be deprecated in favor of PSR-18.) See PSR-18 error handling https://www.php-fig.org/psr/psr-18/#error-handling:
The request could fail for much more reasons as only for an HTTP request, e.g. reading a file from the local file system. One could not rely on the exception code to get a possible response status code. In opposite if you have a response status code the HTTP request was successful (but the response could be a 500 error). Instead of moving the status code into the HttpException you can use the Psr18Client and get the status code from the response: try {
$response = $psr18Client->request(
'GET',
'http://example.com/feed.xml',
['Accept' => 'application/atom+xml']
);
} catch (HttpException $e) {
// Something went wrong making the HTTP request, reading the local file, etc
}
// $response is instance of \SimplePie\HTTP\Response
$statusCode = $response->get_status_code(); // Check for 500, 429, 200 or what else |
|
Right. But currently, Line 124 in 72b4293 Lines 140 to 142 in 72b4293 simplepie/src/HTTP/FileClient.php Lines 72 to 74 in 72b4293 We probably cannot drop the |
|
We can do this right now. HttpException and FileClient only exists in the master branch so far. |
Probably the issue is if the users of SimplePie don't call Lines 3437 to 3454 in 72b4293 The documentation for
Is there any word on when this version is planned to be released? I've never totally understood PSR wording, so please bare with me 😅
And
But in the repo there's no class which implements this interface. So, this will make every user of SimplePie migrating to 2.0.0 (when it comes out) require to implement their own way of making http requests instead of being already included out of the box. |
I think it is still worth it. It is hard to predict volunteer availability.
I would call the first a PSR-18 Client library but I guess they decided to keep it short. Such shortening is fine in unambiguous cases, but ambiguity is often easy to miss, which leads to confusion, as you have experienced. Also, since HTTP is a client-server protocol, even more confusingly, client can also refer to the abstract role in the HTTP protocol, or any implementation the role, such as curl. The purpose of PSR-18 is to make the HTTP client implementations interchangeable. Without that, each library that wants to use HTTP would need to bring its own HTTP client implementation. As a result, an application that uses multiple such libraries (e.g. RSS reader can use SimplePie and Graby) would transitively depend on multiple, possibly slightly different HTTP libraries. An example of a client library is https://github.com/guzzle/guzzle. You can see the full list at https://packagist.org/providers/psr/http-client-implementation.
SimplePie is not a PSR-18 client. It supports using such library to allow consumers to select their preferred HTTP client implementation.
Yes, the name is a bit confusing since language is always a bit ambiguous when we do not want overly long class names. The We introduced
Yes, there is some upgrade cost but projects do not need to implement their own HTTP library, there are tons of ready made PSR-18 HTTP clients. And most modern projects will already be using one of them. |
|
I created #909 to rename the misleading SimplePie stores the status code from the response, so one can use I don't see any reason for changes in this PR. |
|
Just to be clear: I understand that the real problem of FreshRSS/FreshRSS#7038 is that in FreshRSS SimplePie logs 429 status code but |
|
As mentioned above, I believe the issue is that |
|
@jtojnar Thank you. 👍 I should have read the whole conversion again after such a long time. I will try to reproduce it again. |
* SimplePie: Fix propagation of HTTP error codes fix #7038 FreshRSS/simplepie#36 upstream simplepie/simplepie#905 Co-authored-by: Edgar Alvarado <15692727+pe1uca@users.noreply.github.com>
There was a problem hiding this comment.
In #909 I've added integration tests to show that $simplepie->status_code() always returns the correct status code if the request was successful (if the server had send a response, even a 429 or 500 code). If the request failed, $simplepie->status_code() returns 0. In this case one can use $simplePie->error() to get the error message why the HTTP request fails.
The HttpException/ClientException is an internal class for now and should not be used and should not contain the status code as exception code to be compatible with PSR-18 for future changes.
…or in FileClient (#909) This PR renames the `SimplePie\Exception\HttpException` to `SimplePie\HTTP\ClientException` to avoid confusion (#905) that this exception is thrown on an 4xx or 5xx response from the server. Because the `ClientException` is always caught internally, I marked the class as `@internal`. Additionally, change the `FileClient` to only throw the exception on non-HTTP errors to match PSR-18 and our `Psr18Client`. This is not a BC break since this API is not part of any release yet. Also add unit tests to show that the responses (`File` or `Psr7Response` class) always contain the status code, even if it is 429. Plus integration tests using a local HTTP server.
|
Actually, we also need to expose the HTTP headers, for instance to access the |
|
Maybe #930 |
Finalise merge, following: * simplepie#905 (comment) * simplepie#909 * FreshRSS/FreshRSS#7038
* FreshRSS/simplepie#47 Finalise merge, following: * simplepie/simplepie#905 (comment) * simplepie/simplepie#909 * FreshRSS#7038
* Bump SimplePie with PHPStan Level 8 * FreshRSS/simplepie#45 SimplePie increased to PHPStan Level 8: * simplepie/simplepie#857 * Merge upstream Including my two PRs: * simplepie/simplepie#932 * simplepie/simplepie#933 * Resolve upstream sync of Expose HTTP status * FreshRSS/simplepie#47 Finalise merge, following: * simplepie/simplepie#905 (comment) * simplepie/simplepie#909 * #7038
Fixes FreshRSS/FreshRSS#7038