ExceptionParser.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Qcloud\Cos;
  3. use Psr\Http\Message\RequestInterface;
  4. use Psr\Http\Message\ResponseInterface;
  5. /**
  6. * Parses default XML exception responses
  7. */
  8. class ExceptionParser {
  9. public function parse(RequestInterface $request, ResponseInterface $response) {
  10. $data = array(
  11. 'code' => null,
  12. 'message' => null,
  13. //'type' => $response->isClientError() ? 'client' : 'server',
  14. 'type' => 'client',
  15. 'request_id' => null,
  16. 'parsed' => null
  17. );
  18. $body = strval($response->getBody());
  19. if (empty($body)) {
  20. $this->parseHeaders($request, $response, $data);
  21. return $data;
  22. }
  23. try {
  24. $xml = new \SimpleXMLElement(utf8_encode($body));
  25. $this->parseBody($xml, $data);
  26. return $data;
  27. } catch (\Exception $e) {
  28. $data['code'] = 'PhpInternalXmlParseError';
  29. $data['message'] = 'A non-XML response was received';
  30. return $data;
  31. }
  32. }
  33. /**
  34. * Parses additional exception information from the response headers
  35. *
  36. * @param RequestInterface $request Request that was issued
  37. * @param Response $response The response from the request
  38. * @param array $data The current set of exception data
  39. */
  40. protected function parseHeaders(RequestInterface $request, ResponseInterface $response, array &$data) {
  41. $data['message'] = $response->getStatusCode() . ' ' . $response->getReasonPhrase();
  42. $requestId = $response->getHeader('x-cos-request-id');
  43. if (isset($requestId[0])) {
  44. $requestId = $requestId[0];
  45. $data['request_id'] = $requestId;
  46. $data['message'] .= " (Request-ID: $requestId)";
  47. }
  48. // Get the request
  49. $status = $response->getStatusCode();
  50. $method = $request->getMethod();
  51. // Attempt to determine code for 403s and 404s
  52. if ($status === 403) {
  53. $data['code'] = 'AccessDenied';
  54. } elseif ($method === 'HEAD' && $status === 404) {
  55. $path = explode('/', trim($request->getUri()->getPath(), '/'));
  56. $host = explode('.', $request->getUri()->getHost());
  57. $bucket = (count($host) >= 4) ? $host[0] : array_shift($path);
  58. $object = array_shift($path);
  59. if ($bucket && $object) {
  60. $data['code'] = 'NoSuchKey';
  61. } elseif ($bucket) {
  62. $data['code'] = 'NoSuchBucket';
  63. }
  64. }
  65. }
  66. /**
  67. * Parses additional exception information from the response body
  68. *
  69. * @param \SimpleXMLElement $body The response body as XML
  70. * @param array $data The current set of exception data
  71. */
  72. protected function parseBody(\SimpleXMLElement $body, array &$data) {
  73. $data['parsed'] = $body;
  74. $namespaces = $body->getDocNamespaces();
  75. if (isset($namespaces[''])) {
  76. // Account for the default namespace being defined and PHP not being able to handle it :(
  77. $body->registerXPathNamespace('ns', $namespaces['']);
  78. $prefix = 'ns:';
  79. } else {
  80. $prefix = '';
  81. }
  82. if ($tempXml = $body->xpath("//{$prefix}Code[1]")) {
  83. $data['code'] = (string) $tempXml[0];
  84. }
  85. if ($tempXml = $body->xpath("//{$prefix}Message[1]")) {
  86. $data['message'] = (string) $tempXml[0];
  87. }
  88. $tempXml = $body->xpath("//{$prefix}RequestId[1]");
  89. if (empty($tempXml)) {
  90. $tempXml = $body->xpath("//{$prefix}RequestID[1]");
  91. }
  92. if (isset($tempXml[0])) {
  93. $data['request_id'] = (string) $tempXml[0];
  94. }
  95. }
  96. }