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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
<?php
namespace Akamai\Open\EdgeGrid\Handler;
use Akamai\Open\EdgeGrid\Exception\HandlerException\IOException;
class Debug
{
protected static $messages = [
403 => [
"This indicates a problem with authorization.\n",
"Please ensure that the credentials you created for this script\n",
"have the necessary permissions in the Luna portal.\n",
],
400 => [
'This indicates a problem with authentication or headers.',
'Please ensure that the .edgerc file is formatted correctly.',
'If you still have issues, please use gen_edgerc.php to generate the credentials',
],
401 => 400,
404 => [
"This means that the page does not exist as requested.\n",
"Please ensure that the URL you're calling is correctly formatted\n",
"or look at other examples to make sure yours matches.\n",
]
];
protected $fp;
public function __construct($resource = null)
{
$fp = $resource;
if (!is_resource($fp) && $fp !== null) {
$fp = @fopen($fp, 'ab+');
if (!$fp) {
throw new IOException('Unable to use resource: ' . (string) $resource);
}
}
if ($fp === null) {
$fp = fopen('php://stderr', 'ab');
}
$this->fp = $fp;
}
public function __invoke(callable $handler)
{
$colors = [
'red' => '',
'yellow' => '',
'cyan' => '',
'reset' => '',
];
if (PHP_SAPI === 'cli') {
$colors = [
'red' => "\x1b[31;01m",
'yellow' => "\x1b[33;01m",
'cyan' => "\x1b[36;01m",
'reset' => "\x1b[39;49;00m",
];
}
return function (\Psr\Http\Message\RequestInterface $request, array $config) use ($handler, $colors) {
return $handler($request, $config)->then(
function (\Psr\Http\Message\ResponseInterface $response) use ($colors, $request) {
$statusCode = $response->getStatusCode();
if ($statusCode > 399 && $statusCode < 600) {
$body = trim($response->getBody());
$result = json_decode($body);
if ($result !== null) {
if (isset($result->detail)) {
$detail = $result->detail;
} else {
$detail = json_encode($result, JSON_PRETTY_PRINT);
}
} else {
$detail = (!empty(trim($body))) ? $body : 'No response body returned';
}
$out = [];
$out[] = "{$colors['red']}===> [ERROR] Call to %s failed with a %s result";
if (isset(self::$messages[$statusCode])) {
$message = self::$messages[$statusCode];
if (is_int($message) && isset(self::$messages[$message])) {
$message = self::$messages[$message];
}
foreach ($message as $line) {
$out[] = '===> [ERROR] ' . $line;
}
}
$out[] = '===> [ERROR] Problem details:';
$out[] = $detail;
$out = sprintf(
implode("\n", $out),
$request->getUri()->getPath(),
$response->getStatusCode() . ' ' . $response->getReasonPhrase(),
$detail
);
fwrite($this->fp, $out);
fwrite($this->fp, "{$colors['reset']}\n");
}
return $response;
},
function (\Exception $reason) {
return new \GuzzleHttp\Promise\RejectedPromise($reason);
}
);
};
}
}