From 0c7a2d7e759147be744140275346d9ff96873676 Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev <108007295+VPanteleev-S7@users.noreply.github.com> Date: Sat, 18 Jan 2025 10:52:18 +0000 Subject: [PATCH] configy: Catch and re-throw validation errors The documentation states: > If validation of individual fields is not enough and the section as > a whole needs to be validated, one can implement a void validate() > const method which throws an exception in the even of a validation > failure. **The library will rethrow this Exception with the > file/line information pointing to the section itself, and not any > individual field.** The emphasized section was not actually implemented - exceptions thrown from validate() functions bubbled up to the top level and were printed without any context. Fix this by implementing the functionality. --- source/configy/read.d | 2 +- source/configy/test.d | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/source/configy/read.d b/source/configy/read.d index 5f842a6..7885d8b 100644 --- a/source/configy/read.d +++ b/source/configy/read.d @@ -653,7 +653,7 @@ private TLFR.Type parseMapping (alias TLFR) { dbgWrite("%s: Calling `%s` method", TLFR.Type.stringof.paint(Cyan), "validate()".paint(Green)); - result.validate(); + wrapConstruct(result.validate(), path, Location.get(node)); } else { diff --git a/source/configy/test.d b/source/configy/test.d index 330acbf..1145aaa 100644 --- a/source/configy/test.d +++ b/source/configy/test.d @@ -332,11 +332,22 @@ unittest public int value; } + static struct ThrowingValidate + { + void validate() const + { + throw new Exception("Bad data, try again"); + } + + public int value; + } + static struct InnerConfig { public int value; @Optional ThrowingCtor ctor; @Optional ThrowingFromString fromString; + @Optional ThrowingValidate validated; @Converter!int( (Node value) { @@ -374,6 +385,16 @@ unittest assert(exc.toString() == "/dev/null(2:14): config.fromString: Some meaningful error message"); } + try + { + auto result = parseConfigString!Config("config:\n value: 42\n validated:\n value: 42", "/dev/null"); + assert(0); + } + catch (ConfigException exc) + { + assert(exc.toString() == "/dev/null(3:4): config.validated: Bad data, try again"); + } + try { auto result = parseConfigString!Config("config:\n value: 42\n converter: 42", "/dev/null");