Skip to content

Commit 7db8edf

Browse files
authored
Merge pull request ClickHouse#11446 from ClickHouse/aku/better-error-messages
Improve error messages
2 parents eaaf005 + 435f53e commit 7db8edf

File tree

7 files changed

+31
-13
lines changed

7 files changed

+31
-13
lines changed

src/AggregateFunctions/parseAggregateFunctionParameters.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ Array getAggregateFunctionParametersArray(const ASTPtr & expression_list, const
2727
const auto * literal = parameters[i]->as<ASTLiteral>();
2828
if (!literal)
2929
{
30-
throw Exception("Parameters to aggregate functions must be literals" + (error_context.empty() ? "" : " (in " + error_context +")"),
31-
ErrorCodes::PARAMETERS_TO_AGGREGATE_FUNCTIONS_MUST_BE_LITERALS);
30+
throw Exception(
31+
ErrorCodes::PARAMETERS_TO_AGGREGATE_FUNCTIONS_MUST_BE_LITERALS,
32+
"Parameters to aggregate functions must be literals. "
33+
"Got parameter '{}'{}",
34+
parameters[i]->formatForErrorMessage(),
35+
(error_context.empty() ? "" : " (in " + error_context +")"));
3236
}
3337

3438
params_row[i] = literal->value;

src/Common/Exception.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <Common/StackTrace.h>
1010

11+
#include <fmt/format.h>
12+
1113
namespace Poco { class Logger; }
1214

1315

@@ -20,8 +22,14 @@ class Exception : public Poco::Exception
2022
Exception() = default;
2123
Exception(const std::string & msg, int code);
2224

23-
enum CreateFromPocoTag { CreateFromPoco };
24-
enum CreateFromSTDTag { CreateFromSTD };
25+
// Format message with fmt::format, like the logging functions.
26+
template <typename ...Fmt>
27+
Exception(int code, Fmt&&... fmt)
28+
: Exception(fmt::format(std::forward<Fmt>(fmt)...), code)
29+
{}
30+
31+
struct CreateFromPocoTag {};
32+
struct CreateFromSTDTag {};
2533

2634
Exception(CreateFromPocoTag, const Poco::Exception & exc);
2735
Exception(CreateFromSTDTag, const std::exception & exc);

src/DataTypes/DataTypeAggregateFunction.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,11 @@ static DataTypePtr create(const ASTPtr & arguments)
362362
{
363363
const auto * literal = parameters[i]->as<ASTLiteral>();
364364
if (!literal)
365-
throw Exception("Parameters to aggregate functions must be literals",
366-
ErrorCodes::PARAMETERS_TO_AGGREGATE_FUNCTIONS_MUST_BE_LITERALS);
365+
throw Exception(
366+
ErrorCodes::PARAMETERS_TO_AGGREGATE_FUNCTIONS_MUST_BE_LITERALS,
367+
"Parameters to aggregate functions must be literals. "
368+
"Got parameter '{}' for function '{}'",
369+
parameters[i]->formatForErrorMessage(), function_name);
367370

368371
params_row[i] = literal->value;
369372
}

src/DataTypes/DataTypeCustomSimpleAggregateFunction.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@ static std::pair<DataTypePtr, DataTypeCustomDescPtr> create(const ASTPtr & argum
8282
{
8383
const ASTLiteral * lit = parameters[i]->as<ASTLiteral>();
8484
if (!lit)
85-
throw Exception("Parameters to aggregate functions must be literals",
86-
ErrorCodes::PARAMETERS_TO_AGGREGATE_FUNCTIONS_MUST_BE_LITERALS);
85+
throw Exception(
86+
ErrorCodes::PARAMETERS_TO_AGGREGATE_FUNCTIONS_MUST_BE_LITERALS,
87+
"Parameters to aggregate functions must be literals. "
88+
"Got parameter '{}' for function '{}'",
89+
parameters[i]->formatForErrorMessage(), function_name);
8790

8891
params_row[i] = lit->value;
8992
}

src/Databases/DatabaseOnDisk.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ void DatabaseOnDisk::renameTable(
294294
{
295295
attachTable(table_name, table, table_data_relative_path);
296296
/// Better diagnostics.
297-
throw Exception{Exception::CreateFromPoco, e};
297+
throw Exception{Exception::CreateFromPocoTag{}, e};
298298
}
299299

300300
/// Now table data are moved to new database, so we must add metadata and attach table to new database

src/Processors/Formats/Impl/AvroRowInputFormat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ class AvroConfluentRowInputFormat::SchemaRegistry
638638
}
639639
catch (const Poco::Exception & e)
640640
{
641-
throw Exception(Exception::CreateFromPoco, e);
641+
throw Exception(Exception::CreateFromPocoTag{}, e);
642642
}
643643
catch (const avro::Exception & e)
644644
{

src/Server/TCPHandler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,17 +304,17 @@ void TCPHandler::runImpl()
304304
* We will try to send exception to the client in any case - see below.
305305
*/
306306
state.io.onException();
307-
exception.emplace(Exception::CreateFromPoco, e);
307+
exception.emplace(Exception::CreateFromPocoTag{}, e);
308308
}
309309
catch (const Poco::Exception & e)
310310
{
311311
state.io.onException();
312-
exception.emplace(Exception::CreateFromPoco, e);
312+
exception.emplace(Exception::CreateFromPocoTag{}, e);
313313
}
314314
catch (const std::exception & e)
315315
{
316316
state.io.onException();
317-
exception.emplace(Exception::CreateFromSTD, e);
317+
exception.emplace(Exception::CreateFromSTDTag{}, e);
318318
}
319319
catch (...)
320320
{

0 commit comments

Comments
 (0)