I develop a rest webservice that answers in json.
But when some (not all) exceptions are thrown, I am unable to catch them, and rails render an htlm, which is not a valid response for all my clients.
The problem comes if an exception is raised in a method called by this catch all:
rescue_from Exception, with: :render_unkown_error
I have to admit, raising an exception in a last chance catch all is very very critical,
but I wonder:
Is there a way to totally dismiss all rails response in case of such errors instead of returning an html page containing the stack trace ?
EDIT:
In fact this problems also comes for an undefined property in my controller:
ActionController::RoutingError (undefined local variable or method `truc' for V2::Model3dsController:Class):
app/controllers/v2/model3ds_controller.rb:8:in `<class:Model3dsController>'
app/controllers/v2/model3ds_controller.rb:4:in `<module:V2>'
app/controllers/v2/model3ds_controller.rb:3:in `<top (required)>'
even if I have the following rescue in my base controller:
rescue_from ActionController::RoutingError, with: :render_routing_error
EDIT2:
I have the same kind of problem if the user of my webservice send malformatted parameters to my webservice.
Since the execution flow do not pass through my code, the exception is not handled:
Started POST "/users" for XXX.XXX.XXX.XXX at 2013-05-07 11:11:27 +0000
Error occurred while parsing request parameters.
Contents:
MultiJson::LoadError (795: unexpected token at 'url=http%3A%2F%2Ftest.test.com%2Fusers'):
json (1.7.7) lib/json/common.rb:155:in `parse'
json (1.7.7) lib/json/common.rb:155:in `parse'
multi_json (1.7.2) lib/multi_json/adapters/json_common.rb:16:in `load'
multi_json (1.7.2) lib/multi_json/adapter.rb:19:in `load'
multi_json (1.7.2) lib/multi_json.rb:120:in `load'
activesupport (3.2.13) lib/active_support/json/decoding.rb:15:in `decode'
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:47:in `parse_formatted_parameters'
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:17:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `_run__3866502263790514870__call__1366634820855087578__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'
railties (3.2.13) lib/rails/engine.rb:479:in `call'
railties (3.2.13) lib/rails/application.rb:223:in `call'
railties (3.2.13) lib/rails/railtie/configurable.rb:30:in `method_missing'
thin (1.5.1) lib/thin/connection.rb:81:in `block in pre_process'
thin (1.5.1) lib/thin/connection.rb:79:in `catch'
thin (1.5.1) lib/thin/connection.rb:79:in `pre_process'
eventmachine (1.0.3) lib/eventmachine.rb:1037:in `call'
eventmachine (1.0.3) lib/eventmachine.rb:1037:in `block in spawn_threadpool'
Rendered /home/ubuntu/.rvm/gems/ruby-2.0.0-p0#websrv2.4.0/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.8ms)
Rendered /home/ubuntu/.rvm/gems/ruby-2.0.0-p0#websrv2.4.0/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.3ms)
Rendered /home/ubuntu/.rvm/gems/ruby-2.0.0-p0#websrv2.4.0/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (11.7ms)
What you want is exceptions_app.
Rails exceptions_app is basically a configuration option for rails which defaults to the middleware PublicExceptions in ActionDispatch.
This is pretty much what exceptions_app defaults to if none provided:
PublicExceptions
This is the public_exceptions middleware that handles displaying the public 500.html and 400.html
https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/public_exceptions.rb
Show Exceptions
This is part of the middleware that shows exceptions on development/production
https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/show_exceptions.rb#L26
Defaults
Here's what it defaults to when you initialize an application
https://github.com/rails/rails/blob/master/railties/lib/rails/application.rb#L391-L393
You can exchange it for an app or route it to your own rails apps like this:
config/application.rb
config.exceptions_app = self.routes
Then in your routes you would match routes like this
routes.rb
match "/500" => "errors#exception"
match "/404" => "errors#not_found"
So you would then have a ErrorsController that would have these actions and handle the response to them.
errors_controller.rb
class ErrorsController < ApiController
def exception
render json: {error: "Internal Error"}, status: 500
end
def not_found
render json: {error: "Not Found"}, status: 404
end
end
Related
Been stuck on this for a few days now. Any help would be much appreciated. I have a form that sends an artist name to a create controller. The create controller creates the artist with the name, assigns a user to that artist, and creates an artist layout. I think i have added the correct whitelisting for the strong params gem, however i get the the below error.
Error:
ActiveModel::ForbiddenAttributes in ArtistsController#create
ActiveModel::ForbiddenAttributes
Rails.root: /sites/music3
Application Trace | Framework Trace | Full Trace
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"xxxxxxxxxxxxxxxxxxxxxx",
"artist"=>{"name"=>"kkkk"},
"commit"=>"Create Artist"}
Show session dump
Show env dump
Response
Headers:
Controller
def create
#artist = Artist.new(artist_create_params)
#assigns User
#user = current_user
#artist.users << #user
#form = render_to_string('artists/_form',:layout => false)
#creates and assigns layout
#artist.profile_layout = ProfileLayout.new
respond_to do |format|
if #artist.update_attributes(artist_create_params)
format.html { redirect_to(edit_artist_path(#artist.url_slug)) }
format.xml { render :xml => #artist, :status => :created, :location => #artist }
else
format.html { render :action => "new" }
format.xml { render :xml => #artist.errors, :status => :unprocessable_entity }
end
end
end
def artist_create_params
#Using `strong_parameters` gem
params.required(:commit).permit!
params.required(:artist).permit!
end
I'm allowing all params to be past through (params!) as I was trying to identify what was throwing the error. However I would like to whitelist specific params once I figure out what the problem is.
Strong parameters is working in other places. Its just giving me problems on create. Any help would be appreciated
Thanks in advance.
Ted.
Update:
Initializer:
ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)
Changed to
def artist_create_params
# NOTE: Using `strong_parameters` gem
params.require(:artist).permit(:name)
end
Still getting above error. Doesn't show the line the error is occuring on. See below
Started POST "/artists" for 127.0.0.1 at 2014-06-04 19:10:27 -0400
Processing by ArtistsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxxxxxxxxxxxx", "artist"=>{"name"=>"ffasdf"}, "commit"=>"Create Artist"}
Completed 500 Internal Server Error in 8ms
ActiveModel::ForbiddenAttributes (ActiveModel::ForbiddenAttributes):
strong_parameters (0.2.3) lib/active_model/forbidden_attributes_protection.rb:11:in `sanitize_for_mass_assignment'
activerecord (3.2.11) lib/active_record/attribute_assignment.rb:75:in `assign_attributes'
activerecord (3.2.11) lib/active_record/base.rb:497:in `initialize'
cancan (1.6.8) lib/cancan/controller_resource.rb:85:in `new'
cancan (1.6.8) lib/cancan/controller_resource.rb:85:in `build_resource'
cancan (1.6.8) lib/cancan/controller_resource.rb:66:in `load_resource_instance'
cancan (1.6.8) lib/cancan/controller_resource.rb:32:in `load_resource'
cancan (1.6.8) lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
cancan (1.6.8) lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
activesupport (3.2.11) lib/active_support/callbacks.rb:440:in `_run__3264816457187544022__process_action__2854128236876807797__callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.11) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.11) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.11) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.11) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.11) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.11) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.11) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.11) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.11) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.11) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:601:in `call'
warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
warden (1.2.1) lib/warden/manager.rb:34:in `catch'
warden (1.2.1) lib/warden/manager.rb:34:in `call'
client_side_validations (3.2.5) lib/client_side_validations/middleware.rb:21:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.3) lib/rack/etag.rb:23:in `call'
rack (1.4.3) lib/rack/conditionalget.rb:35:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.3) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.3) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.11) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `_run__1232201288606729007__call__1474813276301895872__callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.11) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.11) lib/rails/rack/logger.rb:18:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.3) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.3) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.11) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.3) lib/rack/lock.rb:15:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.11) lib/rails/engine.rb:479:in `call'
railties (3.2.11) lib/rails/application.rb:223:in `call'
rack (1.4.3) lib/rack/content_length.rb:14:in `call'
railties (3.2.11) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.3) lib/rack/handler/webrick.rb:59:in `service'
/Users/therealtedkennedy/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/Users/therealtedkennedy/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/Users/therealtedkennedy/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
Based on the stack trace, it looks like the assignment that triggers the error is coming from cancan, not from setting the artist's name. Try temporarily disabling the before_filter that uses cancan and see whether that resolves the issue. If it does, then you need to re-enable it, dig into cancan and figure out how to make it play nicely with strong_parameters. I don't use it myself, but I think there's a fork for Rails 4 called cancancan, which might work with rails 3.2 and strong_parameters as well.
Unrelated to your immediate problem, but you should probably also look at upgrading to the latest version in the Rails 3.2.x series to pick up the different security fixes they've released.
You don't need to permit the :commit params because you aren't trying to mass assign them to anything. Just use:
params.require(:artist).permit!
Or even better:
params.require(:artist).permit(:name)
That said, I'm surprised you're getting the error you say, so fix that and report back.
Skipping the cancan authorization defeats the purpose of having cancan.
This is what I did:
Switch to CanCanCan
As per the CanCanCan docs:
This repo is a continuation of the dead CanCan project. Our mission is to keep CanCan alive and moving forward, with maintenance fixes and new features.
CanCanCan also supports Strong Parameters out of the box.
Create Private Methods for create_params or update_params
As per docs, create private methods in your controllers to handle attribute permissions:
private
def create_params
params.require(:speaking_lesson).permit(:name, :description, exercises_attributes: [:text])
end
And then you'll have access an instance of the new model for use, in your create method:
def create
# #speaking_lesson is automatically created by CanCanCan during load_and_authorize_resource
#speaking_lesson.user_id = current_user.id
... do other stuff ...
end
I'm in the process of upgrading a Rails 2.3 app to 3.2 - in doing so, one of the plugins I used to depend on ResourceController is no longer available in Rails 3 (at least not a good port that I've found). Thus, I've had to look to other options, each gem I've tried rc_rails and rd_resource_controller as well as inherited_resources has resulted in the same error:
Routing Error uninitialized constant [ResourceController|InheritedResources].
They are most certainly in my gemlock and I've restarted the server in testing them. It almost seems as if the gems did not install, for such an error to occur.
Here are my routes from resources :projects:
projects GET /projects(.:format) projects#index
POST /projects(.:format) projects#create
new_project GET /projects/new(.:format) projects#new
edit_project GET /projects/:id/edit(.:format) projects#edit
project GET /projects/:id(.:format) projects#show
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy
Here's the controller:
class ProjectsController < ResourceController::Base
#....
end
## OR
class ProjectsController < InheritedResources::Base
## ....
end
Which all result in the uninitialized constant:
Routing Error
uninitialized constant InheritedResources
Here's the traceback for the inherited resource:
Started GET "/projects/3903/edit" for 127.0.0.1 at 2014-01-30 10:17:55 -0600
ActionController::RoutingError (uninitialized constant InheritedResources):
app/controllers/projects_controller.rb:3:in `<top (required)>'
activesupport (3.2.16) lib/active_support/inflector/methods.rb:230:in `block in constantize'
activesupport (3.2.16) lib/active_support/inflector/methods.rb:229:in `each'
activesupport (3.2.16) lib/active_support/inflector/methods.rb:229:in `constantize'
actionpack (3.2.16) lib/action_dispatch/routing/route_set.rb:69:in `controller_reference'
actionpack (3.2.16) lib/action_dispatch/routing/route_set.rb:54:in `controller'
actionpack (3.2.16) lib/action_dispatch/routing/route_set.rb:32:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.16) lib/action_dispatch/routing/route_set.rb:608:in `call'
actionpack (3.2.16) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.16) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.16) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.16) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.16) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.16) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.16) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.16) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.16) lib/active_support/callbacks.rb:405:in `_run__542093477292722009__call__814078438384904527__callbacks'
activesupport (3.2.16) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.16) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.16) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.16) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.16) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.16) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.16) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.16) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.16) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.16) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.16) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.16) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.16) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.16) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.16) lib/action_dispatch/middleware/static.rb:63:in `call'
railties (3.2.16) lib/rails/engine.rb:484:in `call'
railties (3.2.16) lib/rails/application.rb:231:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.16) lib/rails/rack/debugger.rb:20:in `call'
railties (3.2.16) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
/Users/myself/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/myself/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/myself/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
Rendered /Users/myself/.rvm/gems/ruby-1.9.3-p448#myapp/gems/actionpack-3.2.16/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.6ms)
Here's the Gemfile and Gemfile.lock for inherited:
[myself]:myapp(upgrade/rails-3.2)$ cat Gemfile | grep inherited
gem 'inherited_resources'
[myself]:myapp(upgrade/rails-3.2)$ cat Gemfile.lock | grep inherited
inherited_resources (1.4.1)
inherited_resources
And the bundle show inherited_resources:
/Users/myself/.rvm/gems/ruby-1.9.3-p448#production/gems/inherited_resources-1.4.1
Why on earth would these not be available?
I am using Rails 3.2.13 and get the following message:
ArgumentError (A secret is required to generate an integrity hash for cookie session data. Use config.secret_token = "some secret phrase of at least 30 characters"in config/initializers/secret_token.rb):
I've found reference to this in this pull request and a discussion here.
I am using Devise (hence, Warden) and the info in the discussion is relevant. I'd prefer not to hack on the railtie gem -- what is the recommended action here?
Thanks
Stack trace:
Connecting to database specified by database.yml
Started GET "/" for 76.176.151.243 at 2013-07-20 12:34:25 -0700
ArgumentError (A secret is required to generate an integrity hash for cookie session data. Use config.secret_token = "some secret phrase of at least 30 characters"in config/initializers/secret_token.rb):
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:319:in `ensure_secret_secure'
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:284:in `initialize'
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:231:in `new'
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:231:in `signed'
actionpack (3.2.13) lib/action_dispatch/middleware/session/cookie_store.rb:50:in `block in unpacked_cookie_data'
actionpack (3.2.13) lib/action_dispatch/middleware/session/abstract_store.rb:57:in `stale_session_check!'
actionpack (3.2.13) lib/action_dispatch/middleware/session/cookie_store.rb:48:in `unpacked_cookie_data'
rack (1.4.5) lib/rack/session/cookie.rb:107:in `extract_session_id'
actionpack (3.2.13) lib/action_dispatch/middleware/session/abstract_store.rb:53:in `block in extract_session_id'
actionpack (3.2.13) lib/action_dispatch/middleware/session/abstract_store.rb:57:in `stale_session_check!'
actionpack (3.2.13) lib/action_dispatch/middleware/session/abstract_store.rb:53:in `extract_session_id'
rack (1.4.5) lib/rack/session/abstract/id.rb:43:in `load_session_id!'
rack (1.4.5) lib/rack/session/abstract/id.rb:32:in `[]'
rack (1.4.5) lib/rack/session/abstract/id.rb:267:in `current_session_id'
rack (1.4.5) lib/rack/session/abstract/id.rb:273:in `session_exists?'
rack (1.4.5) lib/rack/session/abstract/id.rb:107:in `exists?'
rack (1.4.5) lib/rack/session/abstract/id.rb:127:in `load_for_read!'
rack (1.4.5) lib/rack/session/abstract/id.rb:64:in `has_key?'
actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:258:in `ensure in call'
actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:259:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `_run__1076913542946280817__call__830910935667466127__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
rack (1.4.5) lib/rack/sendfile.rb:102:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
rack-cache (1.2) lib/rack/cache/context.rb:136:in `forward'
rack-cache (1.2) lib/rack/cache/context.rb:245:in `fetch'
rack-cache (1.2) lib/rack/cache/context.rb:185:in `lookup'
rack-cache (1.2) lib/rack/cache/context.rb:66:in `call!'
rack-cache (1.2) lib/rack/cache/context.rb:51:in `call'
railties (3.2.13) lib/rails/engine.rb:479:in `call'
railties (3.2.13) lib/rails/application.rb:223:in `call'
railties (3.2.13) lib/rails/railtie/configurable.rb:30:in `method_missing'
passenger (4.0.8) lib/phusion_passenger/rack/thread_handler_extension.rb:77:in `process_request'
passenger (4.0.8) lib/phusion_passenger/request_handler/thread_handler.rb:140:in `accept_and_process_next_request'
passenger (4.0.8) lib/phusion_passenger/request_handler/thread_handler.rb:108:in `main_loop'
passenger (4.0.8) lib/phusion_passenger/request_handler.rb:441:in `block (3 levels) in start_threads'
In my config/initializers/secret_token.rb I have following
# Be sure to restart your server when you modify this file.
# Your secret key for verifying the integrity of signed cookies.
# If you change this key, all old signed cookies will become invalid!
# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
Friendflat::Application.config.secret_token = 'asdf...'
You should add the same, setting value to some long random hex string.
I'm working on a Rails 3.2.11 application (MRI 1.9.3).
PArt of it is a webservice that receives POST requests with JSON serialized bodies.
Everything works, but I want it to be resilient to bad formatted requests, e.g. invalid JSON.
Right now, if it receives a JSON with – let's say – a missing comma, it will return a 500 error with an HTML response containing the default rails error view (plus the backtrace when in dev).
I want to customize it to return a JSON or XML response with info about the error.
The error is MultiJson::DecodeError and I know I can trap exceptions with rescue_from in the ApplicationController... but it doesn't seem to work.
It's like if the error happened outside of the normal request flow.
This is my code (once I get it working I'll expand the error message with more data):
class ApplicationController < ActionController::Base
#protect_from_forgery
rescue_from MultiJson::DecodeError do |exception|
#response = { :error => "the request body was not acceptable" }
respond_to do |format|
format.html { redirect_to :root, notice: "invalid params" }
format.xml { render :xml => #response, status: 400 }
format.json { render :json => #response, status: 400 }
end
end
end
And this is the backtrace:
MultiJson::DecodeError (795: unexpected token at '{
"json_with_missing_comma" : {
"foo" : "qqqqqqqqqqqqqqq"
"bar" : "aaaaaaaaaaaaaa"
}}'):
json (1.7.6) lib/json/common.rb:155:in `parse'
json (1.7.6) lib/json/common.rb:155:in `parse'
multi_json (1.5.0) lib/multi_json/adapters/json_common.rb:7:in `load'
multi_json (1.5.0) lib/multi_json.rb:96:in `load'
activesupport (3.2.11) lib/active_support/json/decoding.rb:15:in `decode'
actionpack (3.2.11) lib/action_dispatch/middleware/params_parser.rb:47:in `parse_formatted_parameters'
actionpack (3.2.11) lib/action_dispatch/middleware/params_parser.rb:17:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.4) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.4) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.11) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `_run__1562301902235545482__call__1964551201027599208__callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.11) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.11) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.4) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.4) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.11) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.4) lib/rack/lock.rb:15:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.11) lib/rails/engine.rb:479:in `call'
railties (3.2.11) lib/rails/application.rb:223:in `call'
rack (1.4.4) lib/rack/content_length.rb:14:in `call'
railties (3.2.11) lib/rails/rack/log_tailer.rb:17:in `call'
thin (1.5.0) lib/thin/connection.rb:81:in `block in pre_process'
thin (1.5.0) lib/thin/connection.rb:79:in `catch'
thin (1.5.0) lib/thin/connection.rb:79:in `pre_process'
thin (1.5.0) lib/thin/connection.rb:54:in `process'
thin (1.5.0) lib/thin/connection.rb:39:in `receive_data'
eventmachine (1.0.0) lib/eventmachine.rb:187:in `run_machine'
eventmachine (1.0.0) lib/eventmachine.rb:187:in `run'
thin (1.5.0) lib/thin/backends/base.rb:63:in `start'
thin (1.5.0) lib/thin/server.rb:159:in `start'
rack (1.4.4) lib/rack/handler/thin.rb:13:in `run'
rack (1.4.4) lib/rack/server.rb:268:in `start'
railties (3.2.11) lib/rails/commands/server.rb:70:in `start'
railties (3.2.11) lib/rails/commands.rb:55:in `block in <top (required)>'
railties (3.2.11) lib/rails/commands.rb:50:in `tap'
railties (3.2.11) lib/rails/commands.rb:50:in `<top (required)>'
script/rails:6:in `require'
script/rails:6:in `<main>'
The gem: https://github.com/kares/request_exception_handler along with below lines works for me.
In Application controller:
"
rescue_from 'REXML::ParseException' do |exception|
render :text => "Bad Request: XML parse exception", :status => 422
end
rescue_from 'MultiJson::DecodeError' do |exception|
render :text => "Bad Request: JSON parse exception", :status => 422
end
"
I'm pretty new in Rails and on stackoverflow so I'll try to be explicite.
I was asking to change someone project, so I get the sources and try to make it works on
my own work computer (that's on Windows ...) to test it on my local browser (cf : rails s). The project is suppose to be working because it's already in production, so I assume that the problem come from my own work environment.
After installing all the needed gems, run bundle install and launch database serveur (the project is using mongodb), I run rails server. The command actually works but as soon as I try to connect on the application with any browser on the localhost adress (http://localhost:3000/) I get an error and I reaaly don't understand the cause...
This is the log I get :
Started GET "/" for 127.0.0.1 at 2012-12-04 14:51:30 +0100
Processing by HomeController#index as HTML
** [Localeapp] 1354629090-- Handling translation updates
** [Localeapp] 1354629090 - polling
** [Localeapp] API CALL: get https://api.localeapp.com/v1/projects/hhMxHgKAzj9p1dFuS0GnTTnemJ5F9dXLDuCa1KacQSymUcmTFk/translations.yml?updated_at=1354612995
** [Localeapp] ATTEMPT 1
Completed 500 Internal Server Error in 1192ms
RestClient::NotAcceptable (406 Not Acceptable):
rest-client (1.6.7) lib/restclient/abstract_response.rb:48:in `return!'
rest-client (1.6.7) lib/restclient/request.rb:230:in `process_result'
rest-client (1.6.7) lib/restclient/request.rb:178:in `block in transmit'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/net/http.rb:745:in `start'
rest-client (1.6.7) lib/restclient/request.rb:172:in `transmit'
rest-client (1.6.7) lib/restclient/request.rb:64:in `execute'
rest-client (1.6.7) lib/restclient/request.rb:33:in `execute'
localeapp (0.5.2) lib/localeapp/api_caller.rb:66:in `make_call'
localeapp (0.5.2) lib/localeapp/api_caller.rb:28:in `call'
localeapp (0.5.2) lib/localeapp/api_call.rb:6:in `api_call'
localeapp (0.5.2) lib/localeapp/poller.rb:43:in `poll!'
localeapp (0.5.2) lib/localeapp/rails/controller.rb:14:in `handle_translation_updates'
activesupport (3.2.3) lib/active_support/callbacks.rb:429:in `_run__744959596__process_action__507377612__callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.3) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.3) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.3) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.3) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.3) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.3) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.3) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.3) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.3) lib/action_controller/metal/params_wrapper.rb:205:in `process_action'
actionpack (3.2.3) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.3) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.3) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.3) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.3) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.3) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.3) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.3) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.3) lib/action_dispatch/routing/route_set.rb:600:in `call'
omniauth (1.1.1) lib/omniauth/strategy.rb:177:in `call!'
omniauth (1.1.1) lib/omniauth/strategy.rb:157:in `call'
omniauth (1.1.1) lib/omniauth/builder.rb:48:in `call'
mongoid (2.5.0) lib/rack/mongoid/middleware/identity_map.rb:33:in `block in call'
mongoid (2.5.0) lib/mongoid.rb:133:in `unit_of_work'
mongoid (2.5.0) lib/rack/mongoid/middleware/identity_map.rb:33:in `call'
sass (3.2.1) lib/sass/plugin/rack.rb:54:in `call'
warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
warden (1.2.1) lib/warden/manager.rb:34:in `catch'
warden (1.2.1) lib/warden/manager.rb:34:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/cookies.rb:338:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `_run__286698992__call__202403334__callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.3) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.3) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.3) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.3) lib/rails/rack/logger.rb:16:in `call'
quiet_assets (1.0.1) lib/quiet_assets.rb:20:in `call_with_quiet_assets'
actionpack (3.2.3) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/static.rb:62:in `call'
airbrake (3.1.4) lib/airbrake/rack.rb:41:in `call'
airbrake (3.1.4) lib/airbrake/user_informer.rb:12:in `call'
railties (3.2.3) lib/rails/engine.rb:479:in `call'
railties (3.2.3) lib/rails/application.rb:220:in `call'
railties (3.2.3) lib/rails/railtie/configurable.rb:30:in `method_missing'
rack (1.4.1) lib/rack/deflater.rb:13:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.3) lib/rails/rack/log_tailer.rb:14:in `call'
thin (1.4.1) lib/thin/connection.rb:80:in `block in pre_process'
thin (1.4.1) lib/thin/connection.rb:78:in `catch'
thin (1.4.1) lib/thin/connection.rb:78:in `pre_process'
thin (1.4.1) lib/thin/connection.rb:53:in `process'
thin (1.4.1) lib/thin/connection.rb:38:in `receive_data'
eventmachine-1.0.0-x86 (mingw32) lib/eventmachine.rb:187:in `run_machine'
eventmachine-1.0.0-x86 (mingw32) lib/eventmachine.rb:187:in `run'
thin (1.4.1) lib/thin/backends/base.rb:63:in `start'
thin (1.4.1) lib/thin/server.rb:159:in `start'
rack (1.4.1) lib/rack/handler/thin.rb:13:in `run'
rack (1.4.1) lib/rack/server.rb:265:in `start'
railties (3.2.3) lib/rails/commands/server.rb:70:in `start'
railties (3.2.3) lib/rails/commands.rb:55:in `block in <top (required)>'
railties (3.2.3) lib/rails/commands.rb:50:in `tap'
railties (3.2.3) lib/rails/commands.rb:50:in `<top (required)>'
script/rails:6:in `require'
script/rails:6:in `<main>'
Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (4.0ms)
Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.0ms)
Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (56.0ms)
And this is the controller that's mentionned :
class HomeController < ApplicationController
def index
#presenter = Home::IndexPresenter.new
#article_presenter = Articles::IndexPresenter.new(Article.with_state(:published).last)
end
end
If someone could help me it would be very kind. Ask if I forgot to mention something.
Thanks you.
The root problem is that the URL being requested by rest_client is returning the HTTP 406 code, which you can see by running curl, e.g.
curl --include https://api.localeapp.com/v1/projects/hhMxHgKAzj9p1dFuS0GnTTnemJ5F9dXLDuCa1KacQSymUcmTFk/translations.yml?updated_at=1354612995
(I just noticed that you are on a windows environment -- if you don't have access to curl there are several tools you can install that will allow you to make requests)
Then, within your controller, there doesn't appear to be any check to see if the response from the RestClient call was successful (2xx status) or not. Because RestClient raises an exception, you get a 500 error in your application.
So, if you're not sure why the URL is not working, then you'll need to check the API provided by localeapp.com
But given that the API is an external service, you may also want to add some error handling within your software. There are several things out there known as RestClient, but if it's by archiloque, the documentation provides a good example of "result handling" here.