Blame view

sources/3rdparty/guzzle/common/Guzzle/Common/Exception/ExceptionCollection.php 2.77 KB
6d9380f96   Cédric Dupont   Update sources OC...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  <?php
  
  namespace Guzzle\Common\Exception;
  
  /**
   * Collection of exceptions
   */
  class ExceptionCollection extends \Exception implements GuzzleException, \IteratorAggregate, \Countable
  {
      /** @var array Array of Exceptions */
      protected $exceptions = array();
  
      /** @var string Succinct exception message not including sub-exceptions */
      private $shortMessage;
  
      public function __construct($message = '', $code = 0, \Exception $previous = null)
      {
          parent::__construct($message, $code, $previous);
          $this->shortMessage = $message;
      }
  
      /**
       * Set all of the exceptions
       *
       * @param array $exceptions Array of exceptions
       *
       * @return self
       */
      public function setExceptions(array $exceptions)
      {
          $this->exceptions = array();
          foreach ($exceptions as $exception) {
              $this->add($exception);
          }
  
          return $this;
      }
  
      /**
       * Add exceptions to the collection
       *
       * @param ExceptionCollection|\Exception $e Exception to add
       *
       * @return ExceptionCollection;
       */
      public function add($e)
      {
          $this->exceptions[] = $e;
          if ($this->message) {
              $this->message .= "
  ";
          }
  
          $this->message .= $this->getExceptionMessage($e, 0);
  
          return $this;
      }
  
      /**
       * Get the total number of request exceptions
       *
       * @return int
       */
      public function count()
      {
          return count($this->exceptions);
      }
  
      /**
       * Allows array-like iteration over the request exceptions
       *
       * @return \ArrayIterator
       */
      public function getIterator()
      {
          return new \ArrayIterator($this->exceptions);
      }
  
      /**
       * Get the first exception in the collection
       *
       * @return \Exception
       */
      public function getFirst()
      {
          return $this->exceptions ? $this->exceptions[0] : null;
      }
  
      private function getExceptionMessage(\Exception $e, $depth = 0)
      {
          static $sp = '    ';
          $prefix = $depth ? str_repeat($sp, $depth) : '';
          $message = "{$prefix}(" . get_class($e) . ') ' . $e->getFile() . ' line ' . $e->getLine() . "
  ";
  
          if ($e instanceof self) {
              if ($e->shortMessage) {
                  $message .= "
  {$prefix}{$sp}" . str_replace("
  ", "
  {$prefix}{$sp}", $e->shortMessage) . "
  ";
              }
              foreach ($e as $ee) {
                  $message .= "
  " . $this->getExceptionMessage($ee, $depth + 1);
              }
          }  else {
              $message .= "
  {$prefix}{$sp}" . str_replace("
  ", "
  {$prefix}{$sp}", $e->getMessage()) . "
  ";
              $message .= "
  {$prefix}{$sp}" . str_replace("
  ", "
  {$prefix}{$sp}", $e->getTraceAsString()) . "
  ";
          }
  
          return str_replace(getcwd(), '.', $message);
      }
  }