22
33-- Documentation: https://postgrespro.ru/docs/postgresql/14/plpgsql-errors-and-messages
44
5- -- TODO: добавить ещё параметры, см. https://postgrespro.ru/docs/postgresql/14/plpgsql-errors-and-messages
6- 75create or replace function raise_exception (
86 value anyelement,
97 message text default ' Unhandled value' ,
10- detail text default null ,
11- hint text default ' See value in detail as JSON' ,
12- errcode text default ' raise_exception'
8+ detail text default null ,
9+ hint text default ' See value in detail as JSON' ,
10+ errcode text default ' raise_exception' ,
11+ " column" text default null ,
12+ " constraint" text default null ,
13+ " table" text default null ,
14+ " schema" text default null
1315)
1416 returns boolean
1517 immutable
2426 message = coalesce(message, ' Unhandled value' ),
2527 detail = coalesce(detail, coalesce(to_json(value), ' null' ::json)::text ),
2628 hint = coalesce(hint, ' See value in detail as JSON' ),
27- errcode = coalesce(errcode, ' raise_exception' );
29+ errcode = coalesce(errcode, ' raise_exception' ),
30+ column = coalesce(" column" , ' ' ),
31+ constraint = coalesce(" constraint" , ' ' ),
32+ table = coalesce(" table" , ' ' ),
33+ schema = coalesce(" schema" , ' ' ),
34+ datatype = pg_typeof(value);
2835end;
2936$$;
3037
3845 exception_context text ;
3946 exception_detail text ;
4047 exception_hint text ;
48+ exception_datatype text ;
4149 BEGIN
4250 LOOP
4351 BEGIN -- subtransaction SAVEPOINT
@@ -53,27 +61,31 @@ do $$
5361 elsif i = 5 then
5462 perform raise_exception(' 1d2h3m4s' ::interval, null , null , null , null );
5563 elsif i = 6 then
56- perform raise_exception(now());
64+ perform raise_exception(now(), null , null , null , null , null );
5765 elsif i = 7 then
58- perform raise_exception(true);
66+ perform raise_exception(true, null , null , null , null , null , null );
5967 elsif i = 8 then
60- perform raise_exception(- 123 .456 );
68+ perform raise_exception(- 123 .456 , null , null , null , null , null , null , null );
69+ elsif i = 9 then
70+ perform raise_exception(point (0 , 0 ), null , null , null , null , null , null , null , null );
6171 end if;
6272 EXIT WHEN true;
6373 EXCEPTION WHEN others THEN
64- GET STACKED DIAGNOSTICS
74+ GET STACKED DIAGNOSTICS-- https://postgrespro.ru/docs/postgresql/14/plpgsql-control-structures#PLPGSQL-ERROR-TRAPPING
6575 exception_sqlstate := RETURNED_SQLSTATE,
6676 exception_message := MESSAGE_TEXT,
6777 exception_context := PG_EXCEPTION_CONTEXT,
6878 exception_detail := PG_EXCEPTION_DETAIL,
69- exception_hint := PG_EXCEPTION_HINT;
79+ exception_hint := PG_EXCEPTION_HINT,
80+ exception_datatype := PG_DATATYPE_NAME;
7081
7182 RAISE NOTICE ' ====== % ======' , i;
7283 RAISE NOTICE ' * exception_sqlstate = %' , exception_sqlstate;
7384 RAISE NOTICE ' * exception_message = %' , exception_message;
7485 RAISE NOTICE ' * exception_context = %' , exception_context;
7586 RAISE NOTICE ' * exception_detail = %' , exception_detail;
7687 RAISE NOTICE ' * exception_hint = %' , exception_hint;
88+ RAISE NOTICE ' * exception_datatype = %' , exception_datatype;
7789 END;
7890 END LOOP;
7991 END;
0 commit comments