Custrom 404 error, routing error stops the app - ruby-on-rails

In ApplicationController I added:
class ApplicationController < ActionController::Base
# This code never reached when routing error occurs
unless Rails.application.config.consider_all_requests_local
rescue_from ActionController::RoutingError, with: :render_404
rescue_from ActionController::UnknownController, with: :render_404
rescue_from ActionController::UnknownAction, with: :render_404
rescue_from ActiveRecord::RecordNotFound, with: :render_404
end
As I am trying to catch exceptions for not found pages, I've noticed that when routing error triggered, the code above never reached, as a result, render_404 never called
Rails.application.config.consider_all_requests_local is false, Any idea ?
Here is what I get:
ActionController::RoutingError (No route matches [GET] "/not_there_route"):
actionpack (3.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (3.2.5) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.5) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.5) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.5) 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.5) 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.5) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.5) lib/rails/engine.rb:479:in `call'
railties (3.2.5) lib/rails/application.rb:220:in `call'
railties (3.2.5) 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.5) lib/rails/rack/log_tailer.rb:17:in `call'
thin (1.3.1) lib/thin/connection.rb:80:in `block in pre_process'
thin (1.3.1) lib/thin/connection.rb:78:in `catch'
thin (1.3.1) lib/thin/connection.rb:78:in `pre_process'
thin (1.3.1) lib/thin/connection.rb:53:in `process'
thin (1.3.1) lib/thin/connection.rb:38:in `receive_data'
eventmachine (0.12.10) lib/eventmachine.rb:256:in `run_machine'
eventmachine (0.12.10) lib/eventmachine.rb:256:in `run'
thin (1.3.1) lib/thin/backends/base.rb:61:in `start'
thin (1.3.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.5) lib/rails/commands/server.rb:70:in `start'
railties (3.2.5) lib/rails/commands.rb:55:in `block in <top (required)>'
railties (3.2.5) lib/rails/commands.rb:50:in `tap'
railties (3.2.5) lib/rails/commands.rb:50:in `<top (required)>'

You should add catch all route, something like this:
match "*path", :to => "application#routing_error"
More detailed here http://www.bdunagan.com/2012/04/27/rescue_from-routingerror-in-rails-3/

Related

Issue with Rails panel

I am using the chrome extension Rails Panel in my Rails application. But it's not working properly.
Gemfile
group :development do
gem 'therubyracer', :platforms => :ruby
gem 'better_errors'
gem 'binding_of_caller'
gem 'meta_request'
end
Development log:
Errno::ENOENT - No such file or directory - /home/rahulv/sprint/S33/tmp/data/meta_request/d927421bce3c90bc465c9ceb894813ca.json:
meta_request (0.3.4) lib/meta_request/storage.rb:16:in `read'
meta_request (0.3.4) lib/meta_request/middlewares/meta_request_handler.rb:20:in `events_json'
meta_request (0.3.4) lib/meta_request/middlewares/meta_request_handler.rb:11:in `call'
pdfkit (0.5.4) lib/pdfkit/middleware.rb:16: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__572592932466143201__call__1542018980761186849__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'
better_errors (2.1.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (2.1.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (2.1.0) lib/better_errors/middleware.rb:57:in `call'
rack-contrib (1.2.0) lib/rack/contrib/response_headers.rb:17:in `call'
meta_request (0.3.4) lib/meta_request/middlewares/headers.rb:16: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/log_tailer.rb:17:in `call'
thin (1.5.1) lib/thin/connection.rb:81:in `block in pre_process'
thin (1.5.1) lib/thin/connection.rb:79:in `pre_process'
thin (1.5.1) lib/thin/connection.rb:54:in `process'
thin (1.5.1) lib/thin/connection.rb:39:in `receive_data'
eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
thin (1.5.1) lib/thin/backends/base.rb:63:in `start'
thin (1.5.1) lib/thin/server.rb:159:in `start'
rack (1.4.5) lib/rack/handler/thin.rb:13:in `run'
rack (1.4.5) lib/rack/server.rb:268:in `start'
railties (3.2.16) lib/rails/commands/server.rb:70:in `start'
railties (3.2.16) lib/rails/commands.rb:55:in `block in <top (required)>'
railties (3.2.16) lib/rails/commands.rb:50:in `<top (required)>'
script/rails:6:in `<main>'
What seems to be the issue?
I have solved the issue by following these steps:
Remove the meta_request folder inside/tmp/data/
Restart the server using rails s
Now everything works fine :)
As RailsPanel is a great tool, keep using it with the meta_request gem creating a Rails initializer in the file config/initializers/meta_request.rb with the below code:
if defined?(MetaRequest) && MetaRequest::Event.private_instance_methods(false).exclude?(:not_encodable?)
MetaRequest::Event.class_eval do
private
def json_encodable(payload)
return {} unless payload.is_a?(Hash)
transform_hash(payload, deep: true) do |hash, key, value|
if value.class.to_s == 'ActionDispatch::Http::Headers'
value = value.to_h.select { |k, _| k.upcase == k }
elsif not_encodable?(value)
value = self.class.const_get(:NOT_JSON_ENCODABLE)
end
begin
value.to_json(methods: [:duration])
new_value = value
rescue StandardError
new_value = self.class.const_get(:NOT_JSON_ENCODABLE)
end
hash[key] = new_value
end.with_indifferent_access
end
def not_encodable?(value)
(defined?(ActiveRecord) && value.is_a?(ActiveRecord::ConnectionAdapters::AbstractAdapter)) ||
(defined?(ActionDispatch) &&
(value.is_a?(ActionDispatch::Request) || value.is_a?(ActionDispatch::Response)))
end
end
end
Reference: https://github.com/dejan/rails_panel/pull/177#issuecomment-797378347

ActiveRecord::ConnectionNotEstablished error in Rails set-up on Ubuntu

I am trying to debug an application using Ruby on Rails(3.0.8) setup in Ubuntu using RubyMine.My application is without a database.
But when i start the server i am getting an error:-
ActiveRecord::ConnectionNotEstablished
This is the full trace of the error:-
ctiverecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:404:in `retrieve_connection'
activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_specification.rb:170:in `retrieve_connection'
activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_specification.rb:144:in `connection'
activerecord (3.2.8) lib/active_record/query_cache.rb:67:in `rescue in call'
activerecord (3.2.8) lib/active_record/query_cache.rb:61:in `call'
activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `_run__515326265__call__889079158__callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.8) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.8) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.8) 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.8) 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.8) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.8) lib/rails/engine.rb:479:in `call'
railties (3.2.8) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.8) lib/rails/rack/log_tailer.rb:17:in `call'
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'
thin (1.5.1) lib/thin/connection.rb:54:in `process'
thin (1.5.1) lib/thin/connection.rb:39:in `receive_data'
eventmachine (1.0.3) lib/eventmachine.rb:1484:in `event_callback'
eventmachine (1.0.3) lib/em/pure_ruby.rb:544:in `block in eventable_read'
eventmachine (1.0.3) lib/em/pure_ruby.rb:542:in `times'
eventmachine (1.0.3) lib/em/pure_ruby.rb:542:in `eventable_read'
/usr/lib/ruby/1.9.1/forwardable.rb:201:in `eventable_read'
eventmachine (1.0.3) lib/em/pure_ruby.rb:369:in `block in crank_selectables'
eventmachine (1.0.3) lib/em/pure_ruby.rb:369:in `each'
eventmachine (1.0.3) lib/em/pure_ruby.rb:369:in `crank_selectables'
eventmachine (1.0.3) lib/em/pure_ruby.rb:324:in `block in run'
eventmachine (1.0.3) lib/em/pure_ruby.rb:318:in `loop'
eventmachine (1.0.3) lib/em/pure_ruby.rb:318:in `run'
eventmachine (1.0.3) lib/em/pure_ruby.rb:62:in `run_machine'
eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
thin (1.5.1) lib/thin/backends/base.rb:63:in `start'
thin (1.5.1) lib/thin/server.rb:159:in `start'
rack (1.4.5) lib/rack/handler/thin.rb:13:in `run'
rack (1.4.5) lib/rack/server.rb:268:in `start'
railties (3.2.8) lib/rails/commands/server.rb:70:in `start'
railties (3.2.8) lib/rails/commands.rb:55:in `block in <top (required)>'
railties (3.2.8) lib/rails/commands.rb:50:in `tap'
railties (3.2.8) lib/rails/commands.rb:50:in `<top (required)>'
script/rails:6:in `require'
script/rails:6:in `<main>'
I have not installed MySql Gem in my Gemfile
When I start the same application on another machine(My Senior's) it seems to work.The code seems to be same.I tried various sites for the solution but it does not seem to help..
Can someone please help me with this
I am using rails 3.2.12
Comment require 'rails/all' in /config/application.rb
and add these in the same file
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
also comment
#config.active_record.whitelist_attributes = true
Now in config/environments/development.rb
comment these lines
#config.active_record.mass_assignment_sanitizer = :strict
#config.active_record.auto_explain_threshold_in_seconds = 0.5
Check my commit on github when I got same error.

Rails 3.2, handle corrupted request bodies (MultiJson::DecodeError)

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
"

How is config.assets.debug causing my Rails application to crash?

I've included the arbor-rails gem in my Rails 3.2.8 application and it works fine in development mode, but in production I get the following error:
Started GET "/nullarbor.js" for 127.0.0.1 at 2012-09-20 15:35:23 -0700
ActionController::RoutingError (No route matches [GET] "/nullarbor.js"):
actionpack (3.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.8) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.8) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.8) 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.8) 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.8) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.8) lib/rails/engine.rb:479:in `call'
railties (3.2.8) lib/rails/application.rb:223:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.8) lib/rails/rack/log_tailer.rb:17: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) lib/eventmachine.rb:187:in `run_machine'
eventmachine (1.0.0) 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.8) lib/rails/commands/server.rb:70:in `start'
railties (3.2.8) lib/rails/commands.rb:55:in `block in <top (required)>'
railties (3.2.8) lib/rails/commands.rb:50:in `tap'
railties (3.2.8) lib/rails/commands.rb:50:in `<top (required)>'
script/rails:6:in `require'
script/rails:6:in `<main>'
I've gone through the settings in environments/ and discovered that when I set the following in development.rb:
config.assets.debug = false
I get the error in dev mode, too. And when I set it to true in production it works properly. What could be causing this?
Update: It seems to actually be something to do with the arbor_path function in arbor.js.
arbor.js uses a web worker which means that you need to provide the Worker creation with the appropriate link to where the arbor.js script will be.
What you could do to solve this problem is to provide the absolute path to your arbor.js file instead of letting the code use the arbor_path function that can be messed up by your caching logic or the way you serve js files.
In other words, try to replace
i=new Worker(arbor_path()+"arbor.js");
with
i=new Worker("/final/path/to/arbor.js");
in arbor.js
As #Asimov4 stated, the problem is produced on the following call:
new Worker(arbor_path()+"arbor.js");
Nonetheless, a more elegant solution, compatible with the precompilation of assets, is to change it to:
new Worker("<%= asset_path('arbor.js') %>");
Note: Remember to change the .js file to .js.erb!

Callback denied with OmniAuth

When I initiate the logging in process using OmniAuth in a Rails 3 app, if I cancel on the provider's page, I get sent back to an URL that looks something like:
http://example.com/auth/twitter/callback?denied=aUho....
and my application throws a 500, that I can see it's a OAuth::Unauthorized: 401 Unauthorized, without ever touching any of my controller actions.
The stack trace is when I reproduce it in my computer is:
oauth (0.4.6) lib/oauth/consumer.rb:216:in `token_request'
oauth (0.4.6) lib/oauth/consumer.rb:136:in `get_request_token'
omniauth-oauth (1.0.1) lib/omniauth/strategies/oauth.rb:29:in `request_phase'
omniauth-twitter (0.0.11) lib/omniauth/strategies/twitter.rb:50:in `request_phase'
omniauth (1.1.0) lib/omniauth/strategy.rb:207:in `request_call'
omniauth (1.1.0) lib/omniauth/strategy.rb:174:in `call!'
omniauth (1.1.0) lib/omniauth/strategy.rb:157:in `call'
omniauth (1.1.0) lib/omniauth/strategy.rb:177:in `call!'
omniauth (1.1.0) lib/omniauth/strategy.rb:157:in `call'
omniauth (1.1.0) lib/omniauth/builder.rb:48:in `call'
sass (3.1.19) lib/sass/plugin/rack.rb:54:in `call'
warden (1.1.1) lib/warden/manager.rb:35:in `block in call'
warden (1.1.1) lib/warden/manager.rb:34:in `catch'
warden (1.1.1) lib/warden/manager.rb:34:in `call'
actionpack (3.2.6) 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.6) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.6) 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.6) lib/action_dispatch/middleware/cookies.rb:338:in `call'
activerecord (3.2.6) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `_run__60653626266012267__call__4496837804684830799__callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.6) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.6) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.6) 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.6) 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.6) 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.6) lib/action_dispatch/middleware/static.rb:62:in `call'
airbrake (3.1.1) lib/airbrake/rack.rb:30:in `call'
airbrake (3.1.1) lib/airbrake/user_informer.rb:12:in `call'
railties (3.2.6) lib/rails/engine.rb:479:in `call'
railties (3.2.6) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.6) lib/rails/rack/log_tailer.rb:17:in `call'
thin (1.3.1) lib/thin/connection.rb:80:in `block in pre_process'
thin (1.3.1) lib/thin/connection.rb:78:in `catch'
thin (1.3.1) lib/thin/connection.rb:78:in `pre_process'
thin (1.3.1) lib/thin/connection.rb:53:in `process'
thin (1.3.1) lib/thin/connection.rb:38:in `receive_data'
eventmachine (0.12.10) lib/eventmachine.rb:256:in `run_machine'
eventmachine (0.12.10) lib/eventmachine.rb:256:in `run'
thin (1.3.1) lib/thin/backends/base.rb:61:in `start'
thin (1.3.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.6) lib/rails/commands/server.rb:70:in `start'
railties (3.2.6) lib/rails/commands.rb:55:in `block in <top (required)>'
railties (3.2.6) lib/rails/commands.rb:50:in `tap'
railties (3.2.6) lib/rails/commands.rb:50:in `<top (required)>'
script/rails:6:in `require'
script/rails:6:in `<top (required)>'
-e:1:in `load'
-e:1:in `<main>'
If I go on with log in (without clicking cancel at Twitter's page), then everything works correctly as expected. Same for Facebook, I can log in just fine, but if I press 'cancel' at Facebook's page, I get redirected back and I throw a 500.
Is there a way to handle this better? I'd like to be able to show a nice page.
When there's a failure, most of the times depending on the type of failure, OmniAuth will call OmniAuth.config.on_failure which by default is set to OmniAuth::FailureEndpoint. By default it is supposed to raise an exception in development mode and redirect otherwise:
def call
raise_out! if ENV['RACK_ENV'].to_s == 'development'
redirect_to_failure
end
The default endpoint, according to the docs, is /auth/failure.
If that is not working as expected for you, then you can force the redirection to always happen, even on dev mode, by adding this to your initializer:
class SafeFailureEndpoint < OmniAuth::FailureEndpoint
def call
redirect_to_failure
end
end
OmniAuth.config.on_failure = SafeFailureEndpoint
A more concise solution from https://github.com/intridea/omniauth/issues/616
# Added to config/initializers/omniauth.rb
OmniAuth.config.on_failure = Proc.new { |env|
OmniAuth::FailureEndpoint.new(env).redirect_to_failure
}
I had same issue, (facebook cancel clicked)
after adding
# Added to config/initializers/omniauth.rb
OmniAuth.config.on_failure = Proc.new { |env|
OmniAuth::FailureEndpoint.new(env).redirect_to_failure
}
implemented auth callback route as
in config/routes.rb
match '/auth/failure' => 'sessions#failure'
session controller will look like
class SessionsController < ApplicationController
def failure
redirect_to #route that you want redirect
end
end
I had the same issue, and with Pramod's solution I was getting an "Unknown action" error, but worked after I changed the callback route to
config/routes.rb
devise_scope :user do
get '/users/auth/failure' => 'sessions#failure'
end
Rails 3.2.14
Devise 3.1.1
Omniauth 1.1.4
For those using omniauth_facebook and something like Users::OmniauthCallbacksController use:
config/routes.rb
devise_scope :user do
get '/omniauth/failure' => 'users/omniauth_callbacks#failure'
end
app/controllers/users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
...
end
def failure
redirect_to root_path
end
end
If you are using the FB popup
app/controllers/users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
...
end
def failure
if request.env['HTTP_REFERER'] == "https://www.facebook.com/"
render 'failure', :layout => false
else
redirect_to root_path
end
end
end
app/views/users/omniauth_callbacks/failure.html.haml
:javascript
window.close();

Resources