【发布时间】:2015-09-16 19:06:44
【问题描述】:
似乎无法找到在 Grails 应用程序中捕获 MySQL 索引违规的正确方法。来自控制器的代码:
try {
configurationInstance.save(flush: true) {
request.withFormat {...}
}
catch (JDBCException exception) {
flash.error = "This would be a duplicate attribute, please change the suffix."
render view: 'edit', model: [exception: exception]
}
当我通过提交重复值引发错误条件时,catch 块被忽略,我得到一个包含以下详细信息的 500 错误页面:
URI /itools/configuration/save
类 com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException
消息重复条目 '1009303-photo-recommendable-exclusion-pattern.2-0' 键'publisher_config_uk'
在控制台中我有: ERROR errors.GrailsExceptionResolver - MySQLIntegrityConstraintViolationException 在处理请求时发生:[POST] /itools/configuration/save - 参数: 我试图抓住
- JDBC异常
- SQL 错误
- SQLException
- SQLDataException
- SQLIntegrityConstraintViolationException
- MySQLIntegrityConstraintViolationException
- com.mysql.jdbc.exceptions.jdbc4.MySQLDataException
一切都无济于事。 (FWIW 我还在相关属性上应用了 unique 约束,当它应该导致验证错误时它什么也不做。这是一个完全不同的问题。)
这样做的正确方法是什么?
【问题讨论】: