exception handling not working?

I am playing with Rails 3.1.1 and made a simple blog app.

I created a unique index on a field and when I submit a new post, it

throws an exception if the field is not unique. I’ve added a rescue, but it’s not doing anything? What am I doing wrong.

 def create
    @post = Post.new(params[:post])

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was

successfully created.’ } format.json { render json: @post, status: :created, location: @post } else format.html { render action: “new” } format.json { render json: @post.errors, status: :unprocessable_entity } end end rescue # SQLite3::ConstraintException render action: “new” end

SQLite3::ConstraintException in PostsController#create

column title is not unique

Rails.root: /home/yadav/dev/rails/tmp/blog

Application Trace | Framework Trace | Full Trace

activerecord (3.1.1) lib/active_record/connection_adapters/sqlite_adapter.rb:84:in `close'
activerecord (3.1.1) lib/active_record/connection_adapters/sqlite_adapter.rb:84:in `dealloc'
activerecord (3.1.1) lib/active_record/connection_adapters/sqlite_adapter.rb:73:in `block in clear'
activerecord (3.1.1) lib/active_record/connection_adapters/sqlite_adapter.rb:72:in `each'
activerecord (3.1.1) lib/active_record/connection_adapters/sqlite_adapter.rb:72:in `clear'
activerecord (3.1.1) lib/active_record/connection_adapters/sqlite_adapter.rb:148:in `clear_cache!'
activerecord (3.1.1) lib/active_record/connection_adapters/sqlite_adapter.rb:142:in `disconnect!'
activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:214:in `block in clear_reloadable_connections!'
activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:213:in `each'
activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:213:in `clear_reloadable_connections!'
activesupport (3.1.1) lib/active_support/core_ext/module/synchronization.rb:35:in `block in clear_reloadable_connections_with_synchronization!'
/usr/local/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
activesupport (3.1.1) lib/active_support/core_ext/module/synchronization.rb:34:in `clear_reloadable_connections_with_synchronization!'
activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:391:in `block in clear_reloadable_connections!'
activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:391:in `each_value'
activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:391:in `clear_reloadable_connections!'
activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_specification.rb:123:in `clear_reloadable_connections!'
activerecord (3.1.1) lib/active_record/railtie.rb:84:in `block (3 levels) in <class:Railtie>'
activesupport (3.1.1) lib/active_support/callbacks.rb:404:in `_run_cleanup_callbacks'
activesupport (3.1.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.1) lib/action_dispatch/middleware/reloader.rb:72:in `rescue in call'
actionpack (3.1.1) lib/action_dispatch/middleware/reloader.rb:67:in `call'
rack (1.3.5) lib/rack/sendfile.rb:101:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.1.1) lib/rails/rack/logger.rb:13:in `call'
rack (1.3.5) lib/rack/methodoverride.rb:24:in `call'
rack (1.3.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.1.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.3.5) lib/rack/lock.rb:15:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/static.rb:53:in `call'
railties (3.1.1) lib/rails/engine.rb:456:in `call'
railties (3.1.1) lib/rails/rack/content_length.rb:16:in `call'
railties (3.1.1) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.3.5) lib/rack/handler/webrick.rb:59:in `service'
/usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/usr/local/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

Request

Parameters:

 {"utf8"=>"✓",
"authenticity_token"=>"c713B1JrYpXMZuH6DnwDAbD3itjj7MAZb1GfgHYLZ9g=",
"post"=>{"author"=>"Rajinder Yadav",
"title"=>"test post",
"message"=>"this is cool"},
"commit"=>"Create Post"}

Show session dump

_csrf_token: "c713B1JrYpXMZuH6DnwDAbD3itjj7MAZb1GfgHYLZ9g="
session_id: "a3784c99107db6ec649d06174043aa2f"

Show env dump

GATEWAY_INTERFACE: "CGI/1.1"
HTTP_ACCEPT: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
HTTP_ACCEPT_CHARSET: "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
HTTP_ACCEPT_ENCODING: "gzip, deflate"
HTTP_ACCEPT_LANGUAGE: "en-us,en;q=0.5"
REMOTE_ADDR: "127.0.0.1"
REMOTE_HOST: "localhost"
SERVER_NAME: "localhost"
SERVER_PROTOCOL: "HTTP/1.1"

Response

Headers:

None

OK I figured out this is a bug with webbrick, it errors out and then crashes!

I installed passenger + nginx and the code works as expected.

Rajinder

But not optimal I think.

If you replaced the lines

rescue

SQLite3::ConstraintException

with

rescue SQLite3::ConstraintException

I would expect your code to also work correctly for the 2 cases that:

  • the id is unique and all is well

  • there is a non-unique id (causing the SQLite3::ConstraintException)

which will still be caught by the rescue

but … if any of the 100 other possible reasons for exception

occur, you will not longer be blind to them… I suggest catching

the most fine grained Exception class you can, so you are not

blind to other unexpected Exceptions.

HTH,

Peter

I tried that first, exactly the same thing happens. The odd thing is if I add the same code to update and modify the title field to not be unique, the exception handling works fine during a update using webrick as the server.

there is a bug with webrick, not sure who i should report this to, i can easily repro the crashing.

Plain "rescue" just catches exceptions derived from the StandardError class. IIRC, "rescue Exception" will catch everything.

HTH,   Jeffrey

Quoting Rajinder Yadav <devguy.ca@gmail.com>:

Plain "rescue" just catches exceptions derived from the StandardError class. IIRC, "rescue Exception" will catch everything.

HTH,    Jeffrey

This still does not fix the error, the exception goes unhandled then webrick crashes on the next server request.

The webpage shows:

Internal Server Error column title is not unique WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09) at localhost:3000