Skip to content

Instantly share code, notes, and snippets.

@mcmire
Last active October 16, 2021 11:09
Show Gist options
  • Save mcmire/68cd9c74ba765a2d5dfb14abf58409aa to your computer and use it in GitHub Desktop.
Save mcmire/68cd9c74ba765a2d5dfb14abf58409aa to your computer and use it in GitHub Desktop.

Revisions

  1. mcmire revised this gist Apr 24, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bypass_broken_images_middleware.rb
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    # ------------
    #
    # * Save this as app/middlewares/bypass_broken_images_middleware.rb
    # * Add the following line inside of the Rails.application.configure block
    # * Add the following inside of the Rails.application.configure block
    # in config/environments/test.rb:
    #
    # config.middleware.insert_before(
  2. mcmire revised this gist Apr 24, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions bypass_broken_images_middleware.rb
    Original file line number Diff line number Diff line change
    @@ -25,6 +25,8 @@ def call(env)
    raise error
    end
    end

    private

    def unknown_path?(error)
    error.is_a?(ActionController::RoutingError)
  3. mcmire created this gist Apr 24, 2017.
    37 changes: 37 additions & 0 deletions bypass_broken_images_middleware.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    # Instructions
    # ------------
    #
    # * Save this as app/middlewares/bypass_broken_images_middleware.rb
    # * Add the following line inside of the Rails.application.configure block
    # in config/environments/test.rb:
    #
    # config.middleware.insert_before(
    #  ActionDispatch::DebugExceptions,
    #  BypassBrokenImagesMiddleware,
    # )

    class BypassBrokenImagesMiddleware
    def initialize(app)
    @app = app
    end

    def call(env)
    @app.(env)
    rescue => error
    if unknown_path?(error) && request_for_image?(env)
    # nothing to see here, move along
    [404, {}, ""]
    else
    raise error
    end
    end

    def unknown_path?(error)
    error.is_a?(ActionController::RoutingError)
    end

    def request_for_image?(env)
    env["PATH_INFO"] =~ %r{\A/(assets|images)/} ||
    env["PATH_INFO"] =~ /\.(jpg|jpeg|png)\Z/
    end
    end