如何解决GuzzleHttp中的“ Guzzle \ Plugin \ ErrorResponse \ ErrorResponsePlugin”等效项是什么
在Guzzle旧版中, Guzzle \ Plugin \ ErrorResponse \ ErrorResponsePlugin 用于在符合errorResponse
规则之一的情况下自动引发已定义的异常。
那么 GuzzleHttp 中有什么等效内容?
顺便说一下,这是旧的 ErrorResponsePlugin 类内容:
<?PHP
namespace Guzzle\Plugin\ErrorResponse;
use Guzzle\Common\Event;
use Guzzle\Http\Message\RequestInterface;
use Guzzle\Service\Command\CommandInterface;
use Guzzle\Service\Description\Operation;
use Guzzle\Plugin\ErrorResponse\Exception\ErrorResponseException;
use Symfony\Component\Eventdispatcher\EventSubscriberInterface;
/**
* Converts generic Guzzle response exceptions into errorResponse exceptions
*/
class ErrorResponsePlugin implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array('command.before_send' => array('onCommandBeforeSend',-1));
}
/**
* Adds a listener to requests before they sent from a command
*
* @param Event $event Event emitted
*/
public function onCommandBeforeSend(Event $event)
{
$command = $event['command'];
if ($operation = $command->getoperation()) {
if ($operation->getErrorResponses()) {
$request = $command->getRequest();
$request->getEventdispatcher()
->addListener('request.complete',$this->getErrorClosure($request,$command,$operation));
}
}
}
/**
* @param RequestInterface $request Request that received an error
* @param CommandInterface $command Command that created the request
* @param Operation $operation Operation that defines the request and errors
*
* @return \Closure Returns a closure
* @throws ErrorResponseException
*/
protected function getErrorClosure(RequestInterface $request,CommandInterface $command,Operation $operation)
{
return function (Event $event) use ($request,$operation) {
$response = $event['response'];
foreach ($operation->getErrorResponses() as $error) {
if (!isset($error['class'])) {
continue;
}
if (isset($error['code']) && $response->getStatusCode() != $error['code']) {
continue;
}
if (isset($error['reason']) && $response->getReasonPhrase() != $error['reason']) {
continue;
}
$className = $error['class'];
$errorClassInterface = __NAMESPACE__ . '\\ErrorResponseExceptionInterface';
if (!class_exists($className)) {
throw new ErrorResponseException("{$className} does not exist");
} elseif (!(in_array($errorClassInterface,class_implements($className)))) {
throw new ErrorResponseException("{$className} must implement {$errorClassInterface}");
}
throw $className::fromCommand($command,$response);
}
};
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。