title: Invalid JSON string error in Rails
body: <p>I'm on Rails 2.3.5. In a typical user controller create action </p>
  
  <pre><code>class UsersController
    def create
      @user = User.new(params[:user])
      respond_to do |format]
        if @user.save ...
        else
          format.json ....
        end        
    end
  end
  </code></pre>
  
  <p>When a client passes an invalid / malformed JSON string for input, Rails throw a 500 internal server error saying "Invalid JSON string". Is it possible for me to trap the error so I can give a custom message? </p>
  
  <p><strong>UPDATED</strong> Here's the stacktrace as requested. Just so I'm clear, I know this is a malformed JSON string, my question is not how to fix the JSON string but rather how to trap this particular error so I can send back a more meaningful error message than HTTP 500 Internal Server Error. Thanks in advance for your help</p>
  
  <pre><code>Error occurred while parsing request parameters.
  Contents:
  
  user: {login: "John", email: "john@yahoo.com", password: "111"}}
  /!\ FAILSAFE /!\  Mon Nov 08 02:01:04 -0800 2010
  
    Status: 500 Internal Server Error
    Invalid JSON string
      c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/json/backends/yaml.rb:14:in `decode'
      c:1:in `__send__'
      c:1:in `decode'
      c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/params_parser.rb:42:in `parse_formatted_parameters'
      c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/params_parser.rb:11:in `call'
      c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/session/cookie_store.rb:93:in `call'
      c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/failsafe.rb:26:in `call'
      c:/ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `call'
      c:/ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `synchronize'
      c:/ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `call'
      c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:114:in `call'
      c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/reloader.rb:34:in `run'
      c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:108:in `call'
      c:/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails/rack/static.rb:31:in `call'
      c:/ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:46:in `call'
      c:/ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:in `each'
      c:/ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:in `call'
      c:/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails/rack/log_tailer.rb:17:in `call'
      ...
      c:/ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/handler/mongrel.rb:34:in `run'
      c:/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/commands/server.rb:111
      c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
      c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
      script/server:3
  re.rb:31:in `require'
      script/server:3
  </code></pre>