Code Monkey home page Code Monkey logo

gaffe's Introduction

gaffe
Gaffe makes having customized error pages in Rails applications an easy thing.
It takes advantage of a feature present in Rails 3.2 (and 4.0+, obviously) called exceptions_app.


It comes with default error pages but makes it very easy to override them (which you should do). The default error pages look like this:

Installation

Add this line to your applicationā€™s Gemfile:

gem 'gaffe'

Usage

The easiest way to use Gaffe is with an initializer:

# config/initializers/gaffe.rb
Gaffe.enable!

Custom controller

However, if you want to use your own controller:

# config/initializers/gaffe.rb
Gaffe.configure do |config|
  config.errors_controller = 'ErrorsController'
end

Gaffe.enable!

Itā€™s also possible to use a custom controller based on the URL in which the error has occured. Both absolute and relative URL supported. This is especially useful if you have an application that also serves API requests via JSON. You would probably want to serve API errors through JSON and regular errors through HTML pages.

# config/initializers/gaffe.rb
Gaffe.configure do |config|
  config.errors_controller = {
    %r[^/api/] => 'Api::ErrorsController',
    %r[^/] => 'ErrorsController',
    %r[^www.example.com] => 'HostSpecificErrorsController'
  }
end

Gaffe.enable!

The only required thing to do in your custom controller is to include the Gaffe::Errors module.

Only show will be called so you might want to override it. If you donā€™t override it, Gaffe will try to render the view "errors/#{@rescue_response}" within your application (or use its default error page if the view doesnā€™t exist).

You might also want to get rid of filters and other stuff to make sure that error pages are always accessible.

class ErrorsController < ApplicationController
  include Gaffe::Errors

  # Make sure anonymous users can see the page
  skip_before_action :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
    # Here, the `@exception` variable contains the original raised error
    render "errors/#{@rescue_response}", status: @status_code
  end
end

For example, you might want your API::ErrorsController to return a standard JSON response:

class API::ErrorsController < API::ApplicationController
  include Gaffe::Errors

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

  # Disable layout (your `API::ApplicationController` probably does this already)
  layout false

  # Render a simple JSON response containing the error ā€œstandardā€ code
  # plus the exception name and backtrace if weā€™re in development.
  def show
    output = { error: @rescue_response }
    output.merge! exception: @exception.inspect, backtrace: @exception.backtrace.first(10) if Rails.env.development? || Rails.env.test?
    render json: output, status: @status_code
  end
end

Custom views

You can (and should!) also use your own views. You just have to create a layout:

<!-- app/views/layouts/error.html.erb -->
<h1>Error!</h1>
<%= yield %>

And create a different view for each possible error rescue response (rails reference). For example, for 404 errors:

<!-- app/views/errors/not_found.html.erb -->
<p>This page does not exist.</p>

Custom exceptions

If your application is raising custom exceptions (through gems or your code) and you want to render specific views when it happens, you can map them to specific rescue responses.

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

Rails development environment

Rails prefers to render its own debug-friendly errors in the development environment, which is totally understandable. However, if you want to test Gaffeā€™s behavior in development youā€™ll have to edit the config/environments/development.rb file.

# Make Rails use `exceptions_app` in development
config.consider_all_requests_local = false

Rails test environment

You also have to configure Railsā€™ test environment so it lets Gaffe handle exceptions in request tests. Youā€™ll have to edit the config/environments/test.rb file.

# Make Rails use `exceptions_app` in tests
config.consider_all_requests_local = false

# Render exceptions instead of raising them
config.action_dispatch.show_exceptions = true

Unfortunately, controller tests (called functional tests in Rails) do not work with Gaffe, since they only test method calls in the controller class ā€” they do not go through the entire Rack stack to simulate a real HTTP request.

To test responses sent by Gaffe, you must use request tests.

Contributors

License

Gaffe is Ā© 2013-2016 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md file.

The mushroom cloud logo is based on this lovely icon by Gokce Ozan, from The Noun Project. Used under a Creative Commons BY 3.0 license.

About Mirego

Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We're a team of talented people who imagine and build beautiful Web and mobile applications. We come together to share ideas and change the world.

We also love open-source software and we try to give back to the community as much as we can.

gaffe's People

Contributors

jmuheim avatar olegantonyan avatar remi avatar simonprev avatar uzzer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gaffe's Issues

Manual Error Redirects

Hi

First of all, thank you very much for your excellent Gem!! I appreciate that this is not really an issue with your gem but more of a use case but I am hoping you might be able to give me some advice, my app has some admin only pages on it so I want to throw a 403 error if a non admin user tries to access those pages. I have seen lots of people suggesting online to do the following:

render :file => File.join(Rails.root, 'public/403.html'), :action => 'show', :status => 403, :layout => false

