Exception handling the correct way

Imre Samu pella.samu at gmail.com
Sun Mar 10 18:51:22 PDT 2024


>
> is there any standard way how to catch Exceptions from Postgis functions?
> For Example:
> SELECT ST_Split(polygon_geom, polygon_geom)
> result in general PostgreSQL InternalError exception with CODE XX000.
>

My preferred way is to write a custom wrapper function in PL/pgSQL for
error handling with PostGIS functions.

Random examples:

https://github.com/kiselev-dv/osm-addresses-pgsql/blob/master/St_CancaveHull_Safe.sql
~ " ...   *EXCEPTION WHEN SQLSTATE 'XX000' THEN return null; * "

or
https://github.com/gojuno/lostgis/blob/master/sql/functions/ST_Safe_Intersection.sql
https://github.com/gojuno/lostgis/blob/master/sql/functions/ST_Safe_Difference.sql
https://github.com/gojuno/lostgis/blob/master/sql/functions/ST_Safe_Repair.sql

or
https://github.com/AfESG/AED-Platform/blob/master/script/etl/safe_isect.sql


>  I mean how to catch from programing language code. PHP, Python, ...

Maybe with the https://www.psycopg.org/docs/errors.html  ?

* except psycopg2.InternalError as exc:*
*        if exc.pgcode == "XX000":*

Regards,
 Imre
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20240311/e8e18fb2/attachment.htm>


More information about the postgis-users mailing list