gpt4 book ai didi

sql - 从异常中提取用户定义的错误消息

转载 作者:行者123 更新时间:2023-11-29 11:22:35 25 4
gpt4 key购买 nike

在我的存储过程中,我只是检查了一个特定的条件,如果该条件失败,我将抛出异常。如下所示,

Raise Exception '%', 'Hello world';

它工作正常,但是错误消息 Hello world 还带有一些其他错误代码,例如我的前端中的以下内容,

DA00014:ERROR: P0001: Hello world
|___________________|
|
I really dont want this part to get displayed in the front end.

是否有任何合适的方法可以从抛出的异常中过滤掉/提取正确的消息。?

[注意:请不要建议我进行一些字符串操作。]

DBMS      : POSTGRESQL 9.0.3
Driver : Npgsql 1.0
Front End : VB.net

最佳答案

Npgsql 快速路径

如果 NpgsqlException.Errors 在您的异常处理程序中为空,那么您很可能遇到了 Npgsql Fastpath 中的错误(无论如何我认为它是错误)。 Here is the offending line在版本 2 中,但在版本 1 中存在同样的问题。

它基本上归结为这段代码,其中构造了适当的 NpgsqlError,然后在通过调用 ToString() 引发异常时将其丢弃。

NpgsqlError e = new NpgsqlError(conn.BackendProtocolVersion, stream);
throw new NpgsqlException(e.ToString());

解决方法很简单,只需将代码更改为此,并使用 NpgsqlException 已经支持的功能在构造函数中获取 NpgsqlError 列表。

NpgsqlError e = new NpgsqlError(conn.BackendProtocolVersion, stream);
throw new NpgsqlException(new ArrayList(e));

所以总而言之,除非您编译自己的 Npgsql 版本来修补该问题,否则您不能在没有字符串操作的情况下做到这一点。

Npgsql

如果您不使用 Fastpath,则抛出的 NpgsqlException 对象包含一个 Errors 属性,该属性应该是一个非空列表。这是从第一个错误中提取消息的示例。

(ex.Errors[0] as NpgsqlError).Message

关于sql - 从异常中提取用户定义的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19493255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com