This is obviously manually rendering a static file which then bypasses Gaff, is there any way throw an error myself that will be handled by Gaffe and routed to my proper error pages?

Many many thanks
David

Don't work config.errors_controller in Test

@mirego Thinks very nice gem, I liked gaffe.

I set config/initializers/gaffe.rb:

Gaffe.configure do |config|
  config.errors_controller = {
    %r[^/api/] => 'Api::ErrorsController',
    %r[^/] => 'ErrorsController'
  }
end

Gaffe.enable!

I wrote request spec (RSpec) in accordance with https://github.com/mirego/gaffe#rails-test-environment .
However, responsed by Gaffe::ErrorsController.
I want to be responsed by Api::ErrorsController or ErrorsController.

I guessed that @env['REQUEST_URI'] was empty in https://github.com/mirego/gaffe/blob/master/lib/gaffe/errors_controller_resolver.rb#L33 .
Rack::Test is not set environment REQUEST_URI: rack/rack-test#26

Default error handler doesn't handle image requests

I expected 404 requests for images, pdfs, etc to be handled by default but was seeing the fallback error message instead:

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.

In the logs you'd see something like this:

F, [2014-07-10T19:26:16.391631 #12765] FATAL -- : 
ActiveRecord::RecordNotFound (ActiveRecord::RecordNotFound):
  app/models/user.rb:69:in `block (2 levels) in singleton class'
  app/controllers/users_controller.rb:9:in `show'
I, [2014-07-10T19:26:16.392072 #12765]  INFO -- : Processing by ErrorsController#show as PNG
I, [2014-07-10T19:26:16.398137 #12765]  INFO -- : Completed 500 Internal Server Error in 6ms

To fix things I ended up having to override the request format with:

before_action :override_format

def override_format
  request.format = "html" unless params[:format] == "json"
end

Just wanted to make a note of it incase anyone else runs into it.

Not testable using request specs?

I want to make sure that the errors are displayed correctly to the end user by doing some request/acceptance/feature tests.

I noticed that this somehow doesn't seem to be possible. I tried playing around with consider_all_requests_local and show_exceptions in test.rb, but didn't achieve to make gaffe work the way I want.

Is there an easy way to achieve this?

Thanks for this nice gem. It's the first time I'm dealing with rescuing errors, and this makes it really cool.

Database connections not cleared

While I don't think this is technically a bug, I wanted to document it as an issue and let users/maintainers decide what to do.

If a gaffe user uses a custom controller that requires a database connection, that database connection must be cleared manually. My suggested course of action is just to note this in the README and to add some sort of code example showing how to clear the connections. Something like:

append_after_filter :clear_connections
private
def clear_connections 
  ActiveRecord::Base.clear_active_connections! 
end

Rendering custom error views doesn't work for me

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

rspec + gaffe + show_exceptions

I try to test specific error pages responses (ex. unauthorized) with rspec, like described in this article, but error page is never rendered, I get an error raised.

Author says you should set this to force rails test environment to render error routes via exceptions_app :

# config/environments/test.rb
config.consider_all_requests_local = false
config.action_dispatch.show_exceptions = true

But my error are still raising in my specs, I am not getting the 401 response template as expected with these settings. I have look into Gaffe code, it doesn't looks like there is any specific test environment behaviour, any ideas?

I have also look into Capybara.raise_server_errors = false setting, without success.

See also : http://stackoverflow.com/questions/18457156/how-do-i-make-config-exceptions-app-work-with-rspec

Error during failsafe response: No route matches {:action=>"index"}

For some reason none of my custom error pages are not rendering. I have tested everything with blank views and still get this error. When I delete the Gaffe initializer the standard rails 404 pages are rendered properly so it has to do with Gaffe. Here is the terminal output:

Started GET "/fadsjklfdsaj" for ::1 at 2016-05-04 09:54:23 -0500
  ActiveRecord::SchemaMigration Load (0.8ms)  SELECT "schema_migrations".* FROM "schema_migrations"

ActionController::RoutingError (No route matches [GET] "/fadsjklfdsaj"):
  actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  web-console (2.2.1) lib/web_console/middleware.rb:39:in `call'
  newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
  newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  rack (1.6.0) lib/rack/methodoverride.rb:22:in `call'
  newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  rack (1.6.0) lib/rack/runtime.rb:18:in `call'
  newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  rack (1.6.0) lib/rack/lock.rb:17:in `call'
  newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
  newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  rack (1.6.0) lib/rack/sendfile.rb:113:in `call'
  newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  airbrake (4.3.3) lib/airbrake/user_informer.rb:16:in `_call'
  airbrake (4.3.3) lib/airbrake/user_informer.rb:12:in `call'
  newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  railties (4.2.0) lib/rails/engine.rb:518:in `call'
  railties (4.2.0) lib/rails/application.rb:164:in `call'
  newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  rack (1.6.0) lib/rack/content_length.rb:15:in `call'
  puma (2.14.0) lib/puma/server.rb:541:in `handle_request'
  puma (2.14.0) lib/puma/server.rb:388:in `process_client'
  puma (2.14.0) lib/puma/server.rb:270:in `block in run'
  puma (2.14.0) lib/puma/thread_pool.rb:106:in `call'
  puma (2.14.0) lib/puma/thread_pool.rb:106:in `block in spawn_thread'


Processing by ErrorsController#show as HTML
Completed 500 Internal Server Error in 9ms
Error during failsafe response: No route matches {:action=>"index"}
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/action_dispatch/journey/formatter.rb:46:in `generate'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:710:in `generate'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:741:in `generate'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:784:in `url_for'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/action_dispatch/routing/url_for.rb:156:in `url_for'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/auto-session-timeout-0.9.2/lib/auto_session_timeout.rb:14:in `block in auto_session_timeout'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:443:in `instance_exec'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:443:in `block in make_lambda'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:163:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:163:in `block in halting'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:169:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:169:in `block in halting'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in `_run_callbacks'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:81:in `run_callbacks'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/abstract_controller/callbacks.rb:19:in `process_action'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/action_controller/metal/rescue.rb:29:in `process_action'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/notifications.rb:164:in `block in instrument'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/notifications.rb:164:in `instrument'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.0/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/abstract_controller/base.rb:137:in `process'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionview-4.2.0/lib/action_view/rendering.rb:30:in `process'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/action_controller/metal.rb:195:in `dispatch'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/action_controller/metal.rb:236:in `block in action'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/gaffe-1.0.2/lib/gaffe.rb:29:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/gaffe-1.0.2/lib/gaffe.rb:29:in `block in enable!'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/action_dispatch/middleware/show_exceptions.rb:47:in `render_exception'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/action_dispatch/middleware/show_exceptions.rb:35:in `rescue in call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/newrelic_rpm-3.14.0.305/lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/railties-4.2.0/lib/rails/rack/logger.rb:38:in `call_app'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/railties-4.2.0/lib/rails/rack/logger.rb:20:in `block in call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/tagged_logging.rb:68:in `block in tagged'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/tagged_logging.rb:26:in `tagged'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/tagged_logging.rb:68:in `tagged'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/railties-4.2.0/lib/rails/rack/logger.rb:20:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/newrelic_rpm-3.14.0.305/lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/action_dispatch/middleware/request_id.rb:21:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/newrelic_rpm-3.14.0.305/lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/rack-1.6.0/lib/rack/methodoverride.rb:22:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/newrelic_rpm-3.14.0.305/lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/rack-1.6.0/lib/rack/runtime.rb:18:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/newrelic_rpm-3.14.0.305/lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/newrelic_rpm-3.14.0.305/lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/rack-1.6.0/lib/rack/lock.rb:17:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/newrelic_rpm-3.14.0.305/lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.0/lib/action_dispatch/middleware/static.rb:113:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/newrelic_rpm-3.14.0.305/lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/rack-1.6.0/lib/rack/sendfile.rb:113:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/newrelic_rpm-3.14.0.305/lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/airbrake-4.3.3/lib/airbrake/user_informer.rb:16:in `_call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/airbrake-4.3.3/lib/airbrake/user_informer.rb:12:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/newrelic_rpm-3.14.0.305/lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/railties-4.2.0/lib/rails/engine.rb:518:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/railties-4.2.0/lib/rails/application.rb:164:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/newrelic_rpm-3.14.0.305/lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/rack-1.6.0/lib/rack/content_length.rb:15:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/puma-2.14.0/lib/puma/server.rb:541:in `handle_request'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/puma-2.14.0/lib/puma/server.rb:388:in `process_client'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/puma-2.14.0/lib/puma/server.rb:270:in `block in run'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/puma-2.14.0/lib/puma/thread_pool.rb:106:in `call'
  /Users/jeffreygoodall/.rvm/gems/ruby-2.2.1/gems/puma-2.14.0/lib/puma/thread_pool.rb:106:in `block in spawn_thread'

Error during failsafe response: Pundit::AuthorizationNotPerformedError

I can't get past this Pundit error. Have you used Pundit with gaffe? The 'AuthorizationNotPerformedError' seems to be a general Pundit error. I have all error views, I have a policy method for each view. Not sure where or why this is preventing gaffe from returning the error page.

Here is the trace:

Processing by Gaffe::ErrorsController#show as HTML
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
School Load (0.9ms) SELECT "schools".* FROM "schools" WHERE "schools"."id" = $1 LIMIT 1 [["id", 1]]
Rendered errors/internal_server_error.html.erb within layouts/error (0.2ms)
Completed 406 Not Acceptable in 41ms
Error during failsafe response: Pundit::AuthorizationNotPerformedError
/Users/XXXX/.rvm/gems/ruby-2.2.0-rc1@XXXX/gems/pundit-0.3.0/lib/pundit.rb:58:in `verify_authorized'

Dealing with active record validation exceptions

A failed active record validation (ex: validates :names, presence: true) produces a 422 (Unprocessable Entity) error response which will be handled by Gaffe.

Is there a way to circumvent this so as to deal with failed validations in the original controller without having to resort to a rescue clause in every create/update method?

Thanks for this handy gem by the way!

Spec order causes failure

Randomly specs return

Failures:

  1) Gaffe ClassMethods enable! should #<RSpec::Mocks::Matchers::Receive:0x007fde1dd72d70>
     Failure/Error: expect(Gaffe::ErrorsController).to receive(:action).with(:show).and_return(action_double)
     NameError:
       uninitialized constant Gaffe::ErrorsController
     # ./spec/gaffe/gaffe_spec.rb:25:in `block (4 levels) in <top (required)>'

Finished in 0.05393 seconds (files took 0.38013 seconds to load)
13 examples, 1 failure

Failed examples:

rspec ./spec/gaffe/gaffe_spec.rb:23 # Gaffe ClassMethods enable! should #<RSpec::Mocks::Matchers::Receive:0x007fde1dd72d70>

Randomized with seed 54725

Not reproducible with same seed.

Missing pattern priority in multiple patterns

There is major issue with patterns matching logic

Current logic does not allow to set priority for controller of path that match two or more different patterns. Hash should be changed to array.

I can deal with it later...

Fallback static pages are not rendered

When my error page encounters an exception during render, the fallback static public/500.html is not rendered. Instead I see the Rails default text:

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.

I fixed that using a rescue block:

  def show
    render "errors/#{@rescue_response}", status: @status_code, layout: (logged_in? ? 'application' : 'external')
  rescue
    # For some reason the static 500.html is not being rendered as a fallback,
    # this fixes it. When the render above encounters an exception, this will
    # catch it and render the static page.
    render file: 'public/500.html', status: 500, layout: false
  end

I then figured out that this also affects other error pages, so I used this code which fixed it:

  def show
    render "errors/#{@rescue_response}", status: @status_code, layout: (logged_in? ? 'application' : 'external')
  rescue
    # For some reason the static fallback pages are not being rendered.
    # This rescue block fixes it. When the render above encounters an exception, this will
    # catch it and render the static page.
    if File.exists?("#{Rails.root.join('public', "#{@status_code}.html")}")
      render file: "public/#{@status_code}.html", status: @status_code, layout: false
    else
      render file: 'public/500.html', status: 500, layout: false
    end
  end

Am I doing something wrong which is causing gaffe to not handle this fallback gracefully automatically?

Gaffe doesnt work when controller inherits from ActionController::API

In the ErrorController class for the API module, my base class extends from ActionController::API instead of ActionController::Base.

Upon starting up rails server, I get the following error (short backtrace):

/Users/abc/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/gaffe-1.1.0/lib/gaffe/errors.rb:8:in `block in <module:Errors>': undefined method `layout' for Api::ErrorsController:Class (NoMethodError)
	from /Users/abc/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-4.2.4/lib/active_support/concern.rb:120:in `class_eval'
	from /Users/abc/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-4.2.4/lib/active_support/concern.rb:120:in `append_features'
	from /Users/abc/Server/RoR/xyz/app/controllers/api/errors_controller.rb:4:in `include'
	from /Users/abc/Server/RoR/xyz/app/controllers/api/errors_controller.rb:4:in `<class:ErrorsController>'
	from /Users/abc/Server/RoR/xyz/app/controllers/api/errors_controller.rb:3:in `<module:Api>'
	from /Users/abc/Server/RoR/xyz/app/controllers/api/errors_controller.rb:2:in `<top (required)>'

If i change the inheritance of the ErrorController class to ActionController::Base, then no issues were found and everything worked perfectly.

I suspect this is due to Gaffe requiring that the "layout" function be present.

Rails 6 compatibility

Rails 6 is officially out but unsupported by gaffe. Is there a plan to update to Rails 6 compatibility?

Rails' cache method throws 500 error

If you use Rails' cache method in a partial that gets rendered say during a 404, it will cause a 500 error to be thrown that seems to escalate out of gaffe's control and render the default rails 500 error page.

To reproduce it use a custom 'errors' layout and simply call the cache method around some content. I interestingly couldn't get it to fail in development, even with caching on, but running in staging or production it would fail using the same caching configuration.

Not working at all in my rails application

Rails default error pages are still being displayed instead of custom error pages even after I add this gem. I configured as mentioned in the gem documentation. Not even getting any error to mention here related to this gem but its not working.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.