config/Data: add WithEach(ConfigBlockOption)
To improve error messages without making callers more complex.
This commit is contained in:
@@ -143,3 +143,10 @@ ConfigBlock::GetBlockValue(const char *name, bool default_value) const
|
||||
|
||||
return bp->GetBoolValue();
|
||||
}
|
||||
|
||||
void
|
||||
ConfigBlock::ThrowWithNested() const
|
||||
{
|
||||
std::throw_with_nested(FormatRuntimeError("Error in block on line %i",
|
||||
line));
|
||||
}
|
||||
|
||||
@@ -137,6 +137,14 @@ struct ConfigBlock {
|
||||
unsigned GetPositiveValue(const char *name, unsigned default_value) const;
|
||||
|
||||
bool GetBlockValue(const char *name, bool default_value) const;
|
||||
|
||||
/**
|
||||
* Call this method in a "catch" block to throw a nested
|
||||
* exception showing the location of this block in the
|
||||
* configuration file.
|
||||
*/
|
||||
[[noreturn]]
|
||||
void ThrowWithNested() const;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -121,6 +121,26 @@ struct ConfigData {
|
||||
|
||||
ConfigBlock &MakeBlock(ConfigBlockOption option,
|
||||
const char *key, const char *value);
|
||||
|
||||
/**
|
||||
* Invoke the given function for each instance of the
|
||||
* specified block.
|
||||
*
|
||||
* Exceptions thrown by the function will be nested in one
|
||||
* that specifies the location of the block.
|
||||
*/
|
||||
template<typename F>
|
||||
void WithEach(ConfigBlockOption option, F &&f) const {
|
||||
for (const auto &block : GetBlockList(option)) {
|
||||
block.SetUsed();
|
||||
|
||||
try {
|
||||
f(block);
|
||||
} catch (...) {
|
||||
block.ThrowWithNested();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user