Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering custom error views doesn't work for me #11

Closed
jmuheim opened this issue Mar 6, 2014 · 11 comments
Closed

Rendering custom error views doesn't work for me #11

jmuheim opened this issue Mar 6, 2014 · 11 comments

Comments

@jmuheim
Copy link
Contributor

jmuheim commented Mar 6, 2014

I have done like you describe in your README:

# config/initializers/gaffe.rb
Gaffe.configure do |config|
  config.errors_controller = ErrorsController
end
Gaffe.enable!
# config/development.rb
config.consider_all_requests_local       = false

I also want to rescue from CanCan:

# config/application.rb
config.action_dispatch.rescue_responses.merge! 'CanCan::AccessDenied' => :forbidden

But this is what's printed out at the screen, regardless what error I trigger using the browser:

500 Internal Server Error
If you are the administrator of this website, then please read this web application's log file and/or the web server's log file to find out what went wrong.

Log:

Started GET "/users/1/edit" for 127.0.0.1 at 2014-03-06 16:55:27 +0100
Processing by UsersController#edit as HTML
  Parameters: {"id"=>"1"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."guest" = 'f' AND "users"."id" = 2 ORDER BY "users"."id" ASC LIMIT 1
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."guest" = 'f' AND "users"."id" = ? LIMIT 1  [["id", "1"]]
   (0.1ms)  SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = ? AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))  [["user_id", 2]]
Completed 403 Forbidden in 6ms

CanCan::AccessDenied (You are not authorized to access this page.):
  cancancan (1.7.0) lib/cancan/ability.rb:208:in `authorize!'
  cancancan (1.7.0) lib/cancan/controller_additions.rb:338:in `authorize!'
  cancancan (1.7.0) lib/cancan/controller_resource.rb:41:in `authorize_resource'
  cancancan (1.7.0) lib/cancan/controller_resource.rb:26:in `load_and_authorize_resource'
  cancancan (1.7.0) lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
  activesupport (4.0.3) lib/active_support/callbacks.rb:427:in `_run__296866192647048093__process_action__callbacks'
  activesupport (4.0.3) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.3) lib/abstract_controller/callbacks.rb:17:in `process_action'
  actionpack (4.0.3) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.0.3) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.0.3) lib/active_support/notifications.rb:159:in `block in instrument'
  activesupport (4.0.3) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.0.3) lib/active_support/notifications.rb:159:in `instrument'
  actionpack (4.0.3) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.0.3) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
  activerecord (4.0.3) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.0.3) lib/abstract_controller/base.rb:136:in `process'
  actionpack (4.0.3) lib/abstract_controller/rendering.rb:44:in `process'
  actionpack (4.0.3) lib/action_controller/metal.rb:195:in `dispatch'
  actionpack (4.0.3) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.0.3) lib/action_controller/metal.rb:231:in `block in action'
  actionpack (4.0.3) lib/action_dispatch/routing/route_set.rb:80:in `call'
  actionpack (4.0.3) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
  actionpack (4.0.3) lib/action_dispatch/routing/route_set.rb:48:in `call'
  actionpack (4.0.3) lib/action_dispatch/journey/router.rb:71:in `block in call'
  actionpack (4.0.3) lib/action_dispatch/journey/router.rb:59:in `each'
  actionpack (4.0.3) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.0.3) lib/action_dispatch/routing/route_set.rb:680:in `call'
  xray-rails (0.1.12) lib/xray/middleware.rb:37:in `call'
  warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.3) lib/warden/manager.rb:34:in `catch'
  warden (1.2.3) lib/warden/manager.rb:34:in `call'
  rack (1.5.2) lib/rack/etag.rb:23:in `call'
  rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
  rack (1.5.2) lib/rack/head.rb:11:in `call'
  actionpack (4.0.3) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.0.3) lib/action_dispatch/middleware/flash.rb:241:in `call'
  rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.0.3) lib/action_dispatch/middleware/cookies.rb:486:in `call'
  activerecord (4.0.3) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.0.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
  activerecord (4.0.3) lib/active_record/migration.rb:369:in `call'
  actionpack (4.0.3) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.0.3) lib/active_support/callbacks.rb:373:in `_run__3876362860285233192__call__callbacks'
  activesupport (4.0.3) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.3) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.0.3) lib/action_dispatch/middleware/reloader.rb:64:in `call'
  actionpack (4.0.3) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  actionpack (4.0.3) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.0.3) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.3) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.3) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.0.3) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.3) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.3) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.3) lib/rails/rack/logger.rb:20:in `call'
  quiet_assets (1.0.2) lib/quiet_assets.rb:18:in `call_with_quiet_assets'
  actionpack (4.0.3) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.3) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack-livereload (0.3.15) lib/rack/livereload.rb:23:in `_call'
  rack-livereload (0.3.15) lib/rack/livereload.rb:14:in `call'
  actionpack (4.0.3) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.0.3) lib/rails/engine.rb:511:in `call'
  railties (4.0.3) lib/rails/application.rb:97:in `call'
  /Users/josh/Library/Application Support/Pow/Versions/0.4.1/node_modules/nack/lib/nack/server.rb:145:in `handle'
  /Users/josh/Library/Application Support/Pow/Versions/0.4.1/node_modules/nack/lib/nack/server.rb:99:in `rescue in block (2 levels) in start'
  /Users/josh/Library/Application Support/Pow/Versions/0.4.1/node_modules/nack/lib/nack/server.rb:96:in `block (2 levels) in start'
  /Users/josh/Library/Application Support/Pow/Versions/0.4.1/node_modules/nack/lib/nack/server.rb:86:in `each'
  /Users/josh/Library/Application Support/Pow/Versions/0.4.1/node_modules/nack/lib/nack/server.rb:86:in `block in start'
  /Users/josh/Library/Application Support/Pow/Versions/0.4.1/node_modules/nack/lib/nack/server.rb:66:in `loop'
  /Users/josh/Library/Application Support/Pow/Versions/0.4.1/node_modules/nack/lib/nack/server.rb:66:in `start'
  /Users/josh/Library/Application Support/Pow/Versions/0.4.1/node_modules/nack/lib/nack/server.rb:13:in `run'
  /Users/josh/Library/Application Support/Pow/Versions/0.4.1/node_modules/nack/bin/nack_worker:4:in `<main>'


