NamespaceExceptionFactory.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Aws\Common\Exception;
  17. use Aws\Common\Exception\Parser\ExceptionParserInterface;
  18. use Guzzle\Http\Message\RequestInterface;
  19. use Guzzle\Http\Message\Response;
  20. /**
  21. * Attempts to create exceptions by inferring the name from the code and a base
  22. * namespace that contains exceptions. Exception classes are expected to be in
  23. * upper camelCase and always end in 'Exception'. 'Exception' will be appended
  24. * if it is not present in the exception code.
  25. */
  26. class NamespaceExceptionFactory implements ExceptionFactoryInterface
  27. {
  28. /**
  29. * @var ExceptionParserInterface $parser Parser used to parse responses
  30. */
  31. protected $parser;
  32. /**
  33. * @var string Base namespace containing exception classes
  34. */
  35. protected $baseNamespace;
  36. /**
  37. * @var string Default class to instantiate if a match is not found
  38. */
  39. protected $defaultException;
  40. /**
  41. * @param ExceptionParserInterface $parser Parser used to parse exceptions
  42. * @param string $baseNamespace Namespace containing exceptions
  43. * @param string $defaultException Default class to use if one is not mapped
  44. */
  45. public function __construct(
  46. ExceptionParserInterface $parser,
  47. $baseNamespace,
  48. $defaultException = 'Aws\Common\Exception\ServiceResponseException'
  49. ) {
  50. $this->parser = $parser;
  51. $this->baseNamespace = $baseNamespace;
  52. $this->defaultException = $defaultException;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function fromResponse(RequestInterface $request, Response $response)
  58. {
  59. $parts = $this->parser->parse($request, $response);
  60. // Removing leading 'AWS.' and embedded periods
  61. $className = $this->baseNamespace . '\\' . str_replace(array('AWS.', '.'), '', $parts['code']);
  62. if (substr($className, -9) !== 'Exception') {
  63. $className .= 'Exception';
  64. }
  65. $className = class_exists($className) ? $className : $this->defaultException;
  66. return $this->createException($className, $request, $response, $parts);
  67. }
  68. /**
  69. * Create an prepare an exception object
  70. *
  71. * @param string $className Name of the class to create
  72. * @param RequestInterface $request Request
  73. * @param Response $response Response received
  74. * @param array $parts Parsed exception data
  75. *
  76. * @return \Exception
  77. */
  78. protected function createException($className, RequestInterface $request, Response $response, array $parts)
  79. {
  80. $class = new $className($parts['message']);
  81. if ($class instanceof ServiceResponseException) {
  82. $class->setExceptionCode($parts['code']);
  83. $class->setExceptionType($parts['type']);
  84. $class->setResponse($response);
  85. $class->setRequest($request);
  86. $class->setRequestId($parts['request_id']);
  87. }
  88. return $class;
  89. }
  90. }