说服我,我在使用异常进行用户验证方面是错误的

发布于 2024-11-09 09:10:41 字数 1901 浏览 0 评论 0原文

尽管很多人说你不应该使用异常来处理错误的用户输入。尽管如此,我并不认为在我的具体情况下这样做是坏事。你能试着解释一下为什么我错了吗?

我的代码(请仅关注异常处理方面)如下。我在这里使用异常的理由是,如果我不这样做,假设我希望验证逻辑接近关键字解析(因为解析和验证紧密耦合),我将不得不更改三个方法(submitOnAdd、submitOnUpdate、 getKeywords)让他们处理这种特殊情况。您认为我在这种情况下使用异常肯定是错误的,还是个人风格的问题?

public SubmitResponse internalSubmit(Map<String, String[]> submitParameters) {
  try {
      if (!submitParameters.containsKey("foo")) {
        return submitOnAdd(submitParameters);
      } else {
        return submitOnModify(submitParameters);
      }
  } catch (SubmitErrorException e) {
      return SubmitResponse.fieldError(Arrays.asList(e.getSubmitError()));
  }
}

SubmitResponse submitOnAdd(Map<String, String[]> submitParamters) {
  // do some stuff
  // ...
  if (addKeywordList(createKeywordList(submitParameters.get("concatenated_keywords"))
    return SubmitResponse.OK();
  return SubmitResponse.bad("Failed to add");
}

SubmitResponse submitOnUpdate(Map<String, String[]> submitParamters) {
  // do some other stuff
  // ...
  if (updateKeywordList(createKeywordList(submitParameters.get("concatenated_keywords"))
    return SubmitResponse.OK();
  return SubmitResponse.bad("Failed to update");
}

List<Keyword> getKeywords(String concatenatedKeywords) {
  List<String> rawKeywords = splitKeywords(concatenatedKeywords);
  return Collections.transform(new Function<String, Keyword>() {
      @Override
      public KeywordListProto.Keyword apply(String expression) {
        return buildKeyword(expression);
      }
    });
}

private Keyword buildKeyword(String rawKeyword) {
  // parse the raw keyword
  if (/*parsing failed */)
    throw new SubmitResponseException("Failed to parse keyword " + rawKeyword);

  return parsedKeyword;
}

It's hard to find a consensus, although many people say you should not use exceptions to handle bad user input. Still, I'm not convinced that it's the bad thing to do in my specific case. Could you try to explain why I'm wrong?

My code (please focus just on the exception handling aspect) follows. My rational for using exceptions here is that if I didn't, assuming I would want to keep the validation logic close to the keyword parsing (because parsing and validation are tightly coupled), I would have to change three methods (submitOnAdd, submitOnUpdate, getKeywords) to make them handle this exceptional situation. Do you think I definitely wrong in this case to use exceptions, or is it a matter of personal style?

public SubmitResponse internalSubmit(Map<String, String[]> submitParameters) {
  try {
      if (!submitParameters.containsKey("foo")) {
        return submitOnAdd(submitParameters);
      } else {
        return submitOnModify(submitParameters);
      }
  } catch (SubmitErrorException e) {
      return SubmitResponse.fieldError(Arrays.asList(e.getSubmitError()));
  }
}

SubmitResponse submitOnAdd(Map<String, String[]> submitParamters) {
  // do some stuff
  // ...
  if (addKeywordList(createKeywordList(submitParameters.get("concatenated_keywords"))
    return SubmitResponse.OK();
  return SubmitResponse.bad("Failed to add");
}

SubmitResponse submitOnUpdate(Map<String, String[]> submitParamters) {
  // do some other stuff
  // ...
  if (updateKeywordList(createKeywordList(submitParameters.get("concatenated_keywords"))
    return SubmitResponse.OK();
  return SubmitResponse.bad("Failed to update");
}

List<Keyword> getKeywords(String concatenatedKeywords) {
  List<String> rawKeywords = splitKeywords(concatenatedKeywords);
  return Collections.transform(new Function<String, Keyword>() {
      @Override
      public KeywordListProto.Keyword apply(String expression) {
        return buildKeyword(expression);
      }
    });
}

private Keyword buildKeyword(String rawKeyword) {
  // parse the raw keyword
  if (/*parsing failed */)
    throw new SubmitResponseException("Failed to parse keyword " + rawKeyword);

  return parsedKeyword;
}

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

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

发布评论

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

评论(1

不语却知心 2024-11-16 09:10:41

我不能说我永远不会建议在输入验证中的某个地方使用异常。但在这种情况下,我想说这会增加很多混乱。我会:

  • 添加一个单独的方法来处理验证。 (可能必须在多个地方调用此方法,这是一个缺点,但它可能会使代码更容易理解)。
  • 在更理想的情况下,我会在更接近用户输入的地方进行验证,并且不允许提交无效数据。 (一个可能的负面影响是验证和解析逻辑的分离,但如果您可以以某种方式使用同一个类来完成这两个任务,则可以避免这种情况)。

I can't say I would never advise to use Exceptions somewhere in input validation. But in this case, I would say it adds to much confusion. I would either:

  • Add a separate method to handle the validation. (Might have to call this method in several places which is a negative, but it might make the code easier to understand).
  • In the more ideal case, I would validate closer to the user input and not allow a submit of invalid data. (A possible negative is separation of validation and parsing logic, but if you could somehow use the same class to do both this could be avoided).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文