mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-04 17:21:39 +09:00
Use helper class to handle various error conditions during HTTP request
This commit is contained in:
parent
6a9902c3bb
commit
6f9bb059e8
2 changed files with 63 additions and 2 deletions
|
|
@ -168,10 +168,17 @@ class HTTP
|
|||
|
||||
// Send the request.
|
||||
$start_time = microtime(true);
|
||||
$response = $guzzle->request($method, $url, $settings);
|
||||
$status_code = $response->getStatusCode() ?: 0;
|
||||
try
|
||||
{
|
||||
$response = $guzzle->request($method, $url, $settings);
|
||||
}
|
||||
catch (\Throwable $e)
|
||||
{
|
||||
$response = new Helpers\HTTPHelper($e);
|
||||
}
|
||||
|
||||
// Measure elapsed time and add a debug entry.
|
||||
$status_code = $response->getStatusCode() ?: 0;
|
||||
$elapsed_time = microtime(true) - $start_time;
|
||||
self::_debug($url, $status_code, $elapsed_time);
|
||||
|
||||
|
|
|
|||
54
common/framework/helpers/HTTPHelper.php
Normal file
54
common/framework/helpers/HTTPHelper.php
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Helpers;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use GuzzleHttp\Psr7\MessageTrait;
|
||||
|
||||
/**
|
||||
* HTTP helper class.
|
||||
*
|
||||
* We use instances of this class to wrap HTTP error conditions
|
||||
* not handled by Guzzle.
|
||||
*/
|
||||
class HTTPHelper implements ResponseInterface
|
||||
{
|
||||
use MessageTrait;
|
||||
|
||||
/**
|
||||
* Information about the exception thrown.
|
||||
*/
|
||||
protected $_exception_class = '';
|
||||
protected $_exception_code = 0;
|
||||
protected $_error_message = '';
|
||||
|
||||
/**
|
||||
* Create a HTTPHelper instance from an exception.
|
||||
*
|
||||
* @param \Throwable $exception
|
||||
*/
|
||||
public function __construct(\Throwable $exception)
|
||||
{
|
||||
$this->_exception_class = get_class($exception);
|
||||
$this->_exception_code = $exception->getCode();
|
||||
$this->_error_message = $exception->getMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Methods to implement ResponseInterface.
|
||||
*/
|
||||
public function getStatusCode(): int
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
public function getReasonPhrase(): string
|
||||
{
|
||||
return $this->_error_message;
|
||||
}
|
||||
public function withStatus($code, $reasonPhrase = ''): ResponseInterface
|
||||
{
|
||||
$new = clone $this;
|
||||
$new->_error_message = $reasonPhrase;
|
||||
return $new;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue