Commit 106a31a8 by 王召彬

test

parent 21b63068
<?php
namespace Hdll\Services\Common\Exception;
use Swoft\App;
use Swoft\Bean\Annotation\ExceptionHandler;
use Swoft\Bean\Annotation\Handler;
use Swoft\Exception\RuntimeException;
use Exception;
use Swoft\Http\Message\Server\Request;
use Swoft\Http\Message\Server\Response;
use Swoft\Exception\ValidatorException;
use Swoft\Http\Server\Exception\BadRequestException;
use Swoft\Http\Server\Exception\RouteNotFoundException;
class BaseExceptionHandler
{
/**
* @Handler(Exception::class)
*
* @param Response $response
* @param \Throwable $throwable
*
* @return Response
*/
public function handlerException(Response $response, \Throwable $throwable)
{
$file = $throwable->getFile();
$line = $throwable->getLine();
$exception = $throwable->getMessage();
$code = 500;
$error_code = 0;
if($throwable instanceof ValidatorException) {
$code = 400;
} else if($throwable instanceof BadRequestException) {
$code = 400;
} else if($throwable instanceof RouteNotFoundException) {
$code = 404;
} else if(\method_exists($throwable, 'getErrcode')) {
$code = $throwable->getCode();
$error_code = $throwable->getErrcode();
}
// $data = ['msg' => $exception, 'file' => $file, 'line' => $line, 'code' => $code, 'error_code'=>$error_code];
// App::error(json_encode($data));
$data = ['error_code' => $error_code, 'msg' => $exception, 'data' => null];
return $response->json($data)->withStatus($code);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment