Skip to content

Commit

Permalink
[HttClientException] Add failedRequest property
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Samofal committed May 21, 2023
1 parent 095e26a commit d9255bc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
27 changes: 23 additions & 4 deletions src/Exceptions/RelesysHttpClientException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,36 @@

namespace Getsno\Relesys\Exceptions;

use Throwable;
use Illuminate\Http\Client\RequestException;
use Getsno\Relesys\HttpClient\FailedRequest;

class RelesysHttpClientException extends RelesysException
{
public readonly FailedRequest $failedRequest;

public function __construct(
$message = '',
$code = 0,
Throwable $previous = null,
FailedRequest $failedRequest = null,
) {
parent::__construct($message, $code, $previous);

$this->failedRequest = $failedRequest;
}

public static function getTokenFailed(string $error, int $code, RequestException $prevException): self
{
return new static("Failed to get Bearer token: $error", $code, $prevException);
return new static("[Relesys] Failed to get Bearer token: $error", $code, $prevException);
}

public static function requestFailed(string $error, int $code, RequestException $prevException): self
{
return new static("API request failed: $error", $code, $prevException);
public static function requestFailed(
string $error,
int $code,
RequestException $prevException,
FailedRequest $failedRequest,
): self {
return new static("[Relesys] API request failed: $error", $code, $prevException, $failedRequest);
}
}
12 changes: 12 additions & 0 deletions src/HttpClient/FailedRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Getsno\Relesys\HttpClient;

class FailedRequest
{
public function __construct(
public readonly string $method,
public readonly string $url,
public readonly array $params,
) {}
}
8 changes: 7 additions & 1 deletion src/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ protected function sendRequest(HttpMethod $type, string $path, array $params = [
return $response->json();
} catch (RequestException $e) {
$error = $e->response?->object()->error ?? $e->getMessage();
throw RelesysHttpClientException::requestFailed($error, $e->getCode(), $e);

throw RelesysHttpClientException::requestFailed(
$error,
$e->getCode(),
$e,
new FailedRequest($type->value, self::BASE_URI . $path, $params)
);
}
}

Expand Down

0 comments on commit d9255bc

Please sign in to comment.