mirror of
https://git.postgresql.org/git/postgresql.git
synced 2026-02-07 19:17:39 +08:00
Add traceback information to PL/Python errors
This mimics the traceback information the Python interpreter prints with exceptions. Jan Urbański
This commit is contained in:
@ -36,7 +36,10 @@ ERROR: spiexceptions.SyntaxError: syntax error at or near "syntax"
|
||||
LINE 1: syntax error
|
||||
^
|
||||
QUERY: syntax error
|
||||
CONTEXT: PL/Python function "sql_syntax_error"
|
||||
CONTEXT: Traceback (most recent call last):
|
||||
PL/Python function "sql_syntax_error", line 1, in <module>
|
||||
plpy.execute("syntax error")
|
||||
PL/Python function "sql_syntax_error"
|
||||
/* check the handling of uncaught python exceptions
|
||||
*/
|
||||
CREATE FUNCTION exception_index_invalid(text) RETURNS text
|
||||
@ -45,7 +48,10 @@ CREATE FUNCTION exception_index_invalid(text) RETURNS text
|
||||
LANGUAGE plpythonu;
|
||||
SELECT exception_index_invalid('test');
|
||||
ERROR: IndexError: list index out of range
|
||||
CONTEXT: PL/Python function "exception_index_invalid"
|
||||
CONTEXT: Traceback (most recent call last):
|
||||
PL/Python function "exception_index_invalid", line 1, in <module>
|
||||
return args[1]
|
||||
PL/Python function "exception_index_invalid"
|
||||
/* check handling of nested exceptions
|
||||
*/
|
||||
CREATE FUNCTION exception_index_invalid_nested() RETURNS text
|
||||
@ -59,7 +65,10 @@ LINE 1: SELECT test5('foo')
|
||||
^
|
||||
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
|
||||
QUERY: SELECT test5('foo')
|
||||
CONTEXT: PL/Python function "exception_index_invalid_nested"
|
||||
CONTEXT: Traceback (most recent call last):
|
||||
PL/Python function "exception_index_invalid_nested", line 1, in <module>
|
||||
rv = plpy.execute("SELECT test5('foo')")
|
||||
PL/Python function "exception_index_invalid_nested"
|
||||
/* a typo
|
||||
*/
|
||||
CREATE FUNCTION invalid_type_uncaught(a text) RETURNS text
|
||||
@ -75,7 +84,10 @@ return None
|
||||
LANGUAGE plpythonu;
|
||||
SELECT invalid_type_uncaught('rick');
|
||||
ERROR: spiexceptions.UndefinedObject: type "test" does not exist
|
||||
CONTEXT: PL/Python function "invalid_type_uncaught"
|
||||
CONTEXT: Traceback (most recent call last):
|
||||
PL/Python function "invalid_type_uncaught", line 3, in <module>
|
||||
SD["plan"] = plpy.prepare(q, [ "test" ])
|
||||
PL/Python function "invalid_type_uncaught"
|
||||
/* for what it's worth catch the exception generated by
|
||||
* the typo, and return None
|
||||
*/
|
||||
@ -121,7 +133,10 @@ return None
|
||||
LANGUAGE plpythonu;
|
||||
SELECT invalid_type_reraised('rick');
|
||||
ERROR: plpy.Error: type "test" does not exist
|
||||
CONTEXT: PL/Python function "invalid_type_reraised"
|
||||
CONTEXT: Traceback (most recent call last):
|
||||
PL/Python function "invalid_type_reraised", line 6, in <module>
|
||||
plpy.error(str(ex))
|
||||
PL/Python function "invalid_type_reraised"
|
||||
/* no typo no messing about
|
||||
*/
|
||||
CREATE FUNCTION valid_type(a text) RETURNS text
|
||||
@ -140,6 +155,164 @@ SELECT valid_type('rick');
|
||||
|
||||
(1 row)
|
||||
|
||||
/* error in nested functions to get a traceback
|
||||
*/
|
||||
CREATE FUNCTION nested_error() RETURNS text
|
||||
AS
|
||||
'def fun1():
|
||||
plpy.error("boom")
|
||||
|
||||
def fun2():
|
||||
fun1()
|
||||
|
||||
def fun3():
|
||||
fun2()
|
||||
|
||||
fun3()
|
||||
return "not reached"
|
||||
'
|
||||
LANGUAGE plpythonu;
|
||||
SELECT nested_error();
|
||||
ERROR: plpy.Error: boom
|
||||
CONTEXT: Traceback (most recent call last):
|
||||
PL/Python function "nested_error", line 10, in <module>
|
||||
fun3()
|
||||
PL/Python function "nested_error", line 8, in fun3
|
||||
fun2()
|
||||
PL/Python function "nested_error", line 5, in fun2
|
||||
fun1()
|
||||
PL/Python function "nested_error", line 2, in fun1
|
||||
plpy.error("boom")
|
||||
PL/Python function "nested_error"
|
||||
/* raising plpy.Error is just like calling plpy.error
|
||||
*/
|
||||
CREATE FUNCTION nested_error_raise() RETURNS text
|
||||
AS
|
||||
'def fun1():
|
||||
raise plpy.Error("boom")
|
||||
|
||||
def fun2():
|
||||
fun1()
|
||||
|
||||
def fun3():
|
||||
fun2()
|
||||
|
||||
fun3()
|
||||
return "not reached"
|
||||
'
|
||||
LANGUAGE plpythonu;
|
||||
SELECT nested_error_raise();
|
||||
ERROR: plpy.Error: boom
|
||||
CONTEXT: Traceback (most recent call last):
|
||||
PL/Python function "nested_error_raise", line 10, in <module>
|
||||
fun3()
|
||||
PL/Python function "nested_error_raise", line 8, in fun3
|
||||
fun2()
|
||||
PL/Python function "nested_error_raise", line 5, in fun2
|
||||
fun1()
|
||||
PL/Python function "nested_error_raise", line 2, in fun1
|
||||
raise plpy.Error("boom")
|
||||
PL/Python function "nested_error_raise"
|
||||
/* using plpy.warning should not produce a traceback
|
||||
*/
|
||||
CREATE FUNCTION nested_warning() RETURNS text
|
||||
AS
|
||||
'def fun1():
|
||||
plpy.warning("boom")
|
||||
|
||||
def fun2():
|
||||
fun1()
|
||||
|
||||
def fun3():
|
||||
fun2()
|
||||
|
||||
fun3()
|
||||
return "you''ve been warned"
|
||||
'
|
||||
LANGUAGE plpythonu;
|
||||
SELECT nested_warning();
|
||||
WARNING: boom
|
||||
CONTEXT: PL/Python function "nested_warning"
|
||||
nested_warning
|
||||
--------------------
|
||||
you've been warned
|
||||
(1 row)
|
||||
|
||||
/* AttributeError at toplevel used to give segfaults with the traceback
|
||||
*/
|
||||
CREATE FUNCTION toplevel_attribute_error() RETURNS void AS
|
||||
$$
|
||||
plpy.nonexistent
|
||||
$$ LANGUAGE plpythonu;
|
||||
SELECT toplevel_attribute_error();
|
||||
ERROR: AttributeError: 'module' object has no attribute 'nonexistent'
|
||||
CONTEXT: Traceback (most recent call last):
|
||||
PL/Python function "toplevel_attribute_error", line 2, in <module>
|
||||
plpy.nonexistent
|
||||
PL/Python function "toplevel_attribute_error"
|
||||
/* Calling PL/Python functions from SQL and vice versa should not lose context.
|
||||
*/
|
||||
CREATE OR REPLACE FUNCTION python_traceback() RETURNS void AS $$
|
||||
def first():
|
||||
second()
|
||||
|
||||
def second():
|
||||
third()
|
||||
|
||||
def third():
|
||||
plpy.execute("select sql_error()")
|
||||
|
||||
first()
|
||||
$$ LANGUAGE plpythonu;
|
||||
CREATE OR REPLACE FUNCTION sql_error() RETURNS void AS $$
|
||||
begin
|
||||
select 1/0;
|
||||
end
|
||||
$$ LANGUAGE plpgsql;
|
||||
CREATE OR REPLACE FUNCTION python_from_sql_error() RETURNS void AS $$
|
||||
begin
|
||||
select python_traceback();
|
||||
end
|
||||
$$ LANGUAGE plpgsql;
|
||||
CREATE OR REPLACE FUNCTION sql_from_python_error() RETURNS void AS $$
|
||||
plpy.execute("select sql_error()")
|
||||
$$ LANGUAGE plpythonu;
|
||||
SELECT python_traceback();
|
||||
ERROR: spiexceptions.DivisionByZero: division by zero
|
||||
CONTEXT: Traceback (most recent call last):
|
||||
PL/Python function "python_traceback", line 11, in <module>
|
||||
first()
|
||||
PL/Python function "python_traceback", line 3, in first
|
||||
second()
|
||||
PL/Python function "python_traceback", line 6, in second
|
||||
third()
|
||||
PL/Python function "python_traceback", line 9, in third
|
||||
plpy.execute("select sql_error()")
|
||||
PL/Python function "python_traceback"
|
||||
SELECT sql_error();
|
||||
ERROR: division by zero
|
||||
CONTEXT: SQL statement "select 1/0"
|
||||
PL/pgSQL function "sql_error" line 3 at SQL statement
|
||||
SELECT python_from_sql_error();
|
||||
ERROR: spiexceptions.DivisionByZero: division by zero
|
||||
CONTEXT: Traceback (most recent call last):
|
||||
PL/Python function "python_traceback", line 11, in <module>
|
||||
first()
|
||||
PL/Python function "python_traceback", line 3, in first
|
||||
second()
|
||||
PL/Python function "python_traceback", line 6, in second
|
||||
third()
|
||||
PL/Python function "python_traceback", line 9, in third
|
||||
plpy.execute("select sql_error()")
|
||||
PL/Python function "python_traceback"
|
||||
SQL statement "select python_traceback()"
|
||||
PL/pgSQL function "python_from_sql_error" line 3 at SQL statement
|
||||
SELECT sql_from_python_error();
|
||||
ERROR: spiexceptions.DivisionByZero: division by zero
|
||||
CONTEXT: Traceback (most recent call last):
|
||||
PL/Python function "sql_from_python_error", line 2, in <module>
|
||||
plpy.execute("select sql_error()")
|
||||
PL/Python function "sql_from_python_error"
|
||||
/* check catching specific types of exceptions
|
||||
*/
|
||||
CREATE TABLE specific (
|
||||
@ -187,7 +360,10 @@ plpy.execute("rollback to save")
|
||||
$$ LANGUAGE plpythonu;
|
||||
SELECT manual_subxact();
|
||||
ERROR: plpy.SPIError: SPI_execute failed: SPI_ERROR_TRANSACTION
|
||||
CONTEXT: PL/Python function "manual_subxact"
|
||||
CONTEXT: Traceback (most recent call last):
|
||||
PL/Python function "manual_subxact", line 2, in <module>
|
||||
plpy.execute("savepoint save")
|
||||
PL/Python function "manual_subxact"
|
||||
/* same for prepared plans
|
||||
*/
|
||||
CREATE FUNCTION manual_subxact_prepared() RETURNS void AS $$
|
||||
@ -199,4 +375,7 @@ plpy.execute(rollback)
|
||||
$$ LANGUAGE plpythonu;
|
||||
SELECT manual_subxact_prepared();
|
||||
ERROR: plpy.SPIError: SPI_execute_plan failed: SPI_ERROR_TRANSACTION
|
||||
CONTEXT: PL/Python function "manual_subxact_prepared"
|
||||
CONTEXT: Traceback (most recent call last):
|
||||
PL/Python function "manual_subxact_prepared", line 4, in <module>
|
||||
plpy.execute(save)
|
||||
PL/Python function "manual_subxact_prepared"
|
||||
|
||||
Reference in New Issue
Block a user