From 521e7af01997ae5f47b6f2c00a4194d714257f87 Mon Sep 17 00:00:00 2001 From: Junior Santos Date: Mon, 24 Nov 2025 12:15:57 -0300 Subject: [PATCH] Improves validation exception message handling Updates the validation exception to resolve the message based on the validation errors. This ensures a more specific error message is returned to the user. The same message always comes back, making it difficult to analyze the error. --- src/Exceptions/ValidationException.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Exceptions/ValidationException.php b/src/Exceptions/ValidationException.php index f973378..4abeb90 100644 --- a/src/Exceptions/ValidationException.php +++ b/src/Exceptions/ValidationException.php @@ -20,7 +20,7 @@ class ValidationException extends Exception */ public function __construct(array $errors) { - parent::__construct('The given data failed to pass validation.'); + parent::__construct($this->resolveMessage($errors)); $this->errors = $errors; } @@ -34,4 +34,19 @@ public function errors() { return $this->errors; } + + /** + * Resolve the message based on the provided errors. + * + * @param array $errors An array containing validation errors. + * @return string The resolved message string. + */ + private function resolveMessage(array $errors): string + { + if (empty($errors)) { + return 'The given data failed to pass validation.'; + } + + return current(current($errors)); + } }