Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement wrapException to generalize parseConfigFileSimple #52

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions source/configy/Read.d
Original file line number Diff line number Diff line change
Expand Up @@ -287,18 +287,48 @@ public struct CLIArgs

public Nullable!T parseConfigFileSimple (T) (string path, StrictMode strict = StrictMode.Error)
{
return parseConfigFileSimple!(T)(CLIArgs(path), strict);
return wrapException(parseConfigFile!(T)(CLIArgs(path), strict));
}


/// Ditto
public Nullable!T parseConfigFileSimple (T) (in CLIArgs args, StrictMode strict = StrictMode.Error)
Nullable!T parseConfigFileSimple (T) (in CLIArgs args, StrictMode strict = StrictMode.Error)
{
try
return wrapException(parseConfigFile!(args, strict));
}

/*******************************************************************************

Wrap and print exceptions to stderr

This allows to call either `parseConfigFile` or `parseConfigString`
and pretty-print the exception:
```
int main ()
{
Node root = Loader.fromFile(args.config_path).load();
return nullable(parseConfig!T(args, root, strict));
auto configN = wrapException(
parseConfigString!Config("config.yaml", "/dev/null")
);
if (configN.isNull()) return 1; // Error path
auto config = configN.get();
// Rest of the program ...
}
```

Params:
parseCall = A call to one of the `parse*` functions, such as
`parseConfigString` or `parseConfigFile`, or anything
that would call them.

Returns:
An initialized `T` instance if reading/parsing was successful;
a `null` instance otherwise.

*******************************************************************************/

public Nullable!T wrapException (T) (lazy T parseCall)
{
try
return nullable(parseCall);
catch (ConfigException exc)
{
exc.printException();
Expand Down
Loading