docker-webman-mongolog/api/app/exception/Handler.php

52 lines
1.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\exception;
use Throwable;
use Webman\Http\Request;
use Webman\Http\Response;
use Wandoubaba\Res;
/**
* 自定义异常处理类
* 要使用这个类请在config/exception.php中配置
* '' => app\exception\Handler::class,
*/
class Handler extends \support\exception\Handler
{
/**
* 自定义异常响应
*
* @author Aaron <chenqiang@h024.cn>
*
* @param \Webman\Http\Request $request
* @param \Throwable $exception
*
* @return \Webman\Http\Response
*/
public function render(Request $request, Throwable $exception): Response
{
$res = new Res();
$resData = null;
if (config('app.debug') == 'true') {
$resData = [
'request_path' => $request->path(),
'post_data' => $request->post(),
'query_param' => $request->get(),
'headers' => $request->header(),
];
$resData['exception'] = [
'code' => $exception->getCode(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'message' => $exception->getMessage(),
'trace' => $exception->getTrace(),
];
}
$res->setCode($exception->getCode())
->setMsg($exception->getMessage())
->setData($resData);
return json($res);
}
}