Exception handling the correct way

Michal Seidl michal.seidl at gmail.com
Mon Mar 11 00:39:14 PDT 2024


Hello,
thanks for the tips. It looks I have read correctly the documentation 
and there are no any special error codes for Postgis/GEOS error just 
XX000 for all.

Regards, Michal

On 3/11/24 02:51, Imre Samu wrote:
>     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 <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_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_Difference.sql>
> https://github.com/gojuno/lostgis/blob/master/sql/functions/ST_Safe_Repair.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 <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 
> <https://www.psycopg.org/docs/errors.html>  ?
> / except psycopg2.InternalError as exc:
> /
> /        if exc.pgcode == "XX000":/
> 
> Regards,
>   Imre
> 


More information about the postgis-users mailing list