gpt4 book ai didi

grails - 如何摆脱双重错误

转载 作者:行者123 更新时间:2023-12-02 14:55:16 25 4
gpt4 key购买 nike

在再次抛出该错误并检查静态约束的其他属性的空值并抛出该错误之后,我在try块中对大型文件进行了验证。

返回第一个错误后如何停止流程?

这是代码

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 [];
}

在出现大文件大小错误之后,未设置应用程序镜像,因此它引发的应用程序镜像也为null错误...

最佳答案

您可以将自定义验证器添加到applicationImage字段,以仅在customErrorMessage字段不为null时检查null值。
这样,如果customErrorMessage不为null,则只会在验证异常中得到applicationImage错误。

在验证程序闭包中,您可以访问要检查的字段的值以及整个对象:

myField(validator: { val, obj -> return propertyName == "myField" })

因此,您可以执行以下操作:
static constraints = {
applicationName(blank: false, size: 1..25)
applicationShortName(blank: false, size: 1..10)
applicationImage(validator: {val, obj ->
if (obj.customErrorMessage != null) {
if (val == null) return ['imageNull']
if (val.size() > MAX_SIZE) return ['tooLarge']
}
})
contentProviderId (
validator: {
if (it == 0) {
return ['notSelected']
}
}
)
customErrorMessage (
validator: {
if ("fileToBig".equals(it)) {
return ['fileToBig']
}
}
)
}

可能需要对错误消息进行一些调整,但是希望您能理解我的观点;)

关于grails - 如何摆脱双重错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8258779/

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