MultipartUploadException.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Aws\Exception;
  3. use Aws\HasMonitoringEventsTrait;
  4. use Aws\MonitoringEventsInterface;
  5. use Aws\Multipart\UploadState;
  6. class MultipartUploadException extends \RuntimeException implements
  7. MonitoringEventsInterface
  8. {
  9. use HasMonitoringEventsTrait;
  10. /** @var UploadState State of the erroneous transfer */
  11. private $state;
  12. /**
  13. * @param UploadState $state Upload state at time of the exception.
  14. * @param \Exception|array $prev Exception being thrown.
  15. */
  16. public function __construct(UploadState $state, $prev = null) {
  17. $msg = 'An exception occurred while performing a multipart upload';
  18. if (is_array($prev)) {
  19. $msg = strtr($msg, ['performing' => 'uploading parts to']);
  20. $msg .= ". The following parts had errors:\n";
  21. /** @var $error AwsException */
  22. foreach ($prev as $part => $error) {
  23. $msg .= "- Part {$part}: " . $error->getMessage(). "\n";
  24. }
  25. } elseif ($prev instanceof AwsException) {
  26. switch ($prev->getCommand()->getName()) {
  27. case 'CreateMultipartUpload':
  28. case 'InitiateMultipartUpload':
  29. $action = 'initiating';
  30. break;
  31. case 'CompleteMultipartUpload':
  32. $action = 'completing';
  33. break;
  34. }
  35. if (isset($action)) {
  36. $msg = strtr($msg, ['performing' => $action]);
  37. }
  38. $msg .= ": {$prev->getMessage()}";
  39. }
  40. if (!$prev instanceof \Exception) {
  41. $prev = null;
  42. }
  43. parent::__construct($msg, 0, $prev);
  44. $this->state = $state;
  45. }
  46. /**
  47. * Get the state of the transfer
  48. *
  49. * @return UploadState
  50. */
  51. public function getState()
  52. {
  53. return $this->state;
  54. }
  55. }