Rails custom error pages won't render on Nginx - ruby-on-rails

I am new to Rails and Nginx. I was asked to add custom error pages to our app. I found instructions to do so on this site and my custom error pages render just fine locally. When I go to the url /no_such_page, I see the appropriate page. When I deploy the code to our test server running nginx and try the same url, I see the following 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.`
On the nginx server, if I go to the url \404, then I do see my page, so I know it renders ok.
Both my local machine and the server are running under the development environment.
I've added this to config\development.rb:
config.consider_all_requests_local = false
config.exceptions_app = self.routes
Here is what I've added to routes.rb:
%w(404 500).each do |code|
get code, to: "errors#show", :code => code, :via => :all
end
and my errors_controller looks like this:
class ErrorsController < ApplicationController
def show
status_code = params[:code] || 500
render status_code.to_s
end
end
Does anyone know if there is something special that I need to do in my nginx config to make this work?

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.
This error message is from Rails itself, not from Nginx.
https://github.com/rails/rails/blob/v5.0.0.1/actionpack/lib/action_dispatch/middleware/show_exceptions.rb#L15-L22
It means that your custom error page is itself raising an exception, which would lead to an infinite loop of error->show error page->error->show error page->... Rails detects this and halts with the error message you are seeing. Check your logs for this string to see what the error is:
Error during failsafe response:

Related

How to pass rewritten url to fcgi in lighttpd

I have a redmine instance that runs in the /redmine sub-uri. This is fully working and I can retrieve /redmine/robots.txt without fault.
Adding
url.rewrite = ( "^/robots\.txt$" => "/redmine/robots.txt" )
still gives a 404 error when trying to retrieve /robots.txt.
The request in error.log appears identical to GETting /redmine/robots.txt after an initial block changing the url if I turn on debug.log-request-handling = "enable"
Using url.rewrite-once does not seem to make a difference.
The request never shows up in redmine production.log.
So my question is what I might be missing?

json-rpc event-machine stand alone service

What am I doing wrong?
I try to run example code from json-rpc documentation. Togather with EventMachine:
require 'json-rpc'
require 'thin'
class AsyncApp
include JsonRpc
AsyncResponse = [-1, {}, []].freeze
def call env
rpc_call(env)
end
def rpc_sum a, b
result = Rpc::AsyncResult.new
EventMachine::next_tick do
result.reply a + b
result.succeed
end
result
end
end
EM::run do
Thin::Server.start('0.0.0.0', 8999) do
map('/'){ run AsyncApp.new }
end
end
There is no error on this server console appears.
The result is on transport layer on the json-rpc client is:
500 Internal Server Error
I've try same client with jimson gem implememtation - it work fine but does not support EventMachine and async calls. (Show example if you know how it possible)
The problem was at default "welcome" page assigned to route "/".
I do not try to go with browser to "/", but only try to connect by rpc client.
Some how default "welcome" page route "/" rule is not overwrited by map("/"){...} rule.
The solution is rewrite route rule like this map("/rpc"){...}

How to handle "NetworkError: 400 Bad Request" in rails?

I am facing an error when a user enters search string as '%' in url itself. After entering my url looks as
http://localhost:3000/search/%
Now its showing "NetworkError: 400 Bad Request - http://localhost:3000/search/%". I want to redirect to some other page and show, a bad request image when the error occurs. Please suggest me some ideas to do this.
I have attached the error image.
UPDATE
I have tried the below.
config/application.rb:
config.exceptions_app = self.routes
config/routes.rb:
match "/400", :to => "errors#bad_request"
It's not coming inside the bad_request method.
In Log i am getting error as
Invalid request: Invalid HTTP format, parsing fails.
Have you checkout this gem 'utf8-cleaner' which removes invalid UTF-8 characters from the environment so that your app doesn't choke on them.
If you want custom error pages: then look at this answer

Pusher Heroku Add-on error

so I'm using the Pusher Heroku Add-on for my application. The application has live notifications, so when a user receives a message he will see a pop up notification saying "new message". However, In production I am getting the below error:
Firefox can't establish a connection to the server at ws://ws.pusherapp.com/app/b1cc5d4f400faddcb40b?protocol=7&client=js&version=2.1.6&flash=false.
Reload the page to get source for: http://js.pusher.com/2.1/pusher.min.js
And here's the Pusher controller:
class PusherController < ApplicationController
protect_from_forgery :except => :auth # stop rails CSRF protection for this action
def auth
Pusher.app_id = ENV['PUSHER_APP_ID']
Pusher.key = ENV['PUSHER_KEY']
Pusher.secret = ENV['PUSHER_SECRET']
if current_user && params[:channel_name] == "private-user-#{current_user.id}"
response = Pusher[params[:channel_name]].authenticate(params[:socket_id])
render :json => response
else
render :text => "Not authorized", :status => '403'
end
end
end
And I'm using the figaro gem to push the keys to heroku.
What am I doing wrong?
Kind regards
JS
That looks like a problem with Javascript, rather than Rails
We've got pusher working very well with one of our production apps, and it works by firstly having the pusher gem installed, allowing you to call the pusher JS files from your layout:
#app/views/layouts/application.html.erb
<%= javascript_include_tag "http://js.pusher.com/2.1/pusher.min.js" %>
Rails
You may also wish to put the pusher initialization code into an initializer:
#config/initializers/pusher.rb
Pusher.url = ENV["PUSHER_URL"]
Pusher.app_id = ENV["PUSHER_APP_ID"]
Pusher.key = ENV["PUSHER_KEY"]
Pusher.secret = ENV["PUSHER_SECRET"]
This will ensure app-wide connectivity, rather than controller-specific (allowing for greater flexibility)
Firefox can't establish a connection to the server at ws://ws.pusherapp.com/app/b1cc5d4f400faddcb40b?protocol=7&client=js&version=2.1.6&flash=false.
Reload the page to get source for: http://js.pusher.com/2.1/pusher.min.js
This doesn't necessarily mean anything is wrong. it just means that an unsecured WebSocket connection couldn't be established. Pusher's fallback strategy should result in a successful connection being established via either HTTP fallback (HTTP or HTTPS) or via WSS (a secure WebSocket connection).
Failed connection attempts are logged as console errors. There's nothing that can be done about that.
To test this you can bind to connection events and ensure that you are indeed connecting. The pusher-js JavaScript logging will also help determine what's happening.
You can also try http://test.pusher.com/

How do I get my rails app to work around errors?

My rails app is a web scraper using Mechanize / Nokogiri. Due to problems with KBB.com and their cookies I have to clear my cookie jar in my app every time I issue a new get request to their server.
agent.cookie_jar.clear!
However, while my app is scraping data, occasionally it hits a KBB.cpm page with an automatic redirect! This causes an error:
Mechanize::ResponseCodeError: 500 => Net::HTTPInternalServerError for http://www.kbb.com/toyota/prius/2002-toyota-prius/sedan-4d/options/?vehicleid=4843&intent=buy-used -- unhandled response
This causes my rails app to crash because I can't clear the cookie jar before it redirects. Instead, what I would like for my app to do is recognize that it could hit an error and if it does to use a different process. Something like:
if there_is_an_error
# alternative process for redirect
else
# business as usual
end
here's my code:
agent = Mechanize.new
agent.cookie_jar.clear!
page = agent.get(url)
agent.cookie_jar.clear!
page.link_with(:text => "Choose this style").click
agent.cookie_jar.clear!
agent.page.link_with(:text => "Choose price type").click
agent.cookie_jar.clear!
agent.page.links_with(:text => "Get used car price")[2].click
url = agent.page.uri.to_s.sub('retail', 'private-party')
agent.cookie_jar.clear!
agent.get(url)
#kbb_value = agent.page.at('.selected .value').text.delete('$')
You should look at http://www.tutorialspoint.com/ruby/ruby_exceptions.htm on handling exceptions. There is also a stack overflow post about this: Begin, Rescue and Ensure in Ruby? . You could fix your problem by setting a flag in the exception handling block, then checking for that flag later on in your code to find out if an exception occurred, and this should fix your problem.
you can just rescue the Mechanize::ResponseCodeError exception and do your alternative process for redirect inside that block

Resources