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

[exceptions] add our own HttpException #39

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use AndroidSmsGateway\Domain\Message;
use AndroidSmsGateway\Domain\MessageState;
use Http\Client\Exception\HttpException;
use AndroidSmsGateway\Exceptions\HttpException;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Psr\Http\Client\ClientInterface;
Expand Down
71 changes: 71 additions & 0 deletions src/Exceptions/HttpException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace AndroidSmsGateway\Exceptions;

use Psr\Http\Client\RequestExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

final class HttpException extends \RuntimeException implements RequestExceptionInterface {
/**
* @var ResponseInterface
*/
protected $response;

/**
* @var RequestInterface
*/
protected $request;

/**
* @param string $message
*/
public function __construct(
$message,
RequestInterface $request,
ResponseInterface $response,
\Exception $previous = null
) {
parent::__construct($message, 0, $previous);

$this->request = $request;
$this->response = $response;

$this->code = $response->getStatusCode();
}

/**
* {@inheritdoc}
*/
public function getRequest(): RequestInterface {
return $this->request;
}

/**
* Returns the response.
*
* @return ResponseInterface
*/
public function getResponse() {
return $this->response;
}

/**
* Factory method to create a new exception with a normalized error message.
*/
public static function create(
RequestInterface $request,
ResponseInterface $response,
\Exception $previous = null
): self {
$message = sprintf(
'[url] %s [http method] %s [status code] %s [reason phrase] %s',
$request->getRequestTarget(),
$request->getMethod(),
$response->getStatusCode(),
$response->getReasonPhrase()
);

return new static($message, $request, $response, $previous);
}
}
Loading