Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use Guzzle and handle RequestErrors #154

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from

Conversation

janbritz
Copy link
Contributor

@janbritz janbritz commented Jan 30, 2025

Benötigt für: #152

Folgende Änderungen habe ich in diesem PR vorgenommen:

  • vollständige Umstellung von der eigenen cURL Implementierung zu Guzzle
  • bei einem RequestError oder OptionsFormValidationError wird jeweils ein request_error oder options_form_validation_error geschmissen. Als Fallback wird einfach die GuzzleException geschmissen.

Comment on lines +54 to +88
// phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod.Found
/**
* Create and send an HTTP GET request.
*
* Use an absolute path to override the base path of the client, or a relative path to append to the base path of the client.
* The URL can contain the query string as well.
*
* @param UriInterface|string $uri
* @param array $options
* @return ResponseInterface
* @throws GuzzleException
* @throws request_error
* @throws options_form_validation_error
*/
public function get($uri, array $options = []): ResponseInterface {
return parent::get($uri, $options);
}

/**
* Create and send an HTTP POST request.
*
* Use an absolute path to override the base path of the client, or a relative path to append to the base path of the client.
* The URL can contain the query string as well.
*
* @param UriInterface|string $uri
* @param array $options
* @return ResponseInterface
* @throws GuzzleException
* @throws request_error
* @throws options_form_validation_error
*/
public function post($uri, array $options = []): ResponseInterface {
return parent::post($uri, $options);
}
// phpcs:enable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warum ist das nötig?

Copy link
Contributor Author

@janbritz janbritz Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wegen den @throws options_form_validation_error und @throws request_error. PhpStorm kennzeichnet diese Stellen zwar mit Exception 'request_error' is never thrown in the function, doch ohne diesen Docstring wird z.B. das request_error in

try {
    return $this->client->post($uri, $options);
} catch (request_error $e) {/*...*/}

grau hinterlegt und beim Hovern wird Exception 'request_error' is never thrown in the corresponding 'try' block angezeigt, mit der Option die gesamte 'catch clause' zu entfernen.

Der Linter schlägt in beiden Fällen aber nicht an.

Eine Überlegung wäre es auch die * @throws X_error nur in den Docstrings der Funktionen zu schreiben, wo get/post aufgerufen wird.

classes/api/utils.php Outdated Show resolved Hide resolved
Comment on lines +259 to +264
if (is_null($value)) {
continue;
}

// Cast arrays to objects so that empty arrays get serialized to JSON objects, not arrays.
$transformed[$key] = is_array($value) ? (object) $value : $value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess bei dem was wir aktuell senden passt das so, ich will nur anmerken, dass wir beide diese Annahmen ("null == serverseitiges default" und "keine Listen auf den obersten beiden Ebenen") vermutlich irgendwann mit neuen Requests brechen. Ich hätte es deshalb beim Caller gesehen, das sicherzustellen. Aber da es gerade stimmt kann ich es auch nicht wirklich beanstanden.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ja, das stimmt. Ich würde deshalb erstmal eine NOTE: ... im Docstring hinzufügen, wäre das ein Kompromiss?

Comment on lines 187 to 192
} catch (BadResponseException $e) {
if ($e->getResponse()->getStatusCode() == 404) {
// The static file was not found.
return null;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hier sollte doch jetzt keine BadResponseException mehr ankommen, sondern ein request_error, oder? Und meine coding_exception unten ist auf jeden Fall weniger geil als den request_error wiederzuwerfen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Falls die statische Datei nicht gefunden wurde, wird direkt eine aiohttp.web.HTTPNotFound-Exception im Server geschmissen (siehe). Von der Server-Middleware werden diese Exceptions einfach gereraised ohne transformiert zu werden (siehe). Deshalb kommt hier eine BadResponseException und kein request_error an.

Da gebe ich dir Recht. Die Stelle sollte aber nur erreicht werden, wenn statt raise web.HTTPNotFound eine andere aiohttp.web.Exception geraised werden würde, die kein RequestError ist und das wäre eine "coding exception", da wir nur aiohttp.web.HTTPNotFound vorgesehen haben.

},
function (GuzzleException $exception) {
if (!($exception instanceof RequestException) || !$exception->hasResponse()) {
// TODO: also handle other guzzle exceptions like `ConnectException`?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Halte ich für sinnvoll, die Standard-Fehlermeldung ist doch eher kryptisch. Hat aber denke ich auch keine große Priorität.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dann würde ich das jetzt erstmal nur als TODO stehen lassen.

@janbritz janbritz requested a review from MHajoha February 5, 2025 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants