\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#221: Allow Background Error Handlers to Accept Return Options}
\date{September 15, 2004}
\author{Don Porter}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#221}&\textbf{Allow Background Error Handlers to Accept Return Options}\\\hline
Author&

Don Porter \tipmail{dgp}{users.sf.net}
\\
Created&Wednesday, \(\text{15}^{\text{th}}\) September 2004\\
Type&Project\\
Tcl Version&\(8.5\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 2.10 $\\
Keywords&bgerror, return, options\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes a new system for registering a background error handler of an interp so that the full set of return options (\cite{tip90}) can be passed to it.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Background}

Whenever a script is evaluated as part of handling an event, and that script evaluation returns TCL\_ERROR or some other non-TCL\_OK return code it is not equipped to handle, then the code that evaluates that script is expected to call \textit{Tcl\_BackgroundError} to report the exceptional code to the application.

Then \textit{Tcl\_BackgroundError} will arrange for the command \textbf{bgerror} to be called during an idle event. It is intended that each application will define a \textbf{::bgerror} command to its liking so that background errors are reported (or ignored) as the application prefers.

The defined syntax for \textbf{bgerror} is 

\begin{quote}
\textbf{bgerror} \textit{message}
\end{quote}

The \textbf{bgerror} command receives as its only argument a copy of what the interp result was when \textit{Tcl\_BackgroundError} was called. This is presumably an error message.

When \textbf{bgerror} is called, it is also arranged that the variables \textbf{::errorInfo} and \textbf{::errorCode} are set to the same values they had when \textit{Tcl\_BackgroundError} was called. This is effectively another set of arguments passed through a side channel.

Note that the non-TCL\_OK return code that triggered the \textit{Tcl\_BackgroundError} call is not itself made known to \textbf{bgerror}. Nor is the \textit{-level}, \textit{-errorline}, or other return option information made possible by \cite{tip90} passed along to \textbf{bgerror} in any way.

\section{Proposal}

A new subcommand, \textbf{interp bgerror} will be created that allows for registration of a handler command to react to background errors. Its syntax will be:

\begin{quote}
\textbf{interp bgerror} \textit{path} ?\textit{cmdPrefix}?
\end{quote}

Here \textit{path} is the path of an interp, interpreted the same way as the \textit{path} argument to the existing \textbf{interp aliases} subcommand. This argument determines which interp's background error handler we are interested in. The value of an empty list for \textit{path} indicates the current interp.

As is the case with other \textbf{interp} subcommands, an alternative means to access the same functionality will be made available as the \textbf{bgerror} subcommand of the \textit{slave} command of an interp with syntax:

\begin{quote}
\textit{slave} \textbf{bgerror} ?\textit{cmdPrefix}?
\end{quote}

When no \textit{cmdPrefix} argument is present, the command prefix currently registered to handle background errors in the \textit{path} interp will be returned. The returned value will be the \textit{cmdPrefix} argument most recently successfully passed to \textbf{interp bgerror} \textit{path}, or the default background error handler command of the interp.

When a \textit{cmdPrefix} argument is present, it must be a valid Tcl list of length at least one. An invalid list or an empty list is an error. A \textit{cmdPrefix} list argument of length \textit{N} will become the first \textit{N} substituted words of the handler command invoked to handle calls to \textit{Tcl\_BackgroundError}. That is, if the \textit{cmdPrefix} argument is stored in a variable \textbf{cmdPrefix}, subsequent calls to \textit{Tcl\_BackgroundError} will lead to evaluation of a command like so:

\begin{quote}
\textbf{\{expand\}\$cmdPrefix} \textit{message} \textit{returnOptionsDictionary}
\end{quote}

The \textit{message} argument is the interp result at the time \textit{Tcl\_BackgroundError} is called. The \textit{returnOptionsDictionary} argument is a Tcl dict value (\cite{tip111}) holding the value of the interp's return options dictionary at the time \textit{Tcl\_BackgroundError} was called. Specifically, the \textit{returnOptionsDictionary} argument is the value returned by \textit{Tcl\_GetReturnOptions} (\cite{tip227}) at the time \textit{Tcl\_BackgroundError} is called.

Stored in the \textit{returnOptionsDictionary} argument will be values for the \textit{-level} and \textit{-code} keys, and when those values indicate a TCL\_ERROR triggered the \textit{Tcl\_BackgroundError} call, the \textit{-errorinfo}, \textit{-errorcode}, and \textit{-errorline} keys will have values as well. Any other return options present in the interp's return options dictionary at the time \textit{Tcl\_BackgroundError} is called will also be available in the \textit{returnOptionsDictionary} argument.

Note that after this change, applications will be able to register a background error handling command that has no need to consult the variables \textbf{::errorInfo} or \textbf{::errorCode} at all.

\section{Compatibility}

Existing applications making use of the \textbf{bgerror} interface provide a \textbf{bgerror} command that expects exactly one argument.

To continue to compatibly support these applications, the default background error handler command prefix registered in each interp will be a command that sets the values of \textbf{::errorInfo} and \textbf{::errorCode} to the values of the corresponding keys in the return options dictionary, if appropriate. Then it will invoke the command \textbf{bgerror} \textit{message} in the global namespace. For complete compatibility, the existing fallbacks will also be honored in the default handler, including the invoking a hidden command \textbf{bgerror} \textit{message} in safe interps, and the ultimate fallback (in trusted interps only) being a message written to the stderr channel of the process as determined by \textit{Tcl\_GetStdChannel(TCL\_STDERR)}.

\section{Rejected Alternatives}

The first draft of this proposal proposed several attempts to call \textbf{bgerror}, first with two arguments, then with one. It was rejected because an error due to calling \textbf{bgerror} with the wrong number of arguments could not be distinguished (easily and reliably) from an error for other reasons. This fallback strategy was prone to the masking of errors.

The new proposal is also preferred over the first draft as it empowers Tcl programmers to leave behind \textbf{bgerror} as a \textit{magic} command name with special significance to Tcl. Callback registration is a cleaner mechanism then giving particular command names privileged status, and we should move in that direction when the opportunity arises.

An alternative syntax for \textbf{interp bgerror},

\begin{quote}
\textbf{interp bgerror} \textit{path target cmdPrefix}
\end{quote}

was considered. This alternative would have allowed background errors in the \textit{path} interp to be handled in the \textit{target} interp. The difficulty with this alternative was in how to define the introspection form of the command. Introspection would need to return \textit{target} information, and it would be possible that the target interp of the handler would be an interp for which no \textit{target} path could be constructed to be returned (a sibling, parent, or uncle interp, etc.).

The proposed form can be combined with \textbf{interp alias} to still allow background errors in one interp to (ultimately) be handled by a command evaluation in a different interp.

\section{Reference Implementation}

See Tcl Patch 1060579.

\section{Comments}

Please make any comments here.

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\begin{thebibliography}{TIP \#3}
\addcontentsline{toc}{section}{References}
\bibitem[TIP \#90]{tip90}
Don Porter,
Donal K. Fellows,
\emph{Enable [return -code] in Control Structure Procs},
on-line at \url{http://purl.org/tcl/tip/90.html}
\bibitem[TIP \#111]{tip111}
Donal K. Fellows,
David S. Cargo,
\emph{Dictionary Values and Manipulators},
on-line at \url{http://purl.org/tcl/tip/111.html}
\bibitem[TIP \#227]{tip227}
Don Porter,
\emph{Interface to Get and Set the Return Options of an Interpreter},
on-line at \url{http://purl.org/tcl/tip/227.html}
\end{thebibliography}
\end{document}
