|
| 1 | +-- test error handling, i forgot to restore Warn_restart in |
| 2 | +-- the trigger handler once. the errors and subsequent core dump were |
| 3 | +-- interesting. |
| 4 | +/* Flat out Python syntax error |
| 5 | + */ |
| 6 | +CREATE FUNCTION python_syntax_error() RETURNS text |
| 7 | + AS |
| 8 | +'.syntaxerror' |
| 9 | + LANGUAGE plpythonu; |
| 10 | +ERROR: could not compile PL/Python function "python_syntax_error" |
| 11 | +DETAIL: SyntaxError: invalid syntax (line 2) |
| 12 | +/* With check_function_bodies = false the function should get defined |
| 13 | + * and the error reported when called |
| 14 | + */ |
| 15 | +SET check_function_bodies = false; |
| 16 | +CREATE FUNCTION python_syntax_error() RETURNS text |
| 17 | + AS |
| 18 | +'.syntaxerror' |
| 19 | + LANGUAGE plpythonu; |
| 20 | +SELECT python_syntax_error(); |
| 21 | +ERROR: could not compile PL/Python function "python_syntax_error" |
| 22 | +DETAIL: SyntaxError: invalid syntax (line 2) |
| 23 | +/* Run the function twice to check if the hashtable entry gets cleaned up */ |
| 24 | +SELECT python_syntax_error(); |
| 25 | +ERROR: could not compile PL/Python function "python_syntax_error" |
| 26 | +DETAIL: SyntaxError: invalid syntax (line 2) |
| 27 | +RESET check_function_bodies; |
| 28 | +/* Flat out syntax error |
| 29 | + */ |
| 30 | +CREATE FUNCTION sql_syntax_error() RETURNS text |
| 31 | + AS |
| 32 | +'plpy.execute("syntax error")' |
| 33 | + LANGUAGE plpythonu; |
| 34 | +SELECT sql_syntax_error(); |
| 35 | +WARNING: plpy.SPIError: unrecognized error in PLy_spi_execute_query |
| 36 | +CONTEXT: PL/Python function "sql_syntax_error" |
| 37 | +ERROR: plpy.SPIError: syntax error at or near "syntax" |
| 38 | +LINE 1: syntax error |
| 39 | + ^ |
| 40 | +QUERY: syntax error |
| 41 | +CONTEXT: PL/Python function "sql_syntax_error" |
| 42 | +/* check the handling of uncaught python exceptions |
| 43 | + */ |
| 44 | +CREATE FUNCTION exception_index_invalid(text) RETURNS text |
| 45 | + AS |
| 46 | +'return args[1]' |
| 47 | + LANGUAGE plpythonu; |
| 48 | +SELECT exception_index_invalid('test'); |
| 49 | +ERROR: IndexError: list index out of range |
| 50 | +CONTEXT: PL/Python function "exception_index_invalid" |
| 51 | +/* check handling of nested exceptions |
| 52 | + */ |
| 53 | +CREATE FUNCTION exception_index_invalid_nested() RETURNS text |
| 54 | + AS |
| 55 | +'rv = plpy.execute("SELECT test5(''foo'')") |
| 56 | +return rv[0]' |
| 57 | + LANGUAGE plpythonu; |
| 58 | +SELECT exception_index_invalid_nested(); |
| 59 | +WARNING: plpy.SPIError: unrecognized error in PLy_spi_execute_query |
| 60 | +CONTEXT: PL/Python function "exception_index_invalid_nested" |
| 61 | +ERROR: plpy.SPIError: function test5(unknown) does not exist |
| 62 | +LINE 1: SELECT test5('foo') |
| 63 | + ^ |
| 64 | +HINT: No function matches the given name and argument types. You might need to add explicit type casts. |
| 65 | +QUERY: SELECT test5('foo') |
| 66 | +CONTEXT: PL/Python function "exception_index_invalid_nested" |
| 67 | +/* a typo |
| 68 | + */ |
| 69 | +CREATE FUNCTION invalid_type_uncaught(a text) RETURNS text |
| 70 | + AS |
| 71 | +'if "plan" not in SD: |
| 72 | + q = "SELECT fname FROM users WHERE lname = $1" |
| 73 | + SD["plan"] = plpy.prepare(q, [ "test" ]) |
| 74 | +rv = plpy.execute(SD["plan"], [ a ]) |
| 75 | +if len(rv): |
| 76 | + return rv[0]["fname"] |
| 77 | +return None |
| 78 | +' |
| 79 | + LANGUAGE plpythonu; |
| 80 | +SELECT invalid_type_uncaught('rick'); |
| 81 | +WARNING: plpy.SPIError: unrecognized error in PLy_spi_prepare |
| 82 | +CONTEXT: PL/Python function "invalid_type_uncaught" |
| 83 | +ERROR: plpy.SPIError: type "test" does not exist |
| 84 | +CONTEXT: PL/Python function "invalid_type_uncaught" |
| 85 | +/* for what it's worth catch the exception generated by |
| 86 | + * the typo, and return None |
| 87 | + */ |
| 88 | +CREATE FUNCTION invalid_type_caught(a text) RETURNS text |
| 89 | + AS |
| 90 | +'if "plan" not in SD: |
| 91 | + q = "SELECT fname FROM users WHERE lname = $1" |
| 92 | + try: |
| 93 | + SD["plan"] = plpy.prepare(q, [ "test" ]) |
| 94 | + except plpy.SPIError, ex: |
| 95 | + plpy.notice(str(ex)) |
| 96 | + return None |
| 97 | +rv = plpy.execute(SD["plan"], [ a ]) |
| 98 | +if len(rv): |
| 99 | + return rv[0]["fname"] |
| 100 | +return None |
| 101 | +' |
| 102 | + LANGUAGE plpythonu; |
| 103 | +SELECT invalid_type_caught('rick'); |
| 104 | +WARNING: plpy.SPIError: unrecognized error in PLy_spi_prepare |
| 105 | +CONTEXT: PL/Python function "invalid_type_caught" |
| 106 | +NOTICE: type "test" does not exist |
| 107 | +CONTEXT: PL/Python function "invalid_type_caught" |
| 108 | + invalid_type_caught |
| 109 | +--------------------- |
| 110 | + |
| 111 | +(1 row) |
| 112 | + |
| 113 | +/* for what it's worth catch the exception generated by |
| 114 | + * the typo, and reraise it as a plain error |
| 115 | + */ |
| 116 | +CREATE FUNCTION invalid_type_reraised(a text) RETURNS text |
| 117 | + AS |
| 118 | +'if "plan" not in SD: |
| 119 | + q = "SELECT fname FROM users WHERE lname = $1" |
| 120 | + try: |
| 121 | + SD["plan"] = plpy.prepare(q, [ "test" ]) |
| 122 | + except plpy.SPIError, ex: |
| 123 | + plpy.error(str(ex)) |
| 124 | +rv = plpy.execute(SD["plan"], [ a ]) |
| 125 | +if len(rv): |
| 126 | + return rv[0]["fname"] |
| 127 | +return None |
| 128 | +' |
| 129 | + LANGUAGE plpythonu; |
| 130 | +SELECT invalid_type_reraised('rick'); |
| 131 | +WARNING: plpy.SPIError: unrecognized error in PLy_spi_prepare |
| 132 | +CONTEXT: PL/Python function "invalid_type_reraised" |
| 133 | +ERROR: plpy.Error: type "test" does not exist |
| 134 | +CONTEXT: PL/Python function "invalid_type_reraised" |
| 135 | +/* no typo no messing about |
| 136 | + */ |
| 137 | +CREATE FUNCTION valid_type(a text) RETURNS text |
| 138 | + AS |
| 139 | +'if "plan" not in SD: |
| 140 | + SD["plan"] = plpy.prepare("SELECT fname FROM users WHERE lname = $1", [ "text" ]) |
| 141 | +rv = plpy.execute(SD["plan"], [ a ]) |
| 142 | +if len(rv): |
| 143 | + return rv[0]["fname"] |
| 144 | +return None |
| 145 | +' |
| 146 | + LANGUAGE plpythonu; |
| 147 | +SELECT valid_type('rick'); |
| 148 | + valid_type |
| 149 | +------------ |
| 150 | + |
| 151 | +(1 row) |
| 152 | + |
0 commit comments