The code in the begin block creates an instance of the Chef::Resource::Bash resource, assigns the code and action resource attributes and adds the resource to run_context.resource_collection . A resource does not "work" in any sense. Creating a resource instance does not raise any exceptions, so the code in the rescue block does not run.
Later, once all of your recipe code is finished, Chef will run_context.resource_collection over resources in run_context.resource_collection . For each resource, the chef will trigger actions with resources. In the case of the bash resource, this means that the line specified in the code resource attribute is executed. Note that this happens after your recipe and all other recipes are finished, and this happens well outside of your begin block. Any exceptions caused by this resource when performing its actions do not apply to your begin block, because actions are not called from your begin block, but from it after it.
You can try something like this:
bash "mongo fix" do code "mongo --verbose #{filename} || python pymongo reconfig" end
source share