Question

I've got a Ruby script and I'm doing this

module MyModule
    class MyClass
        def do_something
            begin
                deployer_object = Object.const_get("MyModule").const_get("#{class_name}Deployer").new(@config, @directory).deploy
            rescue NameError => e
                MyModule::Logger.error("Error loading the deployer #{class_name}. This deployer it's not installed!")
            end
        end
    end
end

Now it rescues all the NameError exceptions in this class and the one loaded in the fifth line as well. How can I make it so it only rescues exceptions in MyModule::MyClass and not in the one being initialized in the 5th line? Thanks!

Was it helpful?

Solution

You don't need to pack everything inside a begin once again. This works in the same manner:

module MyModule
    class MyClass
        def do_something
            deployer_object = Object.const_get("MyModule").const_get("#{class_name}Deployer").new(@config, @directory).deploy
        rescue NameError => e
            MyModule::Logger.error("Error loading the deployer #{class_name}. This deployer it's not installed!")
        end
    end
end

I don't fully understand your question. What are you trying to rescue from? You don't want to catch exceptions from Deployer's initializer?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top