Project

General

Profile

« Previous | Next » 

Revision 3555

sql_gen.py: plpythonu_error_handler: Translate specific Python exception types to PostgreSQL error codes (ValueError -> data_exception) instead of assuming everything is a data_exception. When removing the PL/Python prefix, preserve the Python exception class in a DETAIL message. Support non-PL/Python internal_errors by re-raising them.

View differences:

sql_gen.py
661 661

  
662 662
unique_violation_handler = ExcHandler('unique_violation')
663 663

  
664
# Note doubled "\"s because inside Python string
664 665
plpythonu_error_handler = ExcHandler('internal_error', '''\
665
RAISE data_exception USING MESSAGE =
666
    regexp_replace(SQLERRM, E'^PL/Python: \\w+: ', '');
666
DECLARE
667
    matches text[] := regexp_matches(SQLERRM, E'^PL/Python: (\\\\w+): (.*)$');
668
        -- .* matches everything, including \\n
669
    exc_name text := matches[1];
670
    msg text := matches[2];
671
BEGIN
672
    -- Re-raise non-PL/Python exceptions
673
    IF exc_name IS NULL THEN '''+reraise_exc+'''\
674
    -- Translate specific Python exception types to PostgreSQL error codes
675
    ELSIF exc_name = 'ValueError' THEN RAISE data_exception USING MESSAGE = msg;
676
    /* Re-raise other PL/Python exceptions with the PL/Python prefix removed.
677
    This allows the exception to be parsed like a native exception. */
678
    ELSE RAISE E'%\\nDETAIL:  Python exception class: %', msg, exc_name; 
679
    END IF;
680
END;
667 681
''')
668 682

  
669 683
def data_exception_handler(handler):

Also available in: Unified diff