From ba85357dba001fdff673a732e95ec266236c065c Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Tue, 9 Aug 2022 15:38:41 +0300 Subject: [PATCH] Trying to improve PHP validation error message --- targets/php/php_tpl.inc.php | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/targets/php/php_tpl.inc.php b/targets/php/php_tpl.inc.php index 9af4645..62e55b7 100644 --- a/targets/php/php_tpl.inc.php +++ b/targets/php/php_tpl.inc.php @@ -60,11 +60,11 @@ class %class% %parent_class% function import(&\$message, \$assoc = false, \$root = true) { - \$IDX = 0; + \$IDX = -1; try { if(!is_array(\$message)) - throw new Exception("Bad message: \$message"); + throw new Exception("Bad data: \$message"); try { @@ -72,8 +72,13 @@ class %class% %parent_class% } catch(Exception \$e) { + if(\$IDX > -1) + { \$FIELDS = self::CLASS_FIELDS(); - throw new Exception("Error while filling field '{\$FIELDS[\$IDX]}': " . \$e->getMessage()); + throw new Exception(\$e->getMessage() . " < {\$FIELDS[\$IDX]}"); + } + else + throw \$e; } if(\$root && \$assoc && sizeof(\$message) > 0) @@ -81,7 +86,7 @@ class %class% %parent_class% } catch(Exception \$e) { - throw new Exception("Error while filling fields of '%class%':" . \$e->getMessage()); + throw new Exception(\$e->getMessage() . " < (%class%)"); } return \$IDX; } @@ -106,7 +111,7 @@ class %class% %parent_class% } catch(Exception \$e) { - throw new Exception("Error while dumping fields of '%class%'->\$__last_var: ". PHP_EOL . serialize(\$__last_val) . PHP_EOL."\t" . \$e->getMessage()); + throw new Exception("[%class%]\n->\$__last_var\n" . serialize(\$__last_val) . PHP_EOL."\t" . \$e->getMessage()); } } } @@ -255,17 +260,22 @@ class %class% %parent_class% try { if(!\$message || !is_array(\$message)) - throw new Exception("Bad message"); + throw new Exception("Bad data"); - \$IDX = 0; + \$IDX = -1; try { %fill_fields% } catch(Exception \$e) { + if(\$IDX > -1) + { \$FIELDS = self::CLASS_FIELDS(); - throw new Exception("Error while filling field '{\$FIELDS[\$IDX]}': " . \$e->getMessage()); + throw new Exception(\$e->getMessage() . " < {\$FIELDS[\$IDX]}"); + } + else + throw \$e; } if(\$root && \$assoc && sizeof(\$message) > 0) @@ -273,7 +283,7 @@ class %class% %parent_class% } catch(Exception \$e) { - throw new Exception("Error while filling fields of '%class%':" . \$e->getMessage()); + throw new Exception(\$e->getMessage() . " < (%class%)"); } } @@ -289,7 +299,7 @@ class %class% %parent_class% } catch(Exception \$e) { - throw new Exception("Error while dumping fields of '%class%':" . \$e->getMessage()); + throw new Exception(\$e->getMessage() . "\n(%class%)"); } } }