Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database Exceptions Are Unchecked In Scaffolded Controllers #25

Open
markoschnecke opened this issue May 26, 2016 · 0 comments
Open

Database Exceptions Are Unchecked In Scaffolded Controllers #25

markoschnecke opened this issue May 26, 2016 · 0 comments

Comments

@markoschnecke
Copy link

Steps to Reproduce

  1. create a domain (domain1) that is empty
  2. create another domain (domain2) that has a field Domain1 domain1 as a one-to-one relationship
  3. create controllers for the domains with static scaffold = Domain1 / Domain2
  4. start the application
  5. create an instance of domain1 (object1)
  6. create an instance of domain2 that references object1
  7. delete object1

Expected Behaviour

The application should give an error message that the object cannot be deleted due to the violated foreign key constraint.

Actual Behaviour

The application prints a whole stack trace, which would only be helpful for developers but not for normal users

How It Can Be Fixed

By surrounding the method call delete() with a try-catch block in the Controller-Template the problem would be fixed:

import org.springframework.dao.DataIntegrityViolationException

...

def delete(${className} ${propertyName}) {

...

    try {
        ${propertyName}.delete(flush: true)
        flash.message = message(code: 'default.deleted.message', args: [message(code: '${propertyName}.label', default: '${className}'), params.id])
        redirect(action: "index")
    }catch (DataIntegrityViolationException e) {
        flash.message = message(code: 'default.not.deleted.message', args: [message(code: '${propertyName}.label', default: '${className}'), params.id])
        redirect(action: "show", id: params.id) 
    }
}

Environment Information

  • Operating System: Ubuntu
  • Grails Version: 3.1.7
  • JDK Version: 1.7

Example Application

See attachment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant