Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
42.8. Error Handling in PL/Tcl
Prev UpChapter 42. PL/Tcl — Tcl Procedural LanguageHome Next

42.8. Error Handling in PL/Tcl#

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 Tclerror command or PL/Tcl'selog command. Such errors can be caught within Tcl using the Tclcatch 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.

Conversely, SQL errors that occur within PL/Tcl'sspi_exec,spi_prepare, andspi_execp commands are reported as Tcl errors, so they are catchable by Tcl'scatch 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.

Tcl provides anerrorCode 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 isPOSTGRES, the second word is the PostgreSQL version number, and additional words are field name/value pairs providing detailed information about the error. FieldsSQLSTATE,condition, andmessage are always supplied (the first two represent the error code and condition name as shown inAppendix A). Fields that may be present includedetail,hint,context,schema,table,column,datatype,constraint,statement,cursor_position,filename,lineno, andfuncname.

A convenient way to work with PL/Tcl'serrorCode information is to load it into an array, so that the field names become array subscripts. Code for doing that might look like

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        }    }}

(The double colons explicitly specify thaterrorCode is a global variable.)


Prev Up Next
42.7. Event Trigger Functions in PL/Tcl Home 42.9. Explicit Subtransactions in PL/Tcl
pdfepub
Go to PostgreSQL 17
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp