バージョンごとのドキュメント一覧

44.8. PL/Tclのエラー処理 #

<title>Error Handling in PL/Tcl</title>

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 <function>error</function> command or PL/Tcl's <function>elog</function> command. Such errors can be caught within Tcl using the Tcl <function>catch</function> 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. PL/Tcl関数中の、あるいはPL/Tcl関数から呼ばれるTclコードは、無効な演算の実行により、あるいはTclのerrorコマンドやPL/Tclのelogコマンドを使ってエラーを生成することにより、エラーとなることがありえます。 これらエラーはTclのcatchコマンドを使ってTcl内で捕捉することができます。 あるエラーが捕捉されず、PL/Tcl関数実行のトップレベルに伝播することが許容されているなら、関数が呼び出している問合せにおけるSQエラーとして報告されます。

Conversely, SQL errors that occur within PL/Tcl's <function>spi_exec</function>, <function>spi_prepare</function>, and <function>spi_execp</function> commands are reported as Tcl errors, so they are catchable by Tcl's <function>catch</function> 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. 逆に、PL/Tclのspi_execspi_preparespi_execpコマンドの中で起きるSQLエラーは、Tclのエラーとして報告され、したがって、これらはTclのcatchコマンドにより捕捉可能です。 (各々のPL/Tclコマンドは、エラー時にロールバックするSQL操作をサブトランザクション中で実行するので、部分的に完了した操作は自動的に後始末されます。) ここでも同様に、捕捉されることなくトップレベルに伝播するならSQLエラーに戻ります。

Tcl provides an <varname>errorCode</varname> 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 <literal>POSTGRES</literal>, the second word is the PostgreSQL version number, and additional words are field name/value pairs providing detailed information about the error. Fields <varname>SQLSTATE</varname>, <varname>condition</varname>, and <varname>message</varname> are always supplied (the first two represent the error code and condition name as shown in <xref linkend="errcodes-appendix"/>). Fields that may be present include <varname>detail</varname>, <varname>hint</varname>, <varname>context</varname>, <varname>schema</varname>, <varname>table</varname>, <varname>column</varname>, <varname>datatype</varname>, <varname>constraint</varname>, <varname>statement</varname>, <varname>cursor_position</varname>, <varname>filename</varname>, <varname>lineno</varname>, and <varname>funcname</varname>. Tclは、Tclプログラムで解釈しやすい形式でエラーに関する追加情報を表現できるerrorCode変数を提供します。 変数の中身はTclリスト形式で、1番目の語でエラーを報告したサブシステムまたはライブラリを識別します。それ以降の内容は個々のサブシステムやライブラリに任されています。 PL/Tclコマンドで報告されるデータベースエラーむけには、1番目の語がPOSTGRES、2番目の語がPostgreSQLのバージョン番号で、それ続く語はエラーの詳細情報を提供するフィールド名と値の組です。 フィールドSQLSTATEcondition、およびmessageは常に与えられます(最初の2つは付録Aにあるエラーコードと状態名です)。 出現しうるフィールドとしては、detailhintcontextschematablecolumndatatypeconstraintstatementcursor_positionfilenamelinenoおよびfuncnameがあります。

A convenient way to work with PL/Tcl's <varname>errorCode</varname> information is to load it into an array, so that the field names become array subscripts. Code for doing that might look like PL/TclのerrorCode情報を処理する便利な方法は、それを配列に読み込むことです。これによりフィールド名は配列の添え字になります。 これを行うコードは以下のようになります。

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 that <varname>errorCode</varname> is a global variable.) (二重コロンはerrorCodeがグローバル変数であることを明示的に指定します。)