ExceptionParser.php 3.6 KB

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