| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
namespace Webklex\PHPIMAP\Exceptions; |
| 14 |
|
| 15 |
use \Exception; |
| 16 |
use Webklex\PHPIMAP\Connection\Protocols\Response; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class ResponseException extends Exception { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public static function make(Response $response, bool $debug = false, ?Exception $exception = null): ResponseException { |
| 34 |
$message = "Command failed to process:\n"; |
| 35 |
$message .= "Causes:\n"; |
| 36 |
|
| 37 |
foreach($response->getErrors() as $error) { |
| 38 |
$message .= "\t- $error\n"; |
| 39 |
} |
| 40 |
|
| 41 |
if(!$response->data()) { |
| 42 |
$message .= "\t- Empty response\n"; |
| 43 |
} |
| 44 |
|
| 45 |
if ($debug) { |
| 46 |
$message .= self::debug_message($response); |
| 47 |
} |
| 48 |
|
| 49 |
foreach($response->getStack() as $_response) { |
| 50 |
$exception = self::make($_response, $debug, $exception); |
| 51 |
} |
| 52 |
|
| 53 |
return new self($message."Error occurred", 0, $exception); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
protected static function debug_message(Response $response): string { |
| 63 |
$commands = $response->getCommands(); |
| 64 |
$message = "Commands send:\n"; |
| 65 |
if ($commands) { |
| 66 |
foreach($commands as $command) { |
| 67 |
$message .= "\t".str_replace("\r\n", "\\r\\n", $command)."\n"; |
| 68 |
} |
| 69 |
}else{ |
| 70 |
$message .= "\tNo command send!\n"; |
| 71 |
} |
| 72 |
|
| 73 |
$responses = $response->getResponse(); |
| 74 |
$message .= "Responses received:\n"; |
| 75 |
if ($responses) { |
| 76 |
foreach($responses as $_response) { |
| 77 |
if (is_array($_response)) { |
| 78 |
foreach($_response as $value) { |
| 79 |
$message .= "\t".str_replace("\r\n", "\\r\\n", "$value")."\n"; |
| 80 |
} |
| 81 |
}else{ |
| 82 |
$message .= "\t".str_replace("\r\n", "\\r\\n", "$_response")."\n"; |
| 83 |
} |
| 84 |
} |
| 85 |
}else{ |
| 86 |
$message .= "\tNo responses received!\n"; |
| 87 |
} |
| 88 |
|
| 89 |
return $message; |
| 90 |
} |
| 91 |
} |