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); }); })->withCommands([ SendAppointmentReminders::class, ]) ->create();