> */ protected $dontReport = [ // ]; /** * A list of the inputs that are never flashed for validation exceptions. * * @var array */ protected $dontFlash = [ 'current_password', 'password', 'password_confirmation', ]; /** * Register the exception handling callbacks for the application. * * @return void */ public function register() { $this->reportable(function (Throwable $e) { // }); $this->renderable(function (Throwable $e, $request) { return $this->handleException($request, $e); }); } public function handleException($request, Throwable $exception) { // Route Not Found if ($exception instanceof RouteNotFoundException) { return $this->exceptionResponse($request, $exception, Response::HTTP_NOT_FOUND, config('response-message.router_failed')); } // Not Found if ($exception instanceof NotFoundHttpException) { $message = $exception->getMessage() ?? config('response-message.not_found'); return $this->exceptionResponse($request, $exception, Response::HTTP_NOT_FOUND, $message); } // Model Not Found if ($exception instanceof ModelNotFoundException) { $exception = new NotFoundHttpException($exception->getMessage(), $exception); } // Authentication if ($exception instanceof AuthenticationException) { return $this->exceptionResponse($request, $exception, Response::HTTP_UNAUTHORIZED, config('response-message.authenticate_failed')); } // Unauthorized if ($exception instanceof UnauthorizedHttpException) { return $this->exceptionResponse($request, $exception, $exception->getStatusCode(), config('response-message.authorize_failed')); } // Http if ($exception instanceof HttpException) { return $this->exceptionResponse($request, $exception, $exception->getStatusCode(), $exception->getMessage()); } // Query if ($exception instanceof QueryException) { $this->exceptionResponse($request, $exception, Response::HTTP_INTERNAL_SERVER_ERROR, config('response-message.sql_error')); } // Validation if ($exception instanceof ValidationException) { if (!$request->is('api/*')) { return $this->convertValidationExceptionToResponse($exception, $request); } $validationData = []; foreach ($exception->errors() as $key => $item) { $validationData['errors']['validation'][$key] = $item[0]; } return $this->exceptionResponse($request, $exception, Response::HTTP_UNPROCESSABLE_ENTITY, config('response-message.validation_failed'), $validationData); } // Others return $this->exceptionResponse($request, $exception, Response::HTTP_INTERNAL_SERVER_ERROR, 'server error'); } public function exceptionResponse($request, $exception, $code, $message, $mergeData = []) { if ($code === Response::HTTP_INTERNAL_SERVER_ERROR) { Log::info($exception->getMessage()); } if ($request->is('api/*')) { $data = [ 'code' => $code, 'message' => $message, ]; if (!app()->environment('production')) { $data['errors'] = [ 'message' => $exception->getMessage(), 'trace' => $exception->getTrace(), ]; } if (!empty($mergeData)) { $data = array_merge($data, $mergeData); } return response()->json($data, $code); } } }