API设计:修改返回附加错误信息

发布于 2024-08-16 06:58:14 字数 548 浏览 4 评论 0原文

我有一个 API 定义如下

bool IsValid()

现在,我想修改该 API,以便如果返回值为 false,那么我需要额外的
验证失败的原因的信息。
我正在寻找一个优雅的方式来解决问题...以下是我的选项

选项 1:
bool IsValid(ref string errorMessage)

仅当结果为 false 时才会更新 errorMessage

选项 2:

class Result<T>
{
   bool Succeeded;
   T Argument;
}

Result<string> IsValid()

选项 3:

void Validate();
//throw an exception if it invalid, just return if succeeded.

我对上面列出的任何选项都不满意。所以,想知道是否有任何我可能不知道的优雅解决方案

I have an API defined as follows

bool IsValid()

Now, I want to modify the API so that if the return value is false, then I need additional
information why the validation failed.
I'm looking for an elegant want to solve the problem... Here are the options I have

Option 1:
bool IsValid(ref string errorMessage)

errorMessage is updated only if the result is false

option 2:

class Result<T>
{
   bool Succeeded;
   T Argument;
}

Result<string> IsValid()

Option 3:

void Validate();
//throw an exception if it invalid, just return if succeeded.

I'm not comfortable with any of the options listed above. So, wondering if there is any graceful solution that I might not be aware of

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

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

发布评论

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

评论(5

手长情犹 2024-08-23 06:58:14

您可能的错误消息是固定的集合吗?在这种情况下,我将在枚举中定义它们,然后创建一个返回枚举类型值的 Validate() 方法。这样做的另一个优点是,如果您需要向用户显示消息,则可以轻松使用本地化消息。

Are your possible error messages a fixed set? In that case, I would define them in an enumeration, then make a Validate() method that returns a value of the enum type. This has the added advantage that, if you need to show the message to the user, you can easily use localized messages.

っ左 2024-08-23 06:58:14

我会选择

int IsValid();

string GetErrorString(int id);

或者如果可以的话,更好:

ValidCode IsValid();

string GetErrorString(ValidCode code);

其中 ValidCode 将是一个枚举

I'd go for

int IsValid();

string GetErrorString(int id);

or if you can, better yet:

ValidCode IsValid();

string GetErrorString(ValidCode code);

where ValidCode would be an enum

水水月牙 2024-08-23 06:58:14

如果必须,您可以使用“string isValid()”,其中该方法将在成功时返回 null 或空字符串,否则返回某种错误消息。

但我个人永远不会这样做。让 API 执行不止一件事会增加复杂性。

If you must you could use "string isValid()" where the method would return null or empty string on success and some kind of an error message otherwise.

I would never do that personally though. Having APIs do more than one thing increases the complexity.

找回味觉 2024-08-23 06:58:14

如果您使用动态类型语言,您可以执行以下操作:

def IsValid():
   if condition:
       return (True, "")
   else:
       return (False, "error message")

但是,我真的很喜欢异常方法。它是标准的,可以在失败代码之上的任何级别进行处理。

If you were using a dynamically typed language you could do something like:

def IsValid():
   if condition:
       return (True, "")
   else:
       return (False, "error message")

However, I would really spring for the exception method. It's standard and can be handled at any level above the failing code.

ゃ人海孤独症 2024-08-23 06:58:14

以防万一您想坚持使用选项 3:当验证因输入错误而失败时,validate() 不应抛出异常。异常应该在异常情况下抛出,而不是在您可能期望输入错误的情况下抛出。因此,我将按以下方式设计它

ValidationResult Validate()

,其中 ValidationResult 可能是

class ValidationResult
{
   bool IsValid;
   string Message;
}

验证方法应该仅在无法验证时抛出异常,例如,当它必须在数据库中查找值但无法建立连接时。

编辑:哇。没看到帖子的日期……我猜我有点晚了……

Just in case you wants to stick with option 3: validate() should not throw an exception in cases the validation fails because of wrong input. Exceptions should be thrown in exceptional cases and not in cases where you might expect wrong input. So I would design it in the following way

ValidationResult Validate()

where ValidationResult could be

class ValidationResult
{
   bool IsValid;
   string Message;
}

The validation method should only throw an exception when it is not able to validate, e.g. when it has to lookup values in a database but a connection could not be established.

EDIT: Wow. Didn't see the date of the post.... Guess I am a bit late...

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