-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.sql
More file actions
61 lines (53 loc) · 1.58 KB
/
Copy patherrors.sql
File metadata and controls
61 lines (53 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
--
-- Error handling: how Ruby failures surface as PostgreSQL errors, and how
-- user code can catch and recover from them.
--
-- An uncaught RuntimeError becomes a PostgreSQL ERROR.
CREATE FUNCTION e_raise() RETURNS int LANGUAGE plruby AS $$
raise 'boom'
$$;
SELECT e_raise();
-- A specific exception class's message is preserved.
CREATE FUNCTION e_argerror() RETURNS int LANGUAGE plruby AS $$
Integer('not a number')
$$;
SELECT e_argerror();
-- A ZeroDivisionError surfaces as an error.
CREATE FUNCTION e_divzero() RETURNS int LANGUAGE plruby AS $$
1 / 0
$$;
SELECT e_divzero();
-- Exceptions can be rescued inside the function and turned into a value.
CREATE FUNCTION e_rescued() RETURNS text LANGUAGE plruby AS $$
begin
raise 'inner failure'
rescue => ex
"caught: #{ex.message}"
end
$$;
SELECT e_rescued();
-- A database error from spi_exec is catchable as a Ruby exception.
CREATE FUNCTION e_spi_caught() RETURNS text LANGUAGE plruby AS $$
begin
spi_exec('select * from a_table_that_does_not_exist')
'no error'
rescue => ex
'caught spi error'
end
$$;
SELECT e_spi_caught();
-- Returning the wrong shape is a datatype error.
CREATE FUNCTION e_wrong_array() RETURNS int[] LANGUAGE plruby AS $$
42
$$;
SELECT e_wrong_array();
-- elog('ERROR', ...) aborts like a PostgreSQL error.
CREATE FUNCTION e_elog() RETURNS void LANGUAGE plruby AS $$
elog('ERROR', 'explicit elog error')
$$;
SELECT e_elog();
-- After an error, the session remains usable.
CREATE FUNCTION e_ok() RETURNS int LANGUAGE plruby AS $$
123
$$;
SELECT e_ok();