1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Marwan Al-Soltany <[email protected]> |
4
|
|
|
* @copyright Marwan Al-Soltany 2020 |
5
|
|
|
* For the full copyright and license information, please view |
6
|
|
|
* the LICENSE file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace MAKS\AmqpAgent\Exception; |
10
|
|
|
|
11
|
|
|
use Exception as CoreException; |
12
|
|
|
use MAKS\AmqpAgent\Helper\Utility; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* AMQP Agent base exception class. |
16
|
|
|
* @since 1.0.0 |
17
|
|
|
*/ |
18
|
|
|
class AmqpAgentException extends CoreException |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Redefine the exception so message is not an optional parameter. |
22
|
|
|
* @param string $message |
23
|
|
|
* @param int $code |
24
|
|
|
* @param CoreException $previous |
25
|
|
|
*/ |
26
|
22 |
|
public function __construct(string $message, int $code = 0, CoreException $previous = null) |
27
|
|
|
{ |
28
|
22 |
|
parent::__construct($message, $code, $previous); |
29
|
22 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* String representation of the object. |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
1 |
|
public function __toString() |
36
|
|
|
{ |
37
|
1 |
|
return static::class . ": [{$this->code}]: {$this->message}\n{$this->getTraceAsString()}\n"; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Rethrows an exception with an additional message. |
42
|
|
|
* @param CoreException $exception The exception to rethrow. |
43
|
|
|
* @param string|null $message An additional message to add to the wrapping exception before the message of the passed exception. |
44
|
|
|
* @param string|bool $wrap Wether to throw the exception using the passed class (FQN), in the same exception type (true), or wrap it with the class this method was called on (false). Any other value will be translated to false. |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
5 |
|
public static function rethrow(CoreException $exception, ?string $message = null, $wrap = false): void |
48
|
|
|
{ |
49
|
5 |
|
if (null === $message) { |
50
|
4 |
|
$trace = Utility::backtrace(['file', 'line', 'class', 'function']); |
51
|
4 |
|
$prefix = (isset($trace['class']) ? "{$trace['class']}::" : "{$trace['file']}({$trace['line']}): "); |
52
|
4 |
|
$suffix = "{$trace['function']}() failed!"; |
53
|
4 |
|
$message = 'Rethrown Exception: ' . $prefix . $suffix . ' '; |
54
|
|
|
} else { |
55
|
1 |
|
$message = strlen($message) ? $message . ' ' : $message; |
56
|
|
|
} |
57
|
|
|
|
58
|
5 |
|
$error = is_string($wrap) |
59
|
|
|
? ( |
60
|
1 |
|
class_exists($wrap) && is_subclass_of($wrap, 'Exception') |
61
|
1 |
|
? $wrap |
62
|
1 |
|
: static::class |
63
|
|
|
) |
64
|
|
|
: ( |
65
|
4 |
|
boolval($wrap) |
66
|
1 |
|
? get_class($exception) |
67
|
5 |
|
: static::class |
68
|
|
|
); |
69
|
|
|
|
70
|
5 |
|
throw new $error($message . (string)$exception->getMessage(), (int)$exception->getCode(), $exception); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Rethrows an exception with an additional message. |
75
|
|
|
* @deprecated 1.2.0 Use `self::rethrow()` instead. |
76
|
|
|
* @param CoreException $exception The exception to rethrow. |
77
|
|
|
* @param string|null $message An additional message to add to the wrapping exception before the message of the passed exception. |
78
|
|
|
* @param string|bool $wrap Wether to throw the exception using the passed class (FQN), in the same exception type (true), or wrap it with the class this method was called on (false). Any other value will be translated to false. |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
1 |
|
public static function rethrowException(CoreException $exception, ?string $message = null, $wrap = false): void |
82
|
|
|
{ |
83
|
1 |
|
static::rethrow($exception, $message, $wrap); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|