Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
pltcl-error-handling.html60 linesDownload Raw Back to html
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>2<!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.3 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>3     Tcl code within or called from a PL/Tcl function can raise an error,4     either by executing some invalid operation or by generating an error5     using the Tcl <code class="function">error</code> command or6     PL/Tcl's <code class="function">elog</code> command.  Such errors can be caught7     within Tcl using the Tcl <code class="function">catch</code> command.  If an8     error is not caught but is allowed to propagate out to the top level of9     execution of the PL/Tcl function, it is reported as an SQL error in the10     function's calling query.11    </p><p>12     Conversely, SQL errors that occur within PL/Tcl's13     <code class="function">spi_exec</code>, <code class="function">spi_prepare</code>,14     and <code class="function">spi_execp</code> commands are reported as Tcl errors,15     so they are catchable by Tcl's <code class="function">catch</code> command.16     (Each of these PL/Tcl commands runs its SQL operation in a17     subtransaction, which is rolled back on error, so that any18     partially-completed operation is automatically cleaned up.)19     Again, if an error propagates out to the top level without being caught,20     it turns back into an SQL error.21    </p><p>22     Tcl provides an <code class="varname">errorCode</code> variable that can represent23     additional information about an error in a form that is easy for Tcl24     programs to interpret.  The contents are in Tcl list format, and the25     first word identifies the subsystem or library reporting the error;26     beyond that the contents are left to the individual subsystem or27     library.  For database errors reported by PL/Tcl commands, the first28     word is <code class="literal">POSTGRES</code>, the second word is the PostgreSQL29     version number, and additional words are field name/value pairs30     providing detailed information about the error.31     Fields <code class="varname">SQLSTATE</code>, <code class="varname">condition</code>,32     and <code class="varname">message</code> are always supplied33     (the first two represent the error code and condition name as shown34     in <a class="xref" href="errcodes-appendix.html" title="Appendix A. PostgreSQL Error Codes">Appendix A</a>).35     Fields that may be present include36     <code class="varname">detail</code>, <code class="varname">hint</code>, <code class="varname">context</code>,37     <code class="varname">schema</code>, <code class="varname">table</code>, <code class="varname">column</code>,38     <code class="varname">datatype</code>, <code class="varname">constraint</code>,39     <code class="varname">statement</code>, <code class="varname">cursor_position</code>,40     <code class="varname">filename</code>, <code class="varname">lineno</code>, and41     <code class="varname">funcname</code>.42    </p><p>43     A convenient way to work with PL/Tcl's <code class="varname">errorCode</code>44     information is to load it into an array, so that the field names become45     array subscripts.  Code for doing that might look like46</p><pre class="programlisting">47if {[catch { spi_exec $sql_command }]} {48    if {[lindex $::errorCode 0] == "POSTGRES"} {49        array set errorArray $::errorCode50        if {$errorArray(condition) == "undefined_table"} {51            # deal with missing table52        } else {53            # deal with some other type of SQL error54        }55    }56}57</pre><p>58     (The double colons explicitly specify that <code class="varname">errorCode</code>59     is a global variable.)60    </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.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 44.9. Explicit Subtransactions in PL/Tcl</td></tr></table></div></body></html>
codekingpro/portable-devtools · Team Ai