title: where to load grails message when throwing exception in a service
body: <p>Where should the separation of the UI message elements be if a grails service throws an exception?  Should the message get loaded by the service and passed to the controller via the exception, or should the controller load the message based on the type of exception thrown?  This assumes the message will have some parameter values that need to be filled in.</p>
  
  <p>Here is an exception:</p>
  
  <pre><code>class CustomException extends RuntimeException {
      String message
  }
  </code></pre>
  
  <p>Loading the message source from the controller after catching the exception:</p>
  
  <pre><code>class MyService {
      void doSomething() {
          ...
          if (somethingBad) {
              String value = 'Mary Smith'
              throw new CustomException(value)
          }
          ...
      }
   }
   class MyController {
      def myService
  
      void processRequest() {
          try {
              myService.doSomething()
          }
          catch (CustomException e) {
              flash.error = g.message(code:'user.input.error', args:'[${e.value}]')
              render view:'some_gsp'
          }
          ...
      }
   }
  </code></pre>
  
  <p>Loading error from message source in the service where the controller pulls the message string the from the exception:</p>
  
  <pre><code>class MyService {
      def messageSource
      void doSomething() {
          ...
          if (somethingBad) {
              String value = 'Mary Smith'
              throw new CustomException(messageSource.getMessage('thread.inactive.user', [value]))
          }
          ...
      }
  }
  class MyController {
      def myService
  
      void processRequest() {
          try {
              myService.doSomething()
          }
          catch (CustomException e) {
              flash.error = e.message
              render view:'some_gsp'
          }
          ...
      }
  }
  </code></pre>