Some bots or other external requests cause an ActionController::UnknownFormat error when redirecting
Is there a catch-all method to add somewhere in application_controller to handle those errors ?
A config spec to add a default method in request (if none specified) ?
Below an example of trace :
An ActionController::UnknownFormat occurred in main#index:
ActionController::UnknownFormat
app/controllers/main_controller.rb:17:in `index'
-------------------------------
Request:
-------------------------------
* URL : http://ownedurl.com/
* HTTP Method: GET
* IP address : 82.xxx.xxx.xxx
* Parameters : {"controller"=>"main", "action"=>"index"}
* Timestamp : 2015-06-07 20:54:32 +0200
* Server : servername
* Rails root : /var/www/ownedurl.com/releases/208
* Process: 29726
A get request would obviously work; yet it makes it occurs.
Experiences on this one appreciated
thx a lot
This will handle a request without format :
rescue_from ActionController::UnknownFormat, with: :raise_not_found
def raise_not_found
render(text: 'Not Found', status: 404)
end
You can supply the :format to your routes itself to tell it what are the formats are valid and which are not.
Read some discussions on this and this answer.
Related
I have spec to test for internal server error case.
context "when network error" do
it "returns internal_server_error with network error description" do
allow(Authorer::AuthorizationClient).to receive(:post).and_raise(EOFError)
if request_method == "get"
get url, params: params, headers: #header
else
#header["Content-Type"] = "application/vnd.api+json"
post url, params: params, as: :json, headers: #header
end
reload_response_body
expect(response).to have_http_status(:internal_server_error)
end
end
CONTEXT: Authorer::AuthorizationClient is our client code to access some third-party server, and sometimes we face EOFError then we need to handle it by rescuing it and raise again to client for internal server error.
something like this
# code
....
begin
Authorer::AuthorizationClient.evaluate_permission(id) # this one hits third-party
rescue EOFError
raise StandardError, "Network Error"
end
the test was success and green. but there is error log and traces
[test] [2022-08-15 11:22:52.397] [id.test] [E] {"message":null,"exception":"Network Error","extra":null,"context":null}
/app/service/authorer/authorization_client.rb:29:in `rescue in authorize_access'
/app/service/authorer/authorization_client.rb:21:in `rescue in authorize_user'
........
How to omit this error log?
thanks in advance
lately I have received such an error on ruby on rails, but do not know what it means. Any help well appreciated!
ERROR: Failed to generate exception summary:
ActionView::Template::Error: IP spoofing attack?! HTTP_CLIENT_IP="172.17.3.20" HTTP_X_FORWARDED_FOR="79.170.168.251"
EDIT:
I have such a function in the application controller
before_filter :ensure_domain
APP_DOMAIN = 'www.mysite.com'
def ensure_domain
if request.env['HTTP_HOST'] != APP_DOMAIN && Rails.env.production?
#HTTP 301 is a "permanent" redirect
redirect_to "https://#{APP_DOMAIN}#{request.path}", :status => 301
end
end
Unless you're actually doing IP-based authorization, you can likely ignore this. All that this message is telling you is that the IP that the request is coming from is different than the IP that's passed in the HTTP_X_FORWARDED_FOR header, which doesn't necessarily mean that it's a spoof. There are plenty of legitimate reasons for this, so it's generally okay to simply disable the spoof check by setting config.action_dispatch.ip_spoofing_check = false
I keep seeing errors in my logs similar to the below.
Can anyone suggest why I could be getting such an error intermittently? Is it a caching issue?
Every time I attempt to load the about-us page it loads perfectly but every few days there's an error like this in my logs but it's not confined to a single page. Sometimes it's the homepage, some times it's other pages.
Started GET "/about-us" for xxx.xxx.xxx.xxx at 2014-08-16 07:54:06 +0100
Processing by PagesController#about as */*;q=0.6
Geokit is using the domain: mydomain.com
[1m[35mPage Load (0.2ms)[0m SELECT `pages`.* FROM `pages` WHERE `pages`.`name` = 'About Us' LIMIT 1
Completed 500 Internal Server Error in 2ms
ActionView::MissingTemplate (Missing template pages/about, application/about with {:handlers=>[:erb, :builder, :arb], :formats=>["*/*;q=0.6"], :locale=>[:en, :en]}. Searched in:
* "/var/www/myapp/releases/201408150651/app/views"
* "/var/lib/ruby-rvm/gems/ruby-1.9.2-p320/gems/activeadmin-0.3.2/app/views"
* "/var/lib/ruby-rvm/gems/ruby-1.9.2-p320/gems/kaminari-0.12.4/app/views"
* "/var/lib/ruby-rvm/gems/ruby-1.9.2-p320/gems/devise-1.4.7/app/views"
):
This question is similar to: Random rails ActionView::MissingTemplate errors so this is happening to other people but there's no defined answer there either.
You can't prevent this error as there are load reasons(like you mentioned missing cache, unknown request format and etc)
You can try to restrict the number of predefined formats like:
get '/about-us' => 'controller#about', :format => /(?:|html|json)/
Also you can suppress this exception. Create a new file(for example exception_handler.rb) in the directory config/initializers and Add this line into created file:
ActionDispatch::ExceptionWrapper.rescue_responses.merge! 'ActionView::MissingTemplate' => :not_found
Hope it helps.
#ProblemSolvers is a possible solution. However, I added the following method in my application_controller.rb file so that such errors will render a 404 page rather failing with a error message on screen
rescue_from ActionView::MissingTemplate, :with => :rescue_not_found
protected
def rescue_not_found
Rails.logger.warn "Redirect to 404, Error: ActionView::MissingTemplate"
redirect_to '/404' #or your 404 page
end
you can wrap this code in a if statement, something like this if Rails.env.production? given that the env is setup so your dev environment wont be affected
In production, we regularly get the following exception:
An ActionView::MissingTemplate occurred in constructions#show:
Missing template constructions/show, application/show with {:locale=3D>[:=
ru], :formats=3D>[:jpeg, "image/pjpeg", :png, :gif], :handlers=3D>[:erb, :b=
uilder, :coffee, :jbuilder, :haml]}
What puzzles me here is the formats hash, which requests for some image (:jpeg, "image/pjpeg", :png, :gif). We have no custom MIME types registered in our app, and as far as I know there's no corresponding Rails default MIME-type.
So the question is: what kind of request generates this formats hash?
I got same error as well. I notices this is from a search engine of "YandexImage" trying to get custom format. On my controller and action is just empty, because it is a static *.html.erb page. Here is more information.
* DOCUMENT_ROOT : /srv/www/apps/mysite/current/public
* HTTP_ACCEPT : image/jpeg, image/pjpeg, image/png, image/gif
* HTTP_CONNECTION : Keep-Alive
* HTTP_FROM : support#search.yandex.ru
* HTTP_HOST : mysite.com
* HTTP_USER_AGENT : Mozilla/5.0 (compatible; YandexImages/3.0; +http://yandex.com/bots)
* ORIGINAL_FULLPATH : /
Two ways to fix this:
Edit public/robots.txt to block YandexImage. see more information at http://yandex.com/bots
User-agent: YandexImage
Disallow: /
Or put following code to your action, it will handle only html otherwise raise the not found page
respond_to do |format|
format.html
format.any { raise ActionController::RoutingError.new('Not Found') }
end
I use devise 2.2.2 with rails 3.2.11
I use devise with ajax requests
I changed the following configuration in initializers/devise.rb
config.navigational_formats = [:json, :html]
config.http_authenticatable_on_xhr = false
when I submit an empty sign in request, I expect to get a json response with errors hash, but i get a 500 instead (see below for the trace) (it works fine with sign up request)
here are my routes (nothing special)
devise_for :users
the trace:
Started POST "/users/sign_in.json" for 127.0.0.1 at 2013-01-27 13:33:45 +0100
Processing by Devise::SessionsController#create as JSON
Parameters: {"user"=>{"email"=>"", "password"=>"[FILTERED]"}}
Completed 401 Unauthorized in 1ms
Processing by Devise::SessionsController#new as JSON
Parameters: {"user"=>{"email"=>"", "password"=>"[FILTERED]"}}
Completed 500 Internal Server Error in 40ms
NoMethodError (undefined method `users_url' for #<Devise::SessionsController:0x007fe88ddd9550>):
You are probably overriding after_sign_in_path_for and have a code path in there that returns nil.
This causes devise to fall back to its default behaviour and call users_url to get the path to redirect to.
Why do I think this? Because you are having the same error I had (and lost some hair over) and also this bug report contains the github usernames of many other people who have been humbled by this particular issue.