rescue

General rescue throughout controller when id not found - RoR

一个人想着一个人 提交于 2019-12-04 10:14:05
问题 I have stumbled upon a situation where my application looks for an id that does not exist in the database. An exception is thrown. Of course, this is a pretty standard situation for any web developer. Thanks to this answer I know that using rescue deals with the situation pretty neatly, like so: def show @customer = Customer.find(params[:id]) rescue ActiveRecord::RecordNotFound #customer with that id cannot be found redirect_to action: :index #redirect to index page takes place instead of

How to deal with not knowing what exceptions can be raised by a library method in Ruby?

隐身守侯 提交于 2019-12-04 10:02:56
问题 This is somewhat of a broad question, but it is one that I continue to come across when programming in Ruby. I am from a largely C and Java background, where when I use a library function or method, I look at the documentation and see what it returns on error (usually in C) or which exceptions it can throw (in Java). In Ruby, the situation seems completely different. Just now I need to parse some JSON I receive from a server: data = JSON.parse(response) Naturally, the first thing I think

Ruby Timeout::timeout doesn't fire Exception and doesn't return what documented

别来无恙 提交于 2019-12-03 12:01:21
i have this piece of code: begin complete_results = Timeout.timeout(4) do results = platform.search(artist, album_name) end rescue Timeout::Error puts 'Print me something please' end I then launch the method containing this code, and well, here is the beginning of a stack trace: Exception message : execution expired Exception backtrace : /***/****/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/timeout.rb:64:i So i naively thinks that my call timeouted. But 'Print me something please' is never printed and complete_results wich is suppose to be the timeout status return value (either true or false, as

Begin Rescue not catching error

为君一笑 提交于 2019-12-03 08:25:59
问题 I'm using some ruby code wrapped in a begin - rescue block but somehow it manages to still crash. the block of code looks like this: # Retrieve messages from server def get_messages @connection.select('INBOX') @connection.uid_search(['ALL']).each do |uid| msg = @connection.uid_fetch(uid,'RFC822').first.attr['RFC822'] begin process_message(msg) add_to_processed_folder(uid) if @processed_folder rescue handle_bogus_message(msg) end # Mark message as deleted @connection.uid_store(uid, "+FLAGS", [

How can I password-protect my /sidekiq route (i.e. require authentication for the Sidekiq::Web tool)?

我怕爱的太早我们不能终老 提交于 2019-12-03 06:11:49
问题 I am using sidekiq in my rails application. By Default, Sidekiq can be accessed by anybody by appending "/sidekiq" after the url. I want to password protect / authenticate only the sidekiq part. How can i do that? 回答1: Put the following into your sidekiq initializer require 'sidekiq' require 'sidekiq/web' Sidekiq::Web.use(Rack::Auth::Basic) do |user, password| [user, password] == ["sidekiqadmin", "yourpassword"] end And in the routes file: authenticate :user do mount Sidekiq::Web => '/sidekiq

General rescue throughout controller when id not found - RoR

二次信任 提交于 2019-12-03 05:17:45
I have stumbled upon a situation where my application looks for an id that does not exist in the database. An exception is thrown. Of course, this is a pretty standard situation for any web developer. Thanks to this answer I know that using rescue deals with the situation pretty neatly, like so: def show @customer = Customer.find(params[:id]) rescue ActiveRecord::RecordNotFound #customer with that id cannot be found redirect_to action: :index #redirect to index page takes place instead of crashing end In case the customer cannot be found, the user gets redirected to the index page. This works

How to deal with not knowing what exceptions can be raised by a library method in Ruby?

大城市里の小女人 提交于 2019-12-03 04:45:59
This is somewhat of a broad question, but it is one that I continue to come across when programming in Ruby. I am from a largely C and Java background, where when I use a library function or method, I look at the documentation and see what it returns on error (usually in C) or which exceptions it can throw (in Java). In Ruby, the situation seems completely different. Just now I need to parse some JSON I receive from a server: data = JSON.parse(response) Naturally, the first thing I think after writing this code is, what if the input is bad? Is parse going to return nil on error, or raise some

work with rescue in Rails

℡╲_俬逩灬. 提交于 2019-12-02 23:30:11
I am working with the following piece; def index @user = User.find(params[:id]) rescue flash[:notice] = "ERROR" redirect_to(:action => 'index') else flash[:notice] = "OK" redirect_to(:action => 'index') end Now I either case whether I have a correct ID or not, I am always getting "OK" in my view, what am I doing wrong? I need that when I have no ID in the DB to show "ERROR". I have also tried to use rescue ActiveRecord::RecordNotFound but same happens. All help is appreciated. shingara All code after the end of the rescue block is interpreted only if there are no returns in the rescue block.

hdu Rescue

匿名 (未验证) 提交于 2019-12-02 22:56:40
Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards. You have

How can I password-protect my /sidekiq route (i.e. require authentication for the Sidekiq::Web tool)?

我们两清 提交于 2019-12-02 18:42:06
I am using sidekiq in my rails application. By Default, Sidekiq can be accessed by anybody by appending "/sidekiq" after the url. I want to password protect / authenticate only the sidekiq part. How can i do that? bravenewweb Put the following into your sidekiq initializer require 'sidekiq' require 'sidekiq/web' Sidekiq::Web.use(Rack::Auth::Basic) do |user, password| [user, password] == ["sidekiqadmin", "yourpassword"] end And in the routes file: authenticate :user do mount Sidekiq::Web => '/sidekiq' end Mark Nadig See "Security" under https://github.com/mperham/sidekiq/wiki/Monitoring Sidekiq