public function fetchFreshData($source, EncapsulatedOptions $options = null) { if ($options && !$options instanceof SoapOptions) { throw new \InvalidArgumentException('Options must be an instance of SoapOptions.'); } $params = array_merge($this->options->getParameters(), $options ? $options->getParameters() : []); return ObjectType::toArray(\ScriptFUSION\Retry\retry(5, function () use($source, $params) { return $this->getOrCreateClient()->{$source}($params); }, new ExponentialBackoffErrorHandler())); }
/** * {@inheritdoc} * * @param string $source Source. * @param EncapsulatedOptions|null $options Optional. Options. * * @return string Response. * * @throws \InvalidArgumentException Options is not an instance of HttpOptions. * @throws HttpConnectionException Failed to connect to source. * @throws HttpServerException Server sent an error code. */ public function fetchFreshData($source, EncapsulatedOptions $options = null) { if ($options && !$options instanceof HttpOptions) { throw new \InvalidArgumentException('Options must be an instance of HttpOptions.'); } return \ScriptFUSION\Retry\retry($this->getTries(), function () use($source, $options) { if (false === ($response = @file_get_contents($this->getOrCreateUrlBuilder()->buildUrl($source, $options ? $options->getQueryParameters() : [], $this->getBaseUrl()), false, stream_context_create(['http' => ['ignore_errors' => true] + array_merge($this->options->extractHttpContextOptions(), $options ? $options->extractHttpContextOptions() : [])])))) { $error = error_get_last(); throw new HttpConnectionException($error['message'], $error['type']); } $code = explode(' ', $http_response_header[0], 3)[1]; if ($code < 200 || $code >= 400) { throw new HttpServerException("HTTP server responded with error: \"{$http_response_header['0']}\".\n\n{$response}"); } return $response; }, new ExponentialBackoffErrorHandler()); }
public function testErrorCallback() { $invocations = $errors = 0; $outerException = $innerException = null; try { \ScriptFUSION\Retry\retry($tries = 2, function () use(&$invocations, &$innerException) { ++$invocations; throw $innerException = new \RuntimeException(); }, function (\Exception $exception) use(&$innerException, &$errors) { ++$errors; self::assertSame($innerException, $exception); }); } catch (FailingTooHardException $outerException) { } self::assertInstanceOf(FailingTooHardException::class, $outerException); self::assertSame($tries, $invocations); self::assertSame(1, $errors); }