扩展了Exception 得到一个状态码
<?php
/**
* Created by PhpStorm.
* User: DELL
* Date: 2019/4/21
* Time: 15:39
*
*/
class MyHttpException extends Exception {
private $statusCode;
/**
* HttpException constructor.
* @param int $statusCode
* @param string $message
* @param int $code
* @param $exception
*/
public function __construct($statusCode, $message = '', $code = 0, $exception = NULL) {
parent::__construct($message, $code, $exception);
$this->statusCode = $statusCode;
}
/**
* @return mixed
*/
public function getStatusCode() {
return $this->statusCode;
}
function tableArray($params) {
$tableHtml = '<table style=" border:1px dashed #ccc; width: 100%" cellspacing="0" cellpadding="0">';
foreach ($params as $k => $v) {
$tableHtml .= '<tr>';
if (is_array($v)) {
$tableHtml .= '<td style="border-bottom :1px dashed #ccc;">' . $k . '</td>' . '<td style="border-left:1px dashed #ccc;" >' . tableArray($v) . '</td>';
} else {
$tableHtml .= '<td style="border-bottom:1px dashed #ccc;" >' . $k . '</td>' . '<td style="border-left:1px dashed #ccc;" >' . $v . '</td>';
}
$tableHtml .= '</tr>';
}
$tableHtml .= '</table>';
return $tableHtml;
}
/**
* showExceptionInfoWithHtml HTML格式显示异常信息
*
* @param \Throwable $e
*
* @static static
* @author JadeSouth <jadesouth@aliyun.com>
* @date 2018-09-19 20:47:53
*/
private static function showExceptionInfoWithHtml(\Throwable $e): void {
/** @var object $e */
$errorType = get_class($e);
$errorMsg = $e->getMessage();
$code = $e->getCode();
$file = $e->getFile();
$line = $e->getLine();
$traceString = $e->getTraceAsString();
$traceString = explode('#', $traceString);
$traceString = implode('<br />#', $traceString);
$traceString = substr($traceString, 6);
$htmlTemplate = '<div style="border:1px dodgerblue;font-size:18px;line-height:2.0em;padding:10px;">' . PHP_EOL;
$htmlTemplate .= '<div><i>Error Type:</i> <b>%s</b></div>' . PHP_EOL;
$htmlTemplate .= '<div><i>Error Message:</i> <b>%s</b></div>' . PHP_EOL;
$htmlTemplate .= '<div><i>Error Code:</i> <b style="color:red;">%d</b></div>' . PHP_EOL;
$htmlTemplate .= '<div><i>File:</i> <b>%s</b>, on line <b style="color:red;"> %s</b>.</div>'
. PHP_EOL;
$htmlTemplate .= '<div><div style="margin-top:20px;"><i><b>Stack Trace:</b></i></div>%s</div></div>' . PHP_EOL;
printf($htmlTemplate, $errorType, $errorMsg, $code, $file, $line, $traceString);
}
}