31 lines
804 B
PHP
31 lines
804 B
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use app\common\service\JsonService;
|
|
use Next\VarDumper\Dumper;
|
|
use Next\VarDumper\DumperHandler;
|
|
use support\exception\Handler;
|
|
use Throwable;
|
|
use Webman\Http\Request;
|
|
use Webman\Http\Response;
|
|
|
|
class ExceptionHandler extends Handler
|
|
{
|
|
|
|
use DumperHandler;
|
|
|
|
public function render(Request $request, Throwable $exception): Response
|
|
{
|
|
if ($exception instanceof Dumper) {
|
|
return \response(self::convertToHtml($exception));
|
|
}
|
|
// json请求返回json数据
|
|
if ($request->expectsJson()) {
|
|
return json(['code' => $exception->getCode() ?: 500, 'msg' => $exception->getMessage(), 'show' => 1]);
|
|
}
|
|
// 非json请求则返回一个页面
|
|
return new Response(200, [], $exception->getMessage());
|
|
}
|
|
}
|