I am using parse-ruby-client in a Rails web app. According to the documentation at http://www.rubydoc.info/github/adelevie/parse-ruby-client/file/README.md#Logging_In, I can do
user = Parse::User.authenticate("cooldude6", "p_n7!-e8")
to log a user in. This works as long as the credentials are correct. If they're not correct, I get an error in the Rails app pointing to that line above.
In the logs, I see:
I, [2015-10-29T18:41:06.636916 #563] INFO -- Status: 404
Completed 500 Internal Server Error in 625ms
My question is: how do I catch this 404 status and 500 internal server error so the page doesn't get rendered and throw this error? Ideally I'd want to redirect back to the sign in page if there is an error.
You need to rescue Parse::ParseProtocolError
user = Parse::User.authenticate(params[:username], params[:password])
# continue normally
rescue Parse::ParseProtocolError => e
status_code = e.code
# handle error and try again
Related
I have done this tons of times before when fetching things from a database, etc.
For my specific case I am using a 3rd party to connect to a piece of hardware... Anyways, in the case of an error, such as an invalid id obviously, we want to raise a exception or a rescue... but unfortunately I don't know how to raise it because by the time it is hit, it's too late (I think)
Here...
#
# getting params and saving item above...
#
if item.save
device = RubySpark::Device.new("FAKEUNITID800")
device.function("req", "ITEM")
redirect_to controller: 'items', action: 'edit_items'
end
If this was a valid ID, everything would work, and it would take you to the /edit page! But the issue is, with an invalid ID, it just does...
Completed 500 Internal Server Error in 897ms
RubySpark::Device::ApiError - Permission Denied: Invalid Device ID:
I checked out the following tutorials
Rescue StandardError, Not Exception
How to catch 404 and 500 error in Rails?
Dynamic Rails Error Pages
But honestly, they just make me more confused. Maybe I have the wrong approach to this. I always thought that first you make the request, and then you have a fall back case, depending what status (ie. 200, 500, 404) you get... you go from there.
Rails returns an 500 Internal Server Error response because an exception was raised that it does not no how to handle. You can't rescue "500 Internal Server Error" in Rails because it is not an exception - its the framework bailing from an uncaught exception to avoid data loss or unpredictable behavior.
Fortunatly you don't have to. You can just rescue the RubySpark exception:
begin
device = RubySpark::Device.new("FAKEUNITID800")
device.function("req", "ITEM")
rescue RubySpark::Device::ApiError => e
logger.error(e.message)
end
You can also use rescue_from in Rails controllers that wraps the entire action in a before block:
class FooController < ApplicationCotnroller
rescue_from RubySpark::Device::ApiError, with: :do_something
# ...
end
I have an application running on NGINX/Passenger in production environment. Most of the pages render fine, but some do not. The server returns the static 404 error page, but when checking the production log, the following is appended:
Started GET "/upload" for 92.111.174.132 at 2015-11-26 14:07:50 +0000
Processing by Upload::StaticController#index as HTML
Rendered upload/static/index.html.erb within layouts/upload (1.1ms)
Completed 500 Internal Server Error in 8.0ms
Note that there are no error messages or backtraces; this is really all there is. I've checked NGINX's error log and it does not report an error. I really don't know where to go from here.
In my rails4 project I am using the client side validation gem (branch 4-0-beta).
For security reasons I have disabled the uniqueness validation in the config/initializers/client_side_validations.rb file.
ClientSideValidations::Config.disabled_validators = [:uniqueness]
This works fine and when I test in the browser whether a user email exists I get a 500 status no matter if the user exists or not.
http://localhost:3000/validators/uniqueness?case_sensitive=false&id=01&user[email]=tester#mail.com&_=1379052030490
(If uniqueness is not disabled I get a 200 or 404 status response dependent on whether the user is unique or not.)
HTTP Status Code
However I am not sure if I understand correctly the status code defined in middleware.rb:
client_side_validations / lib / client_side_validations / middleware.rb
def call(env)
if matches = /^\/validators\/(\w+)$/.match(env['PATH_INFO'])
if ClientSideValidations::Config.disabled_validators.include?(matches[1].to_sym)
[500, {'Content-Type' => 'application/json', 'Content-Length' => '0'}, ['']]
else
"::ClientSideValidations::Middleware::#{matches[1].camelize}".constantize.new(env).response
end
else
#app.call(env)
end
end
end
My understanding is that status code 500 should be used for server errors ("A generic error message, given when no more specific message is suitable"). However in this case the uniqueness validation is disabled on purpose and I was therefore thinking whether a 403 "Forbidden" or 405 "Method not allowed" would be more appropriate?
The reason why I am not too happy about the 500 status is that Chrome will automatically display a server error message, and since the client_side_validation gem is not raising an exception, which I can catch within rails, I am stuck with Chrome's default server error message for 500 status code.
Any advice on the appropriate status code in this situation or alternative solutions would be cool.
I'm implementing a tool that needs to crawl a website. I'm using anemone to crawl and on each anemone's page I'm using boilerpipe and Nokogiri to manage HTML format, etc.
My problem is: if I get 500 Internal Server Error, it makes Nokogiri fail because there is no page.
Anemone.crawl(name) do |anemone|
anemone.on_every_page do |page|
if not (page.nil? && page.not_found?)
result = Boilerpipe.extract(page.url, {:output => :htmlFragment, :extractor => :ArticleExtractor})
doc = Nokogiri::HTML.parse(result)
end
end
end
In the case above, if there is a 500 Internal Server Error, the application will give an error on Nokogiri::HTML.parse(). I want to avoid this problem. If the server gives an error I want to continue computation ignoring this page.
There is any way to handle 500 Internal Server Error and 404 Page Not Found with these tools?
Kind regards,
Hugo
# gets the reponse of the link
res = Net::HTTP.get_response(URI.parse(url))
# if it returns a good code
if res.code.to_i >= 200 && res.code.to_i < 400 #good codes will be betweem 200 - 399
# do something with the url
else
# skip the object
next
end
I ran into a similar problem. The question and the reply is here
How to handle 404 errors with Nokogiri
I have this line in my view file:
- tweets = Twitter.user_timeline(#organization.twitter_name)
The attr twitter_name is entered by the user. If they enter an invalid twitter account name, it throws a 404 error.
How can I deal with the error to avoid getting this action controller exception? Twitter::NotFound in Organizations#show
You ca use rescue to catch an exception and show a user friendly error message