记录SQL Server报错时的调用堆栈

发布于 2024-08-18 14:44:47 字数 837 浏览 3 评论 0原文

这是问题的后续 嵌套存储过程包含 TRY CATCH ROLLBACK 模式?

在 catch 中块我使用存储过程通过读取 ERROR_MESSAGE()、ERROR_PROCEDURE()、ERROR_LINE() 等来报告(重新引发)错误。如上所述 此处 我还进行了一项检查,以便确定错误是否已被重新抛出(这种情况发生在嵌套存储过程中,如下所示)错误信息通过每个 TRY CATCH 块向下传递)。

我想要做的,无论是直接在“ReportError”中,还是间接地用我的模式(如第一个问题中所述)记录堆栈跟踪 - 因此,当 ReportError 检测到它正在接收自己抛出的错误时,它会附加堆栈的下一层到错误消息。这将帮助我避免看到来自某个小实用程序存储过程的错误消息,而无法知道它的名称。如果我尝试直接在 ReportError 中执行此操作,则会失败,因为重新抛出的错误将其自身报告为来自 ReportError - 只有原始错误可见。

ReportError 是否有某种方法可以在 SQL Server 中执行堆栈跟踪,而无需向每个存储过程传递参数,也无需使用 #temp 表手动维护此类跟踪?基本上我想要递归调用 ERROR_PROCEDURE() 和 ERROR_LINE()。

This is a follow up to the question
Nested stored procedures containing TRY CATCH ROLLBACK pattern?

In the catch block I use a stored procedure to report (reraise) the error by reading from ERROR_MESSAGE(), ERROR_PROCEDURE(), ERROR_LINE(), etc. As described here I also have a check so that it can determine if the error has already been rethrown (this happens with nested stored procedures as the error information is passed down through each TRY CATCH block).

What I would like to do, either directly in 'ReportError', or indirectly with my pattern (as described in the first question), is record a stack trace - so when ReportError detects that it is receving an error thrown by itself, it appends the next level of the stack to the error message. This would help me avoid cases where I see an error message coming from some little utility stored procedure, without any way of knowing what called it. If I try doing this directly in ReportError it fails, since the rethrown error reports itself as coming from ReportError - only the original error is visible.

Is there some way for ReportError to perform a stack trace in SQL Server, without passing an argument to every single stored procedure, and without manually maintaining such a trace with #temp table? Basically I want a recursive call of ERROR_PROCEDURE() and ERROR_LINE().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾挽 2024-08-25 14:44:47

好的,我将在 :-) 中添加错误处理:

ERROR_%() 函数对于 CATCH 块的范围是可见的。这意味着您可以在每个 CATCH 块中的存储过程或函数调用中使用它们。

对于嵌套存储过程,了解导致错误的原因以及记录错误的内容很有用。

...
END TRY
BEGIN CATCH
    IF XACT_STATE() <> 0 AND @starttrancount = 0 
        ROLLBACK TRANSACTION
    EXEC dbo.MyExceptionHandler @@PROCID, @errmsg OUTPUT;
    RAISERROR (@errmsg, 16, 1);
END CATCH

---with this handler (cut down version of ours)
CREATE PROCEDURE dbo.MyExceptionHandler
    @CallerProcID int,
    @ErrorMessage varchar(2000) OUTPUT
WITH EXECUTE AS OWNER --may be needed to get around metadata visibility issues of OBJECT_NAME
AS
SET NOCOUNT, XACT_ABORT ON;

BEGIN TRY
    SET @ErrorMessage = --cutdown
            CASE
                WHEN @errproc = @callerproc THEN        --Caller = error generator
                        --build up stuff

                ELSE    --Just append stuff             --Nested error stack
            END;

    IF @@TRANCOUNT = 0
        INSERT dbo.Exception (Who, TheError, WhatBy, LoggedBy)
        VALUES (ORIGINAL_LOGIN()), RTRIM(ERROR_MESSAGE()), ERROR_PROCEDURE(), OBJECT_NAME(@CallerProcID));
END TRY
BEGIN CATCH
   --and do what exactly?
END CATCH
GO

无论如何,这是基本思想:每个 CATCH 块都很简单,错误处理程序中的工作继续进行。例如,如果您愿意,请附加 ERROR_NUMBER()

Ok, I'll add our error handling back in :-)

The ERROR_%() functions are visible to the scope of the CATCH block. This means you can use them in a stored proc or function call in each CATCH block

And with nested stored procs, it's useful to know what caused the error and what's logging the error

...
END TRY
BEGIN CATCH
    IF XACT_STATE() <> 0 AND @starttrancount = 0 
        ROLLBACK TRANSACTION
    EXEC dbo.MyExceptionHandler @@PROCID, @errmsg OUTPUT;
    RAISERROR (@errmsg, 16, 1);
END CATCH

---with this handler (cut down version of ours)
CREATE PROCEDURE dbo.MyExceptionHandler
    @CallerProcID int,
    @ErrorMessage varchar(2000) OUTPUT
WITH EXECUTE AS OWNER --may be needed to get around metadata visibility issues of OBJECT_NAME
AS
SET NOCOUNT, XACT_ABORT ON;

BEGIN TRY
    SET @ErrorMessage = --cutdown
            CASE
                WHEN @errproc = @callerproc THEN        --Caller = error generator
                        --build up stuff

                ELSE    --Just append stuff             --Nested error stack
            END;

    IF @@TRANCOUNT = 0
        INSERT dbo.Exception (Who, TheError, WhatBy, LoggedBy)
        VALUES (ORIGINAL_LOGIN()), RTRIM(ERROR_MESSAGE()), ERROR_PROCEDURE(), OBJECT_NAME(@CallerProcID));
END TRY
BEGIN CATCH
   --and do what exactly?
END CATCH
GO

This is the basic idea anyway: each CATCH block is simple, the work goes on in the error handler. Eg append ERROR_NUMBER() if you want to

夜还是长夜 2024-08-25 14:44:47

对此的有限答案是将 OBJECT_NAME(@@PROCID) 传递给 ReportError 过程 - 当 ReportError 检测到它正在接收递归错误(自身引发的错误)时,它可以使用该值并将其附加到错误消息中,提供部分堆栈跟踪(堆栈跟踪除了第一个元素之外没有行号)

A limited answer to this would be to pass OBJECT_NAME(@@PROCID) to the ReportError procedure - when ReportError detects that it is receving a recursive error (an error thrown by itself), it can use this value and append it to the error message, providing a partial stack trace (stack trace won't have line numbers except for the first element)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文