44 lines
1.3 KiB
PHP
Executable File
44 lines
1.3 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* @desc ExceptionHandler
|
|
* @author Tinywan(ShaoBo Wan)
|
|
* @email 756684177@qq.com
|
|
* @date 2022/3/6 14:08
|
|
*/
|
|
declare(strict_types=1);
|
|
|
|
namespace support;
|
|
|
|
use Throwable;
|
|
use Webman\Exception\ExceptionHandler;
|
|
use Webman\Http\Request;
|
|
use Webman\Http\Response;
|
|
|
|
class Exception extends ExceptionHandler
|
|
{
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @param Throwable $exception
|
|
* @return Response
|
|
*/
|
|
public function render(Request $request, Throwable $exception): Response
|
|
{
|
|
|
|
$code = $exception->getCode();
|
|
$debug = $this->_debug ?? $this->debug;
|
|
if ($request->expectsJson()) {
|
|
$json = ['code' => $code ?: 500, 'msg' => $debug ? $exception->getMessage() : 'Server internal error'];
|
|
$debug && $json['traces'] = (string)$exception;
|
|
return new Response($code, ['Content-Type' => 'application/json'],
|
|
\json_encode($json, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
|
|
}
|
|
$error = $debug ? \nl2br((string)$exception) : 'Server internal error';
|
|
return new Response($code, [], $error);
|
|
// $header = array_merge(['Content-Type' => 'application/json;charset=utf-8'], $this->header);
|
|
|
|
// return new Response($this->statusCode, $header, json_encode($responseBody));
|
|
}
|
|
|
|
}
|