summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/html/pltcl-error-handling.html
blob: eae7b10b548172111f534999965b86756b75958c (plain)
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
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>44.8. Error Handling in PL/Tcl</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="pltcl-event-trigger.html" title="44.7. Event Trigger Functions in PL/Tcl" /><link rel="next" href="pltcl-subtransactions.html" title="44.9. Explicit Subtransactions in PL/Tcl" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">44.8. Error Handling in PL/Tcl</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="pltcl-event-trigger.html" title="44.7. Event Trigger Functions in PL/Tcl">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="pltcl.html" title="Chapter 44. PL/Tcl — Tcl Procedural Language">Up</a></td><th width="60%" align="center">Chapter 44. PL/Tcl — Tcl Procedural Language</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.2 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="pltcl-subtransactions.html" title="44.9. Explicit Subtransactions in PL/Tcl">Next</a></td></tr></table><hr /></div><div class="sect1" id="PLTCL-ERROR-HANDLING"><div class="titlepage"><div><div><h2 class="title" style="clear: both">44.8. Error Handling in PL/Tcl <a href="#PLTCL-ERROR-HANDLING" class="id_link">#</a></h2></div></div></div><a id="id-1.8.9.12.2" class="indexterm"></a><p>
     Tcl code within or called from a PL/Tcl function can raise an error,
     either by executing some invalid operation or by generating an error
     using the Tcl <code class="function">error</code> command or
     PL/Tcl's <code class="function">elog</code> command.  Such errors can be caught
     within Tcl using the Tcl <code class="function">catch</code> command.  If an
     error is not caught but is allowed to propagate out to the top level of
     execution of the PL/Tcl function, it is reported as an SQL error in the
     function's calling query.
    </p><p>
     Conversely, SQL errors that occur within PL/Tcl's
     <code class="function">spi_exec</code>, <code class="function">spi_prepare</code>,
     and <code class="function">spi_execp</code> commands are reported as Tcl errors,
     so they are catchable by Tcl's <code class="function">catch</code> command.
     (Each of these PL/Tcl commands runs its SQL operation in a
     subtransaction, which is rolled back on error, so that any
     partially-completed operation is automatically cleaned up.)
     Again, if an error propagates out to the top level without being caught,
     it turns back into an SQL error.
    </p><p>
     Tcl provides an <code class="varname">errorCode</code> variable that can represent
     additional information about an error in a form that is easy for Tcl
     programs to interpret.  The contents are in Tcl list format, and the
     first word identifies the subsystem or library reporting the error;
     beyond that the contents are left to the individual subsystem or
     library.  For database errors reported by PL/Tcl commands, the first
     word is <code class="literal">POSTGRES</code>, the second word is the PostgreSQL
     version number, and additional words are field name/value pairs
     providing detailed information about the error.
     Fields <code class="varname">SQLSTATE</code>, <code class="varname">condition</code>,
     and <code class="varname">message</code> are always supplied
     (the first two represent the error code and condition name as shown
     in <a class="xref" href="errcodes-appendix.html" title="Appendix A. PostgreSQL Error Codes">Appendix A</a>).
     Fields that may be present include
     <code class="varname">detail</code>, <code class="varname">hint</code>, <code class="varname">context</code>,
     <code class="varname">schema</code>, <code class="varname">table</code>, <code class="varname">column</code>,
     <code class="varname">datatype</code>, <code class="varname">constraint</code>,
     <code class="varname">statement</code>, <code class="varname">cursor_position</code>,
     <code class="varname">filename</code>, <code class="varname">lineno</code>, and
     <code class="varname">funcname</code>.
    </p><p>
     A convenient way to work with PL/Tcl's <code class="varname">errorCode</code>
     information is to load it into an array, so that the field names become
     array subscripts.  Code for doing that might look like
</p><pre class="programlisting">
if {[catch { spi_exec $sql_command }]} {
    if {[lindex $::errorCode 0] == "POSTGRES"} {
        array set errorArray $::errorCode
        if {$errorArray(condition) == "undefined_table"} {
            # deal with missing table
        } else {
            # deal with some other type of SQL error
        }
    }
}
</pre><p>
     (The double colons explicitly specify that <code class="varname">errorCode</code>
     is a global variable.)
    </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="pltcl-event-trigger.html" title="44.7. Event Trigger Functions in PL/Tcl">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="pltcl.html" title="Chapter 44. PL/Tcl — Tcl Procedural Language">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="pltcl-subtransactions.html" title="44.9. Explicit Subtransactions in PL/Tcl">Next</a></td></tr><tr><td width="40%" align="left" valign="top">44.7. Event Trigger Functions in PL/Tcl </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.2 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 44.9. Explicit Subtransactions in PL/Tcl</td></tr></table></div></body></html>