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
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'm on Sinatra and i don't understand how to deal with my problem:
I want to "send" to curl a custom message when he try to go on a wrong path.
curl http://localhost:port/blabla
It's an error 404 but i want to send him thing like 'error try other path'
I tried with this :
before'/*' do
if (params[:splat].to_s =~ /path_i_want/) != 2
'wrong path'
end
end
or with raise 404 but it doesn't work.
Could you help me please ?
Regards.
Sinatra has a built-in handler for 404, see Error Handling page. You could do all your logic in there.
Not Found
When a Sinatra::NotFound exception is raised, or the
response’s status code is 404, the not_found handler is invoked:
not_found do
'This is nowhere to be found.'
end
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
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
I'm using Symfony2.1.
It has a builtin CSRF protection for the forms. The error message returned when the CSRF token is invalid is: "The CSRF token is invalid. Please try to resubmit the form".
I show it on the top of the form in my Twig template by using the classic call:
{{ form_errors(form) }}
How can I change the returned message?
In alternative, a more advanced possibility is to catch this error type in order to show a lot of options/links in my Twig template.
Any idea?
Did you try to set in the file validators.{locale_code}.yml to set a translation for key The CSRF token is invalid. Please try to resubmit the form?
To change the default message you can try this out:
#MyBundle\Resources\translations\validators.en.yml
The CSRF token is invalid. Please try to resubmit the form : My custom CSRF error
The error message is thrown here and it is of FormError class...thus it should be possible to translate it.