首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误中的ValidationException错误

错误中的ValidationException错误
EN

Stack Overflow用户
提问于 2016-01-30 12:51:46
回答 1查看 846关注 0票数 0

在grails 2.3中,我想通过JSON发送定制的错误消息。但是当我使用

代码语言:javascript
复制
class ApiError {
    String message
    String status
    Exception error
}

并尝试解析它,我得到了:(我省略了无关的消息和状态as JSON )

代码语言:javascript
复制
"error": {
    "cause": null
    "class": "grails.validation.ValidationException"
    "errors": {
        "errors": [1]
        0:  {
            "object": "com.example.Firm"
            "field": "context"
            "rejected-value": null
            "message": "Property [context] of class [class com.example.Firm] cannot be null"
        }
    }
    "localizedMessage": "Firm is not valid: - Field error in object 'com.example.Firm' on field 'context': rejected value [null]; codes [com.example.Firm.context.nullable.error.com.example.Firm.context,com.example.Firm.context.nullable.error.context,com.example.Firm.context.nullable.error.java.lang.String,com.example.Firm.context.nullable.error,firm.context.nullable.error.com.example.Firm.context,firm.context.nullable.error.context,firm.context.nullable.error.java.lang.String,firm.context.nullable.error,com.example.Firm.context.nullable.com.example.Firm.context,com.example.Firm.context.nullable.context,com.example.Firm.context.nullable.java.lang.String,com.example.Firm.context.nullable,firm.context.nullable.com.example.Firm.context,firm.context.nullable.context,firm.context.nullable.java.lang.String,firm.context.nullable,nullable.com.example.Firm.context,nullable.context,nullable.java.lang.String,nullable]; arguments [context,class com.example.Firm]; default message [Property [{0}] of class [{1}] cannot be null] "
    "message": "firm is not valid: - Field error in object 'com.example.Firm' on field 'context': rejected value [null]; codes [com.example.Firm.context.nullable.error.com.example.Firm.context,com.example.Firm.context.nullable.error.context,com.example.Firm.context.nullable.error.java.lang.String,com.example.Firm.context.nullable.error,firm.context.nullable.error.com.example.Firm.context,firm.context.nullable.error.context,firm.context.nullable.error.java.lang.String,firm.context.nullable.error,com.example.Firm.context.nullable.com.example.Firm.context,com.example.Firm.context.nullable.context,com.example.Firm.context.nullable.java.lang.String,com.example.Firm.context.nullable,firm.context.nullable.com.example.Firm.context,firm.context.nullable.context,firm.context.nullable.java.lang.String,firm.context.nullable,nullable.com.example.Firm.context,nullable.context,nullable.java.lang.String,nullable]; arguments [context,class com.example.Firm]; default message [Property [{0}] of class [{1}] cannot be null] "
    "stackTrace": [...143]
    "suppressed": [0]
}

我不明白;在错误中使用错误数组有什么意义?如何只得到一个错误数组呢?Here是这个类的源代码。

EN

回答 1

Stack Overflow用户

发布于 2016-02-01 00:40:05

Grails验证错误包括全局错误和字段错误。每当grails中发生验证异常时,它不会为每个字段错误抛出新的异常,而是将所有字段错误包装在一个error对象中。可能存在全局对象错误,这是拒绝对象的全局原因。

因此,如果您希望避免错误内部的错误,那么您应该修改您的ApiError类,将Exception error替换为List<Errors> errors,并从instance.errors设置它的值。

或者,您可以使用自定义方法解析错误: CodecLookup codecLookup

代码语言:javascript
复制
protected List retrieveErrors(instance) {

    CodecLookup codec = Holders.applicationContext.getBean(CodecLookup)

    List errors = []
    g.eachError([bean: instance], {
        error ->
            errors.add(codec.lookupDecoder('HTML').decode(g.message(error: error)))
    })

    return errors
}

g是每个控制器中可用的ValidationTagLib的名称空间。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35098129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档