diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 56af264..44e017f 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -2,7 +2,11 @@ namespace App\Exceptions; +use App\Service\ReplyService; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; +use Illuminate\Http\Request; +use Illuminate\Support\Facades\Log; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Throwable; class Handler extends ExceptionHandler @@ -23,8 +27,17 @@ class Handler extends ExceptionHandler */ public function register(): void { + $this->renderable(function (NotFoundHttpException $e, Request $request) { + return ReplyService::error( 'record not find'); + }); + + $this->renderable(function (AppException $e, Request $request) { + Log::error($e->getTraceAsString()); + return ReplyService::error( $e->getMessage()); + }); + $this->reportable(function (Throwable $e) { - // + }); } } diff --git a/app/Http/Controllers/Base/BaseController.php b/app/Http/Controllers/Base/BaseController.php index 7cab2ec..cfb0f10 100644 --- a/app/Http/Controllers/Base/BaseController.php +++ b/app/Http/Controllers/Base/BaseController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Base; use App\Const\Responses; +use App\Service\ReplyService; use Illuminate\Routing\Controller; class BaseController extends Controller @@ -12,28 +13,24 @@ class BaseController extends Controller function __call($method, $parameters) { if (isset($this->validateMethodParams[$method])) { - $a = request()->validate($this->validateMethodParams[$method]); + request()->validate($this->validateMethodParams[$method]); } parent::__call($method, $parameters); } function reply($code, $msg, $data = []): \Illuminate\Http\JsonResponse { - return response()->json([ - 'code' => $code, - 'msg' => $msg, - 'data' => $data, - ]); + return ReplyService::reply($code, $msg, $data); } function success($data = []): \Illuminate\Http\JsonResponse { - return $this->reply(Responses::CODE_SUCCESS, 'success', $data); + return ReplyService::success($data); } function error($msg = 'error', $data = []): \Illuminate\Http\JsonResponse { - return $this->reply(Responses::CODE_ERROR, $msg, $data); + ReplyService::error($msg, $data); } } diff --git a/app/Service/ReplyService.php b/app/Service/ReplyService.php new file mode 100644 index 0000000..6fe089f --- /dev/null +++ b/app/Service/ReplyService.php @@ -0,0 +1,28 @@ +json([ + 'code' => $code, + 'msg' => $msg, + 'data' => $data, + ]); + } + + static function success($data = []): \Illuminate\Http\JsonResponse + { + return self::reply(Responses::CODE_SUCCESS, 'success', $data); + } + + static function error($msg = 'error', $data = []): \Illuminate\Http\JsonResponse + { + return self::reply(Responses::CODE_ERROR, $msg, $data); + } + +}