Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internal Server Error 500 gets thrown instead of 404 while trying to access broken picture urls

We have a rails server with custom 404 and 500 pages setup using this tutorial here:

http://ramblinglabs.com/blog/2012/01/rails-3-1-adding-custom-404-and-500-error-pages

While it works nice and throws 404s for all kinds of paths, it generates internal server errors 500 while trying to access any kind of suffixed path like en/foo.png, en/foo.pdf, en/foo.xml, ...

But something like en/file.foo throws 404. So only valid suffixes throw a 500.

End of routes.rb:

if Rails.application.config.consider_all_requests_local
  match '*not_found', to: 'errors#error_404'
end

application_controller.rb

  unless Rails.application.config.consider_all_requests_local
    rescue_from Exception, with: :render_500
    rescue_from ActionController::RoutingError, with: :render_404
    rescue_from ActionController::UnknownController, with: :render_404
    rescue_from ::AbstractController::ActionNotFound, with: :render_404
    rescue_from ActiveRecord::RecordNotFound, with: :render_404
  end

  protected

  def render_404(exception)
    @not_found_path = exception.message
    respond_to do |format|
      format.html { render template: 'errors/error_404', layout: 'layouts/application', status: 404 }
      format.all { render nothing: true, status: 404 }
    end
  end

  def render_500(exception)
    logger.fatal(exception)
    respond_to do |format|
      format.html { render template: 'errors/error_500', layout: 'layouts/application', status: 500 }
      format.all { render nothing: true, status: 500}
    end
  end

500 that appears:

Missing template errors/error_404 with {:locale=>[:de, :en], :formats=>[:png], :handlers=>[:erb, :builder, :coffee, :arb, :haml]}
like image 963
NielsH Avatar asked Apr 23 '12 12:04

NielsH


People also ask

How do you fix 500 Internal server error There is a problem with the resource you are looking for and it Cannot be displayed?

500 - Internal server error. There is a problem with the resource you are looking for, and it cannot be displayed. To resolve this issue, set the Enable 32-bit Applications to "False": Open the Internet Information Services (IIS) Manager.

How do I fix 500 Internal server error in IIS?

The error 500.19 is an internal server error often occurring on a server using Microsoft IIS software. It indicates that the configuration data for the page is invalid. To solve the issue, delete the malformed XML element from the Web. config file or from the ApplicationHost.


2 Answers

We found the mistake.

We had an error_controller.rb containing this:

  def error_404
    @not_found_path = params[:not_found]
    render template: 'errors/error_404', layout: 'layouts/application', status: 404
  end

and we changed it to fix this problem to:

  def error_404
    @not_found_path = params[:not_found]
    respond_to do |format|
      format.html { render template: 'errors/error_404', layout: 'layouts/application', status: 404 }
      format.all { render nothing: true, status: 404 }
    end
  end
like image 181
NielsH Avatar answered Oct 18 '22 16:10

NielsH


Try adding

respond_to :html, :json, :png

and any other necessary formats at the top of your controller. If I'm right, then the problem is that format.all in the individual controller actions isn't set up to include :png as one of the formats it responds to.

You will probably also need to add to your config/environment.rb the following definition and any similar ones:

Mime::Type.register "image/png", :png

See more details here. Basically you need to set up the mime types that you want to respond to. The error message indicates that rails doesn't understand how to render the format png.

like image 25
Peter Avatar answered Oct 18 '22 15:10

Peter