Merge pull request #15513 from ClickHouse/aku/error-context

Add some context to an error message
This commit is contained in:
alexey-milovidov 2020-10-03 16:24:19 +03:00 committed by GitHub
commit 97d97f6b2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 27 deletions

View File

@ -22,10 +22,14 @@ public:
Exception() = default;
Exception(const std::string & msg, int code);
Exception(int code, const std::string & message)
: Exception(message, code)
{}
// Format message with fmt::format, like the logging functions.
template <typename ...Fmt>
Exception(int code, Fmt&&... fmt)
: Exception(fmt::format(std::forward<Fmt>(fmt)...), code)
template <typename ...Args>
Exception(int code, const std::string & fmt, Args&&... args)
: Exception(fmt::format(fmt, std::forward<Args>(args)...), code)
{}
struct CreateFromPocoTag {};
@ -40,7 +44,16 @@ public:
const char * what() const throw() override { return message().data(); }
/// Add something to the existing message.
void addMessage(const std::string & arg) { extendedMessage(arg); }
template <typename ...Args>
void addMessage(const std::string& format, Args&&... args)
{
extendedMessage(fmt::format(format, std::forward<Args>(args)...));
}
void addMessage(const std::string& message)
{
extendedMessage(message);
}
std::string getStackTraceString() const;

View File

@ -390,13 +390,21 @@ String BaseSettings<Traits_>::valueToStringUtil(const std::string_view & name, c
template <typename Traits_>
Field BaseSettings<Traits_>::stringToValueUtil(const std::string_view & name, const String & str)
{
const auto & accessor = Traits::Accessor::instance();
if (size_t index = accessor.find(name); index != static_cast<size_t>(-1))
return accessor.stringToValueUtil(index, str);
if constexpr (Traits::allow_custom_settings)
return Field::restoreFromDump(str);
else
BaseSettingsHelpers::throwSettingNotFound(name);
try
{
const auto & accessor = Traits::Accessor::instance();
if (size_t index = accessor.find(name); index != static_cast<size_t>(-1))
return accessor.stringToValueUtil(index, str);
if constexpr (Traits::allow_custom_settings)
return Field::restoreFromDump(str);
else
BaseSettingsHelpers::throwSettingNotFound(name);
}
catch (Exception & e)
{
e.addMessage("while parsing value '{}' for setting '{}'", str, name);
throw;
}
}
template <typename Traits_>

View File

@ -49,21 +49,15 @@ TEST(Processors, PortsNotConnected)
processors.emplace_back(std::move(source));
processors.emplace_back(std::move(sink));
auto exec = [&]()
try
{
try
{
PipelineExecutor executor(processors);
executor.execute(1);
}
catch (DB::Exception & e)
{
std::cout << e.displayText() << std::endl;
ASSERT_TRUE(e.displayText().find("pipeline") != std::string::npos);
throw;
}
};
ASSERT_THROW(exec(), DB::Exception);
PipelineExecutor executor(processors);
executor.execute(1);
ASSERT_TRUE(false) << "Should have thrown.";
}
catch (DB::Exception & e)
{
std::cout << e.displayText() << std::endl;
ASSERT_TRUE(e.displayText().find("pipeline") != std::string::npos) << "Expected 'pipeline', got: " << e.displayText();
}
}