说服我,我在使用异常进行用户验证方面是错误的
尽管很多人说你不应该使用异常来处理错误的用户输入。尽管如此,我并不认为在我的具体情况下这样做是坏事。你能试着解释一下为什么我错了吗?
我的代码(请仅关注异常处理方面)如下。我在这里使用异常的理由是,如果我不这样做,假设我希望验证逻辑接近关键字解析(因为解析和验证紧密耦合),我将不得不更改三个方法(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不能说我永远不会建议在输入验证中的某个地方使用异常。但在这种情况下,我想说这会增加很多混乱。我会:
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: