From 11136fb4fa274959e6017c6a7906c70167d51a78 Mon Sep 17 00:00:00 2001 From: Josh Anderson Date: Thu, 12 Nov 2020 21:21:06 -0600 Subject: [PATCH 1/2] Make AbstractHandler compatible with Throwable and the latest version of Laravel --- src/Handlers/AbstractHandler.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Handlers/AbstractHandler.php b/src/Handlers/AbstractHandler.php index 31508a2..89fa9f3 100644 --- a/src/Handlers/AbstractHandler.php +++ b/src/Handlers/AbstractHandler.php @@ -2,6 +2,7 @@ namespace SMartins\Exceptions\Handlers; +use Throwable; use Exception; use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Auth\AuthenticationException; @@ -55,7 +56,7 @@ abstract class AbstractHandler * * @param Exception $e */ - public function __construct(Exception $e) + public function __construct(Throwable $e) { $this->exception = $e; } From a726bb6dffd5dc4fca9d0cc2ded4b76310023464 Mon Sep 17 00:00:00 2001 From: Josh Anderson Date: Thu, 12 Nov 2020 23:41:45 -0600 Subject: [PATCH 2/2] Make validation handler compatible with manually thrown validation messages (via withMessages) --- src/Handlers/ValidationHandler.php | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Handlers/ValidationHandler.php b/src/Handlers/ValidationHandler.php index b22b3b2..d8129e6 100644 --- a/src/Handlers/ValidationHandler.php +++ b/src/Handlers/ValidationHandler.php @@ -48,12 +48,22 @@ public function handle() */ public function getValidationTitle(array $failedFieldsRules, string $key, string $field) { - $title = __('exception::exceptions.validation.title', [ - 'fails' => strtolower(array_keys($failedFieldsRules[$field])[$key]), - 'field' => $field, - ]); + if (empty($failedFieldsRules)) { + $fails = ''; + } else { + $fails = strtolower(array_keys($failedFieldsRules[$field])[$key]); + } - return is_array($title) ? $title[0] : $title; + $title = __('exception::exceptions.validation.title', [ + 'fails' => $fails, + 'field' => $field, + ]); + + if (is_array($title)) { + $title = $title[0]; + } + + return ucfirst(trim($title)); } /** @@ -66,9 +76,12 @@ public function getValidationTitle(array $failedFieldsRules, string $key, string */ public function getValidationCode(array $failedFieldsRules, string $key, string $field) { - $rule = strtolower(array_keys($failedFieldsRules[$field])[$key]); + if (empty($failedFieldsRules)) { + return config('json-exception-handler.codes.validation'); + } - return config('json-exception-handler.codes.validation_fields.'.$field.'.'.$rule); + $rule = strtolower(array_keys($failedFieldsRules[$field])[$key]); + return config('json-exception-handler.codes.validation_fields.' . $field . '.' . $rule); } /**