Merge branch 'ClickHouse:master' into master

This commit is contained in:
andrc1901 2021-10-24 19:57:15 +03:00 committed by GitHub
commit eb88a77b5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 50 additions and 35 deletions

View File

@ -86,7 +86,6 @@ void Client::processError(const String & query) const
{ {
if (server_exception) if (server_exception)
{ {
bool print_stack_trace = config().getBool("stacktrace", false);
fmt::print(stderr, "Received exception from server (version {}):\n{}\n", fmt::print(stderr, "Received exception from server (version {}):\n{}\n",
server_version, server_version,
getExceptionMessage(*server_exception, print_stack_trace, true)); getExceptionMessage(*server_exception, print_stack_trace, true));
@ -225,7 +224,7 @@ bool Client::executeMultiQuery(const String & all_queries_text)
{ {
// Surprisingly, this is a client error. A server error would // Surprisingly, this is a client error. A server error would
// have been reported w/o throwing (see onReceiveSeverException()). // have been reported w/o throwing (see onReceiveSeverException()).
client_exception = std::make_unique<Exception>(getCurrentExceptionMessage(true), getCurrentExceptionCode()); client_exception = std::make_unique<Exception>(getCurrentExceptionMessage(print_stack_trace), getCurrentExceptionCode());
have_error = true; have_error = true;
} }
// Check whether the error (or its absence) matches the test hints // Check whether the error (or its absence) matches the test hints
@ -813,7 +812,7 @@ bool Client::processWithFuzzing(const String & full_query)
// uniformity. // uniformity.
// Surprisingly, this is a client exception, because we get the // Surprisingly, this is a client exception, because we get the
// server exception w/o throwing (see onReceiveException()). // server exception w/o throwing (see onReceiveException()).
client_exception = std::make_unique<Exception>(getCurrentExceptionMessage(true), getCurrentExceptionCode()); client_exception = std::make_unique<Exception>(getCurrentExceptionMessage(print_stack_trace), getCurrentExceptionCode());
have_error = true; have_error = true;
} }
@ -1179,6 +1178,7 @@ void Client::processConfig()
if (!query_id.empty()) if (!query_id.empty())
global_context->setCurrentQueryId(query_id); global_context->setCurrentQueryId(query_id);
} }
print_stack_trace = config().getBool("stacktrace", false);
if (config().has("multiquery")) if (config().has("multiquery"))
is_multiquery = true; is_multiquery = true;

View File

@ -62,7 +62,6 @@ void LocalServer::processError(const String &) const
String message; String message;
if (server_exception) if (server_exception)
{ {
bool print_stack_trace = config().getBool("stacktrace", false);
message = getExceptionMessage(*server_exception, print_stack_trace, true); message = getExceptionMessage(*server_exception, print_stack_trace, true);
} }
else if (client_exception) else if (client_exception)
@ -131,9 +130,12 @@ bool LocalServer::executeMultiQuery(const String & all_queries_text)
} }
catch (...) catch (...)
{ {
if (!is_interactive && !ignore_error)
throw;
// Surprisingly, this is a client error. A server error would // Surprisingly, this is a client error. A server error would
// have been reported w/o throwing (see onReceiveSeverException()). // have been reported w/o throwing (see onReceiveSeverException()).
client_exception = std::make_unique<Exception>(getCurrentExceptionMessage(true), getCurrentExceptionCode()); client_exception = std::make_unique<Exception>(getCurrentExceptionMessage(print_stack_trace), getCurrentExceptionCode());
have_error = true; have_error = true;
} }
@ -287,23 +289,30 @@ void LocalServer::tryInitPath()
void LocalServer::cleanup() void LocalServer::cleanup()
{ {
connection.reset(); try
if (global_context)
{ {
global_context->shutdown(); connection.reset();
global_context.reset();
if (global_context)
{
global_context->shutdown();
global_context.reset();
}
status.reset();
// Delete the temporary directory if needed.
if (temporary_directory_to_delete)
{
const auto dir = *temporary_directory_to_delete;
temporary_directory_to_delete.reset();
LOG_DEBUG(&logger(), "Removing temporary directory: {}", dir.string());
remove_all(dir);
}
} }
catch (...)
status.reset();
// Delete the temporary directory if needed.
if (temporary_directory_to_delete)
{ {
const auto dir = *temporary_directory_to_delete; tryLogCurrentException(__PRETTY_FUNCTION__);
temporary_directory_to_delete.reset();
LOG_DEBUG(&logger(), "Removing temporary directory: {}", dir.string());
remove_all(dir);
} }
} }
@ -446,23 +455,20 @@ try
cleanup(); cleanup();
return Application::EXIT_OK; return Application::EXIT_OK;
} }
catch (const DB::Exception & e)
{
cleanup();
bool print_stack_trace = config().getBool("stacktrace", false);
std::cerr << getExceptionMessage(e, print_stack_trace, true) << std::endl;
return e.code() ? e.code() : -1;
}
catch (...) catch (...)
{ {
try cleanup();
{
cleanup();
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
if (!ignore_error) std::cerr << getCurrentExceptionMessage(false) << std::endl;
std::cerr << getCurrentExceptionMessage(config().hasOption("stacktrace")) << '\n'; return getCurrentExceptionCode();
auto code = getCurrentExceptionCode();
/// If exception code isn't zero, we should return non-zero return code anyway.
return code ? code : -1;
} }
@ -485,6 +491,7 @@ void LocalServer::processConfig()
ignore_error = config().getBool("ignore-error", false); ignore_error = config().getBool("ignore-error", false);
is_multiquery = true; is_multiquery = true;
} }
print_stack_trace = config().getBool("stacktrace", false);
shared_context = Context::createShared(); shared_context = Context::createShared();
global_context = Context::createGlobal(shared_context.get()); global_context = Context::createGlobal(shared_context.get());

View File

@ -1364,9 +1364,7 @@ void ClientBase::runInteractive()
catch (const Exception & e) catch (const Exception & e)
{ {
/// We don't need to handle the test hints in the interactive mode. /// We don't need to handle the test hints in the interactive mode.
bool print_stack_trace = config().getBool("stacktrace", false);
std::cerr << "Exception on client:" << std::endl << getExceptionMessage(e, print_stack_trace, true) << std::endl << std::endl; std::cerr << "Exception on client:" << std::endl << getExceptionMessage(e, print_stack_trace, true) << std::endl << std::endl;
client_exception = std::make_unique<Exception>(e); client_exception = std::make_unique<Exception>(e);
} }

View File

@ -202,6 +202,7 @@ protected:
bool written_first_block = false; bool written_first_block = false;
size_t processed_rows = 0; /// How many rows have been read or written. size_t processed_rows = 0; /// How many rows have been read or written.
bool print_stack_trace = false;
/// The last exception that was received from the server. Is used for the /// The last exception that was received from the server. Is used for the
/// return code in batch mode. /// return code in batch mode.
std::unique_ptr<Exception> server_exception; std::unique_ptr<Exception> server_exception;

View File

@ -0,0 +1,8 @@
#!/usr/bin/env bash
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=../shell_config.sh
. "$CURDIR"/../shell_config.sh
touch test_exception
$CLICKHOUSE_LOCAL --query="SELECT 1 INTO OUTFILE 'test_exception' FORMAT Native" 2>&1 | grep -q "Code: 76. DB::ErrnoException:" && echo 'OK' || echo 'FAIL' ||: