我有一個try塊中的大文件大小的驗證,拋出該錯誤之後,它再次去檢查靜態約束的其他屬性的空值,並拋出該錯誤。如何擺脫雙重錯誤groovy
如何在返回第一個錯誤後停止流程?
這裏是大文件大小錯誤後的代碼
static constraints = {
applicationName(blank: false, size: 1..25)
applicationShortName(blank: false, size: 1..10)
applicationImage(nullable: false, maxSize: MAX_SIZE)
contentProviderId (
validator: {
if (it == 0) {
return ['notSelected']
}
}
)
customErrorMessage (
validator: {
if ("fileToBig".equals(it)) {
return ['fileToBig']
}
}
)
}
try {
CommonsMultipartFile file = request.getFile('applicationImageUrl');
logger.debug("POSTPROCESS: is file empty=${file.isEmpty()}")
if(!file.isEmpty()) {
try {
-- other logic
}
catch (Exception ex) {
logger.warn("Failed to upload file - improper file type", ex)
return [];
}
logger.debug("Getting new image file")
try {
-- logic
if (file.size <= MAX_SIZE) {
-- logic
} else {
customErrorMessage = "fileToBig"; (ERROR FOR BIG FILE SIZE)
}
} catch (Exception e) {
logger.warn("Failed to upload file", e)
customErrorMessage = "fileToBig";
}
} else {
logger.debug("File was empty. Will check if there is a file in submission")
if (submission.applicationImage != null && submission.applicationImage != []) {
logger.debug("submission contains applicationImage=${submission.applicationImage}")
this.applicationImage = submission.applicationImage;
}
}
} catch (Exception e) {
this.errors.reject("error","An error occured when uploading file. Please try again.");
logger.error("Failed to upload file", e);
return [];
}
--logic
if (application != null) { //Application already exists!
submission.applicationId = application.id;
return [next: 10];
}
return [];
}
,應用圖像沒有被設置,所以它的投擲應用圖像零誤差,以及...
我不知道有什麼辦法做到這一點。你能解釋爲什麼你想停止驗證,如果這個約束被違反?也許我可以提出另一種方法來實現你的目標。 –
請檢查編輯的問題...請看看它 – Techie