Processing by ErrorsController#show as HTML
  Parameters: {"id"=>"1"}
Completed 500 Internal Server Error in 3ms
@simonprev
Copy link
Member

Could you post your ErrorsController?

@jmuheim
Copy link
Contributor Author

jmuheim commented Mar 6, 2014

It's a copy&paste of your example:

class ErrorsController < ApplicationController
  include Gaffe::Errors

  def show
    # The following variables are available:
    @exception # The encountered exception (Eg. `#<ActiveRecord::NotFound …>`)
    @status_code # The status code we should return (Eg. `404`)
    @rescue_response # The "standard" name for the status code (Eg. `:not_found`)
  end
end

I figured out that commenting out the following files in the initializer...

# See https://github.com/mirego/gaffe
# Gaffe.configure do |config|
#   config.errors_controller = ErrorsController
# end
Gaffe.enable!

...it seems to work:

image

@remi
Copy link
Member

remi commented Mar 6, 2014

It looks like you’re not doing anything in the show action. You could either remove it or implement a rendering behavior. Gaffe’s default is:

render "errors/#{@rescue_response}", status: @status_code

You can also use a custom layout in your ErrorsController. By including Gaffe::Errors, it makes it using the error layout, which is something you might not want to use. You could use this and see if the error still occurs:

class ErrorsController < ApplicationController
  include Gaffe::Errors
  layout 'application'

  def show
    render text: @rescue_response.to_s
  end
end

@jmuheim
Copy link
Contributor Author

jmuheim commented Mar 7, 2014

Thank you. I now use the following:

Gaffe.configure do |config|
  config.errors_controller = ErrorsController
end
Gaffe.enable!
class ErrorsController < ApplicationController
  include Gaffe::Errors
  layout 'application'

  def show
    binding.pry
    render "errors/#{@rescue_response}", status: @status_code
  end
end

Interestingly, nothing changes! Still the default layout is presented, and the show action isn't executed (which is proved by not halting execution at binding.pry).

@jmuheim
Copy link
Contributor Author

jmuheim commented Mar 7, 2014

I restarted the server etc.

@rymohr
Copy link

rymohr commented Mar 19, 2014

I just ran into the same issue. I think it's this example from the README that's throwing people off:

class ErrorsController < ApplicationController
  include Gaffe::Errors
  skip_before_filter :ensure_current_user

  def show
    # The following variables are available:
    @exception # The encountered exception (Eg. `#<ActiveRecord::NotFound …>`)
    @status_code # The status code we should return (Eg. `404`)
    @rescue_response # The "standard" name for the status code (Eg. `:not_found`)
  end
end

@jmuheim
Copy link
Contributor Author

jmuheim commented Mar 19, 2014

Did you make it work then? I agree that the example in the README is quite misleading: people like to just copy&paste and don't want to think a lot when trying a new gem.

@remi
Copy link
Member

remi commented Mar 19, 2014

What do you suggest the example should look like? Something like this?

class ErrorsController < ApplicationController
  include Gaffe::Errors

  # Make sure anonymous users can see the page
  skip_before_filter :authenticate_user!

  # Override 'error' layout
  layout 'application'

  # Render the correct template based on the exception “standard” code.
  # Eg. For a 404 error, the `errors/not_found` template will be rendered.
  def show
    render "errors/#{@rescue_response}", status: @status_code    
  end
end

@rymohr
Copy link

rymohr commented Mar 19, 2014

Mine worked fine once I commented out the broken show method.

@remiprev I think that example is much clearer. Add one more comment about the exception being available through @exception and you're good to go.

Thanks for the library!

@remi
Copy link
Member

remi commented Mar 20, 2014

@jmuheim @rymohr I made improvements to the examples in README.md and I think they’re good now. I’m closing this issue but feel to reopen it if you still have questions!

@remi remi closed this as completed Mar 20, 2014
@jmuheim
Copy link
Contributor Author

jmuheim commented Mar 20, 2014

It's working for me now, too. 👍 Thanks for your support!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants