如何摆脱双重错误groovy

时间:2011-11-24 14:39:50

标签: grails groovy

我在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错误...

2 个答案:

答案 0 :(得分:0)

您可以向applicationImage字段添加自定义验证器,以便仅在customErrorMessage字段不为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']
                    }
                }
        )
    }

可能需要在错误消息中进行调整,但我希望你明白我的观点;)

答案 1 :(得分:0)

applicationImage(maxSize: MAX_SIZE,
            validator: {val, obj ->
            if (obj.customErrorMessage == null) {
                if (val == null) return ['nullable']

            }
        })