Tcl_AddErrorInfo

Tcl_AddErrorInfo

2017-08-21 HaO: If a C program wants to add any information to the stack trace, one should start with a newline to separate from already present information.

Example to get a Windows CLR exception reported:

static int SetCLRErrorResult(Tcl_Interp * interp, System::Exception^ exception)
{
        if (interp != NULL) {
                Tcl_DString        Recoded1;
                Tcl_DStringInit(&Recoded1);
                MString2DString(&Recoded1,exception->Message);
                if (Tcl_DStringLength(&Recoded1) > 0) {
                        Tcl_DStringResult(interp, &Recoded1);
                } else {
                        Tcl_SetResult(interp, fg_error_clr, TCL_STATIC);
                }
                Tcl_DStringFree(&Recoded1);
                // ---------------------------------------------------------------------
                // >> Addidional Error trace
                Tcl_AddErrorInfo(interp, "\n");
                Tcl_AddErrorInfo(interp, fg_error_clr);
                Tcl_DStringInit(&Recoded1);
                MString2DString(&Recoded1, exception->StackTrace);
                if (Tcl_DStringLength(&Recoded1) > 0) {
                        Tcl_AddErrorInfo(interp, "\n");
                        Tcl_AddErrorInfo(interp, Tcl_DStringValue(&Recoded1));
                        Tcl_AddErrorInfo(interp, fg_stack_end);
                }
        }
        return TCL_ERROR;
}

// Set the text corresponding to the given windows error code as result.
static void MString2DString(Tcl_DString  *pdOut, String ^mIn)
{
        // Protect string from being moved
        pin_ptr<const wchar_t> wpIn = PtrToStringChars(mIn);
        Tcl_WinTCharToUtf(wpIn, -1, pdOut);
        // pinning is released on exit
}

try {
        ...
}
catch (System::Exception^ exception) {
        return SetCLRErrorResult(interp, exception);
}