Commit 18726a65 by wulei@ldy.com

fix

1 parent d9fdb78d
Showing with 19 additions and 6 deletions
...@@ -10,8 +10,10 @@ use Illuminate\Database\QueryException; ...@@ -10,8 +10,10 @@ use Illuminate\Database\QueryException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException; use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Throwable; use Throwable;
class Handler extends ExceptionHandler class Handler extends ExceptionHandler
...@@ -45,11 +47,11 @@ class Handler extends ExceptionHandler ...@@ -45,11 +47,11 @@ class Handler extends ExceptionHandler
{ {
$this->renderable(function(Throwable $e, Request $request) { $this->renderable(function(Throwable $e, Request $request) {
if (strstr($request->getRequestUri(), '/api') || $request->expectsJson()) { if (strstr($request->getRequestUri(), '/api') || $request->expectsJson()) {
if ($e instanceof NotFoundHttpException) { if ($e instanceof NotFoundHttpException || $e instanceof RouteNotFoundException) {
if ($e->getPrevious() instanceof ModelNotFoundException) { if ($e->getPrevious() instanceof ModelNotFoundException) {
return ApiResponse::error(Response::HTTP_NOT_FOUND, '记录找不到了'); return ApiResponse::error(Response::HTTP_NOT_FOUND, '数据找不到了');
} }
return ApiResponse::error(Response::HTTP_NOT_FOUND, '未知接口'); return ApiResponse::error(Response::HTTP_NOT_FOUND, $e->getMessage());
} elseif ($e instanceof AuthenticationException) { } elseif ($e instanceof AuthenticationException) {
return ApiResponse::error(Response::HTTP_UNAUTHORIZED, '未授权的访问'); return ApiResponse::error(Response::HTTP_UNAUTHORIZED, '未授权的访问');
} elseif ($e instanceof AuthorizationException) { } elseif ($e instanceof AuthorizationException) {
...@@ -57,10 +59,21 @@ class Handler extends ExceptionHandler ...@@ -57,10 +59,21 @@ class Handler extends ExceptionHandler
} elseif ($e instanceof ValidationException) { } elseif ($e instanceof ValidationException) {
return ApiResponse::error(Response::HTTP_UNPROCESSABLE_ENTITY, '数据验证失败', $e->errors()); return ApiResponse::error(Response::HTTP_UNPROCESSABLE_ENTITY, '数据验证失败', $e->errors());
} elseif ($e instanceof QueryException) { } elseif ($e instanceof QueryException) {
return ApiResponse::error(Response::HTTP_INTERNAL_SERVER_ERROR, '数据错误'); return ApiResponse::error(Response::HTTP_INTERNAL_SERVER_ERROR, '数据查询异常', config('app.env') <> 'production' ? [
} else { 'sql' => $e->getSql(),
return ApiResponse::error(); 'binds' => $e->getBindings(),
'message' => $e->getMessage(),
] : null);
} }
Log::error($e->getMessage(), [
'path' => request()->path(),
'query' => request()->query(),
'class' => get_class($e),
'file' => $e->getFile(). ':' . $e->getLine()
]);
return ApiResponse::error();
} }
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!