Skip to content

Commit

Permalink
configy: Catch and re-throw validation errors
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
VPanteleev-S7 authored and Geod24 committed Jan 18, 2025
1 parent 384a064 commit 0c7a2d7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion source/configy/read.d
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
21 changes: 21 additions & 0 deletions source/configy/test.d
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 0c7a2d7

Please sign in to comment.