Grails
  1. Grails
  2. GRAILS-8605

g.eachError(bean: instanceName) returns error message while used in controllers

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0 final
    • Fix Version/s: 2.0.4
    • Component/s: Controllers
    • Labels:

      Description

      I always used g.eachError(bean: instanceName) in my controllers (and also in services) in Grails 1.3.7 and it worked like a charm. After an upgrade to Grails 2.0.0 it seems that g.eachError(bean: instanceName) doesnt work anymore.

      Here the errormessage from the grails-error page / stacktrace in netbeans:

      Error 500: Internal Server Error
      URI: /savetest/test/save
      Class: java.lang.NullPointerException
      Message: null

      Around line 22 of grails-app/controllers/savetest/TestController.groovy

      NullPointerException occurred when processing request: [POST] /savetest/test/save - parameters:
      vorname:
      test: test
      name:
      Stacktrace follows:
      Message: null
      Line | Method
      ->> 22 | doCall in savetest.TestController$_closure2$$ENLSCNLc

      • - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        886 runTask in java.util.concurrent.ThreadPoolExecutor$Worker
        908 run . . in ''
        ^ 680
        run in java.lang.Thread

      //Stacktrace taken from the error.gsp (extended it with the whole code)
      18: if(testInstance.save())

      { 19: //Save successfull, go on 20: }

      else { //Constraint errors, prepare JSON response with error messages.
      21: //This doenst work anymore
      22: g.eachError(bean: testInstance) {
      23: postResponse.errors."$

      {it.field}" = g.message(error: it)
      24: }
      25: //This one works perfect, but seems inefficient to me
      26: testInstance.errors.allErrors.each {
      27: //Do Something with the error
      28: postResponse.errors."${it.field}

      " = g.message(code: "$

      {testInstance.class.name}

      .$

      {it.field}

      .$

      {testInstance.errors.getFieldError(it.field).code}

      .error")
      29: }

      It seems to me that the non persisted domain objects causes this error. But i am not 100% sure.

        Activity

        Hide
        Dominik Tamm added a comment -

        After noticing the Wiki-Markup ability to show formatted Code... here it is (nice and tidy):

        if(testInstance.save()) { 
            //Save successfull, go on
        } else { //Constraint errors, prepare JSON response with error messages.
            //This doenst work anymore
            g.eachError(bean: testInstance) {
              postResponse.errors."${it.field}" = g.message(error: it)
            }
            //This one works perfect, but seems inefficient to me
            testInstance.errors.allErrors.each {
              //Do Something with the error
              postResponse.errors."${it.field}" = g.message(code: "${testInstance.class.name}.${it.field}.${testInstance.errors.getFieldError(it.field).code}.error")
            }
        }
        
        Show
        Dominik Tamm added a comment - After noticing the Wiki-Markup ability to show formatted Code... here it is (nice and tidy): if (testInstance.save()) { //Save successfull, go on } else { //Constraint errors, prepare JSON response with error messages. //This doenst work anymore g.eachError(bean: testInstance) { postResponse.errors. "${it.field}" = g.message(error: it) } //This one works perfect, but seems inefficient to me testInstance.errors.allErrors.each { //Do Something with the error postResponse.errors. "${it.field}" = g.message(code: "${testInstance.class.name}.${it.field}.${testInstance.errors.getFieldError(it.field).code}.error" ) } }

          People

          • Assignee:
            Lari Hotari
            Reporter:
            Dominik Tamm
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Last Reviewed:

              Development