Skip to content

Commit cbc28da

Browse files
authored
Add source location (#758)
Fixes #523 Reproduce `elog(ERROR, ...)` implementation to override the source function/file/line.
1 parent 6436704 commit cbc28da

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

include/pgduckdb/utility/cpp_wrapper.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace pgduckdb {
1010

1111
template <typename Func, Func func, typename... FuncArgs>
1212
typename std::invoke_result<Func, FuncArgs &...>::type
13-
__CPPFunctionGuard__(const char *func_name, FuncArgs &...args) {
13+
__CPPFunctionGuard__(const char *func_name, const char *file_name, int line, FuncArgs &...args) {
1414
const char *error_message = nullptr;
1515
auto pg_es_start = PG_exception_stack;
1616
try {
@@ -73,12 +73,18 @@ __CPPFunctionGuard__(const char *func_name, FuncArgs &...args) {
7373
PG_exception_stack = pg_es_start;
7474
}
7575

76-
elog(ERROR, "(PGDuckDB/%s) %s", func_name, error_message);
76+
// Simplified version of `elog(ERROR, ...)`, with arguments inlined
77+
if (errstart_cold(ERROR, TEXTDOMAIN)) {
78+
errmsg_internal("(PGDuckDB/%s) %s", func_name, error_message);
79+
errfinish(file_name, line, func_name);
80+
}
81+
pg_unreachable();
7782
}
7883

7984
} // namespace pgduckdb
8085

81-
#define InvokeCPPFunc(FUNC, ...) pgduckdb::__CPPFunctionGuard__<decltype(&FUNC), &FUNC>(#FUNC, ##__VA_ARGS__)
86+
#define InvokeCPPFunc(FUNC, ...) \
87+
pgduckdb::__CPPFunctionGuard__<decltype(&FUNC), &FUNC>(#FUNC, __FILE__, __LINE__, ##__VA_ARGS__)
8288

8389
// Wrappers
8490

test/regression/expected/execution_error.out

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,16 @@ ERROR: (PGDuckDB/Duckdb_ExecCustomScan_Cpp) Conversion Error: Could not convert
99
LINE 1: SELECT (a)::integer AS a FROM pgduckdb.public.int_as_varchar
1010
^
1111
DROP TABLE int_as_varchar;
12+
\set VERBOSITY verbose
13+
select * from duckdb.raw_query('aaaaa');
14+
WARNING: 01000: (PGDuckDB/CreatePlan) Prepared query returned an error: 'Catalog Error: Table Function with name raw_query does not exist!
15+
Did you mean "main.pragma_user_agent"?
16+
17+
LINE 1: SELECT raw_query FROM duckdb.raw_query('aaaaa'::text) raw_query(raw_query)
18+
^
19+
LOCATION: CreatePlan, pgduckdb_planner.cpp:58
20+
ERROR: XX000: (PGDuckDB/pgduckdb_raw_query_cpp) Parser Error: syntax error at or near "aaaaa"
21+
22+
LINE 1: aaaaa
23+
^
24+
LOCATION: pgduckdb_raw_query_cpp, pgduckdb_options.cpp:212

test/regression/sql/execution_error.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ INSERT INTO int_as_varchar SELECT * from (
66

77
SELECT a::INTEGER FROM int_as_varchar;
88
DROP TABLE int_as_varchar;
9+
10+
\set VERBOSITY verbose
11+
select * from duckdb.raw_query('aaaaa');

0 commit comments

Comments
 (0)