From 4da07ec3970bcab801c522d0c2042b6d27402515 Mon Sep 17 00:00:00 2001 From: Yaang Vu Date: Sun, 10 Dec 2023 22:33:58 +0700 Subject: [PATCH] fix bug validate --- src/Base/Utility/Validatable.php | 7 ++++++- src/Exception/BaseException.php | 29 +++++++++++++++++++++-------- src/lang/en/laravel-base.php | 13 +++++++------ src/lang/vi/laravel-base.php | 13 +++++++------ 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/Base/Utility/Validatable.php b/src/Base/Utility/Validatable.php index 484a2d3..3037c16 100644 --- a/src/Base/Utility/Validatable.php +++ b/src/Base/Utility/Validatable.php @@ -23,6 +23,7 @@ trait Validatable * @param bool $throwable * * @return bool|array + * @throws BadRequestException */ public function validateStoreRequest(object $request, array $rules = [], array $messages = [], array $customAttributes = [], bool $throwable = true): bool|array @@ -41,6 +42,7 @@ public function validateStoreRequest(object $request, array $rules = [], array $ * @param bool $throwable * * @return bool|array + * @throws BadRequestException */ public static function doValidate(object $request, array $rules = [], array $messages = [], array $customAttributes = [], bool $throwable = true): bool|array @@ -57,7 +59,8 @@ public static function doValidate(object $request, array $rules = [], array $mes return true; if ($throwable) - throw new BadRequestException(['messages' => $validator->errors()->toArray()]); + throw new + BadRequestException(__('laravel-base.request-not-valid'), messages: $validator->errors()->toArray()); else return $validator->errors()->toArray(); } @@ -73,6 +76,7 @@ public static function doValidate(object $request, array $rules = [], array $mes * @param bool $throwable * * @return bool|array + * @throws BadRequestException */ public function validateUpdateRequest(int|string $id, object $request, array $rules = [], array $messages = [], array $customAttributes = [], bool $throwable = true): bool|array @@ -94,6 +98,7 @@ public function validateUpdateRequest(int|string $id, object $request, array $ru * @param bool $throwable * * @return bool|array + * @throws BadRequestException */ public function validatePutUpdateRequest(int|string $id, object $request, array $rules = [], array $messages = [], array $customAttributes = [], bool $throwable = true): bool|array diff --git a/src/Exception/BaseException.php b/src/Exception/BaseException.php index e403eba..ffba4f9 100644 --- a/src/Exception/BaseException.php +++ b/src/Exception/BaseException.php @@ -12,8 +12,8 @@ class BaseException extends Exception { public int $statusCode = Response::HTTP_INTERNAL_SERVER_ERROR; - public function __construct(string $message = "", int $code = 0, - ?Throwable $previous = null, private readonly string $error = '') + public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null, + private readonly string $error = '', private readonly array $messages = []) { parent::__construct($message, $code, $previous); } @@ -21,19 +21,32 @@ public function __construct(string $message = "", int $code = 0, public function render($request): JsonResponse { $response = [ - 'message' => $this->getMessage(), - 'error' => $this->getError(), - 'code' => $this->getCode(), - 'file' => $this->getFile(), - 'line' => $this->getLine(), - 'trace' => $this->getTrace(), + 'message' => $this->getMessage(), + 'messages' => $this->getMessages(), + 'error' => $this->getError(), + 'code' => $this->getCode(), + 'file' => $this->getFile(), + 'line' => $this->getLine(), + 'trace' => $this->getTrace(), ]; Log::error($this->getMessage(), $response); + // if the application was not enabling debug mode, then return only a message + if (!config('app.debug')) + $response = [ + 'message' => $this->getMessage(), + 'messages' => $this->getMessages(), + ]; + return response()->json($response)->setStatusCode($this->statusCode); } + public function getMessages(): array + { + return $this->messages; + } + public function getError(): string { return $this->error; diff --git a/src/lang/en/laravel-base.php b/src/lang/en/laravel-base.php index 40b5872..d876adf 100644 --- a/src/lang/en/laravel-base.php +++ b/src/lang/en/laravel-base.php @@ -5,10 +5,11 @@ */ return [ - 'server-error' => 'Internal Server Error', - 'can-not-del' => 'The :attribute can not be deleted', - 'can-not-add' => 'The :attribute can not be added', - 'can-not-update' => 'The :attribute can not be updated', - 'not-found' => 'The :attribute can not be found', - 'query-error' => 'Query Error' + 'server-error' => 'Internal Server Error', + 'can-not-del' => 'The :attribute can not be deleted', + 'can-not-add' => 'The :attribute can not be added', + 'can-not-update' => 'The :attribute can not be updated', + 'not-found' => 'The :attribute can not be found', + 'query-error' => 'Query Error', + 'request-not-valid' => 'The request is not valid' ]; diff --git a/src/lang/vi/laravel-base.php b/src/lang/vi/laravel-base.php index 38047c5..a55e9d7 100644 --- a/src/lang/vi/laravel-base.php +++ b/src/lang/vi/laravel-base.php @@ -5,10 +5,11 @@ */ return [ - 'server-error' => 'Hệ thống đang gặp lỗi', - 'can-not-del' => 'Đối tượng :attribute không thể xóa', - 'can-not-add' => 'Đối tượng :attribute không thể thêm', - 'can-not-update' => 'Đối tượng :attribute không thể chỉnh sửa', - 'not-found' => 'Đối tượng :attribute không tìm thấy', - 'query-error' => 'Câu lệnh lỗi', + 'server-error' => 'Hệ thống đang gặp lỗi', + 'can-not-del' => 'Đối tượng :attribute không thể xóa', + 'can-not-add' => 'Đối tượng :attribute không thể thêm', + 'can-not-update' => 'Đối tượng :attribute không thể chỉnh sửa', + 'not-found' => 'Đối tượng :attribute không tìm thấy', + 'query-error' => 'Câu lệnh lỗi', + 'request-not-valid' => 'Dữ liệu không hợp lệ' ];