postgresqlplpgsqlplpython

Is there a way to remove the “plpy.Error:” prefix from an exception raised with `plpy.error`?


In a PL/pgSQL function, RAISE EXCEPTION 'mymessage' USING ...; will have “mymessage” as the error message, but in a PL/Python function, plpy.error('mymessage', ...) will have “plpy.Error: mymessage” as the error message. Is there a straightforward way to remove that prefix from the error message?


Solution

  • I afraid so you cannot to change it anyway. It's by design. It has little bit different semantics. Inside plpgsql you can raise direct PostgreSQL exception. It is not possible in Python - the python exceptions plpy.Error is raised, catched and transformed to PostgreSQL exceptions - the "plpy.Error" is name of exception. There can be any name of any other exception.

    The convention of Python exceptions in this case is - level: name: text

    you can see

    ERROR:  ZeroDivisionError: division by zero
    ERROR:  plpy.Error: some text
    

    and there is not possibility how to change it. This is by design - this is convention.

    PLpgSQL has not this convention - but PLpgSQL is much more integrated to Postgres. It has not own environment, own exceptions.