2

Is it possible to pass a custom error code with plpy.error/fatal or inherit SPIError to pass a custom ERRCODE, HINT, etc?

hooblei
  • 3,210
  • 2
  • 20
  • 17

2 Answers2

2

While poking through the plpython source, a way I found so far, is to inherit from SPIError and set sqlstate

raise type('MyError', (plpy.SPIError,), {'sqlstate': 'D000M'})(...)

will propagate the ERRCODE D000M and works for me.

A nice to have would be to define a custom HINT too but looks more tricky, since it comes with spidata.

hooblei
  • 3,210
  • 2
  • 20
  • 17
  • It is strange to see this workaround, although a fix of this missing feature can be four hour work :(. PostgreSQL is open source, if somebody miss some feature, the he should to send a proposal for this feature. Python has all features for implementation user friendly plpy.error function with support DETAIL, HINT, SQLCODE. – Pavel Stehule Sep 28 '15 at 19:51
1

I found a solution how to do it. Use a instance of SPIError class, and set a spidata property.

postgres=# do $$
x = plpy.SPIError('Nazdarek'); 
x.spidata = (0, "Some detail", "some hint", None, -1); 
raise x;
$$ language plpythonu;

ERROR:  plpy.SPIError: Nazdarek
DETAIL:  Some detail
HINT:  some hint
CONTEXT:  Traceback (most recent call last):
  PL/Python anonymous code block, line 4, in <module>
    raise x;
PL/Python anonymous code block

Errcode should be entered as integer value, what is pretty unfriendly. I'll look how to translate it better.

Pavel Stehule
  • 42,331
  • 5
  • 91
  • 94