香洲二院小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
mini_xzey/bootstrap/app.php

91 lines
3.3 KiB

<?php
use App\Exceptions\GeneralException;
use App\Http\Middleware\RecordApiLog;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
api: __DIR__.'/../routes/api.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
apiPrefix: 'Api',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->alias([
'apiLog' => RecordApiLog::class
]);
})
->withExceptions(function (Exceptions $exceptions) {
$exceptions->dontReportDuplicates();
$exceptions->dontReport([
GeneralException::class,
]);
// Exception错误拦截
$exceptions->render(function (Exception $exception, Request $request) {
// 错误消息
$err_msg = $exception->getMessage();
$record_msg = $err_msg. ' in '. $exception->getFile(). ':'. $exception->getLine();
// 校验入参错误
if ($exception instanceof ValidationException) {
$err_arr = $exception->errors();
$err_arr = array_shift($err_arr);
return jsonResponse(Response::HTTP_BAD_REQUEST, reset($err_arr));
}
// 接口请求方式不在路由设置中或不被允许时
if($exception instanceof MethodNotAllowedHttpException) {
return jsonResponse(Response::HTTP_METHOD_NOT_ALLOWED, 'Method Not Allow.');
}
// 404异常扩展设置
if ($exception instanceof NotFoundHttpException) {
return jsonResponse(Response::HTTP_NOT_FOUND, 'Not Found.');
}
// 权限
if ($exception instanceof AuthenticationException) {
return jsonResponse(Response::HTTP_UNAUTHORIZED, 'Unauthenticated');
}
// 数据库错误
if($exception instanceof PDOException) {
recordLog('DBError', $record_msg);
return jsonResponse(Response::HTTP_INTERNAL_SERVER_ERROR, $err_msg);
}
// 自定义错误
if ($exception instanceof GeneralException) {
return jsonResponse(Response::HTTP_BAD_REQUEST, $err_msg);
}
// 500 错误拦截
recordLog('AppError', $record_msg);
return jsonResponse(Response::HTTP_INTERNAL_SERVER_ERROR, $record_msg);
});
// Throwable错误拦截
$exceptions->render(function (Throwable $e, $request) {
// 错误消息
$err_msg = $e->getMessage();
$record_msg = $err_msg. ' in '. $e->getFile(). ':'. $e->getLine();
// 500 错误拦截
recordLog('AppError', $record_msg);
return jsonResponse(Response::HTTP_INTERNAL_SERVER_ERROR, $record_msg);
});
})->create();