AdapterException.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Rest\Exception;
  6. use Temando\Shipping\Rest\Response\Document\Errors;
  7. use Temando\Shipping\Rest\Response\DataObject\Error;
  8. /**
  9. * Temando REST Adapter Exception – parsed Http Exception
  10. *
  11. * @package Temando\Shipping\Rest
  12. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  13. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  14. * @link https://www.temando.com/
  15. */
  16. class AdapterException extends RestException
  17. {
  18. /**
  19. * @param \Exception $cause
  20. * @return static
  21. */
  22. public static function create(\Exception $cause)
  23. {
  24. $message = 'API connection failed';
  25. return new static($message, $cause->getCode(), $cause);
  26. }
  27. /**
  28. * @param Errors $errors
  29. * @param \Exception|null $cause
  30. * @return static
  31. */
  32. public static function errorResponse(Errors $errors, \Exception $cause = null)
  33. {
  34. $messages = array_map(function (Error $error) {
  35. $message = $error->getDetail() ?: $error->getTitle();
  36. return sprintf('%s: %s', $error->getCode(), $message);
  37. }, $errors->getErrors());
  38. $messages = implode(', ', $messages);
  39. if ($cause !== null) {
  40. return new static($messages, $cause->getCode(), $cause);
  41. }
  42. return new static($messages);
  43. }
  44. }