Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit def1777

Browse files
authored
null support
1 parent edf8e9f commit def1777

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

‎functions/raise_exception.sql‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
11
--Inspired by https://hakibenita.com/future-proof-sql
22

33
--TODO: добавить ещё параметры, см. https://postgrespro.ru/docs/postgresql/14/plpgsql-errors-and-messages
4-
54
create or replace function raise_exception(
65
value anyelement,
76
message text default 'Unhandled value',
8-
detail text default '',
7+
detail text default null,
98
hint text default 'See value in detail as JSON',
109
errcode text default 'raise_exception'
1110
)
1211
returns boolean
1312
immutable
14-
returns null onnull input
13+
--strict -- returns null if any parameter is null
1514
parallel safe
1615
language plpgsql
1716
set search_path = ''
1817
as
1918
$$
2019
begin
2120
raise exception using
22-
message = message,
23-
detail = case when detail = ''
24-
then to_json(value)::text
25-
else detail
26-
end,
27-
hint = hint,
28-
errcode = errcode;
21+
message = coalesce(message, 'Unhandled value'),
22+
detail = coalesce(detail, coalesce(to_json(value), 'null'::json)::text),
23+
hint = coalesce(hint, 'See value in detail as JSON'),
24+
errcode = coalesce(errcode, 'raise_exception');
2925
end;
3026
$$;
3127

3228
--TEST
33-
29+
--select raise_exception(null::int);
3430
--select raise_exception(1234567890);
3531
--select raise_exception('ABCDE'::text);
3632
--select raise_exception(json_build_object('id', 123));
3733
--select raise_exception('1d2h3m4s'::interval);
34+
--select raise_exception(now());
3835

3936
/*
4037
--explain

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /