Unleash/unleash-client-php

Bug: stale data cache is not loaded when HTTP communication fails with 4xx or 5xx codes

eblanshey opened this issue · 3 comments

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The docs state:

Stale data cache is used when http communication fails while fetching feature list from the server.

The DefaultUnleashRepository only fetches stale cache when an exception is thrown:

                try {
                    $response = $this->httpClient->sendRequest($request);
                    if ($response->getStatusCode() === 200) {
                        $data = $response->getBody()->getContents();
                        $this->setLastValidState($data);
                    }
                } catch (Exception $exception) {
                    $this->configuration->getEventDispatcherOrNull()?->dispatch(
                        new FetchingDataFailedEvent($exception),
                        UnleashEvents::FETCHING_DATA_FAILED,
                    );
                    $data = $this->getLastValidState();
                }

If a 4xx or 5xx code is returned, the FETCHING_DATA_FAILED event is not dispatched, and last valid state not fetched.

The Client's sendRequest() method implements PSR-7, which expects the method to return instead of throwing in the event of a valid response but 4xx or 5xx error code. Since Unleash fails to fetch data from the server with these header codes (according to HTTP conventions), this should be fixed.

My hacky workaround was to create a new Client child class that forces it to throw on non-2xx header codes. Setting the http_errors option on the default client does not work.

To reproduce

  1. Set up the code to use the default Unleash repository with a cache
  2. Make a request to get a response and put data in the cace
  3. Change the authorization in the configuration to something incorrect
  4. Make another request (this will cause the HTTP request to return a 401 header code)
  5. An exception is thrown

Sample code (optional)

No response

Version

v1.8.081

Expected behavior

Stale cache should be loaded, but it's not.

Logs (optional)

No response

Additional context (optional)

No response

Thanks for the report! PR has been made and is now awaiting code review.

Thanks for the quick turnaround!

Version 1.8.2 has been released, thanks for the bug report!