Search code examples
rubychef-infrarecipe

How prevent chef failure on ruby code?


Sometimes Chef fails on the next code (this code not in block or any resource):

tmp = title.force_encoding("ISO-8859-1").encode("UTF-8")
title = tmp.encode('ISO8859-1').force_encoding('UTF-8')

error that I get:

NoMethodError

undefined method `force_encoding' for nil:NilClass

My question is how best practice to ignore any code error and to continue to run rest recipes, thanks


Solution

  • You didn't show where title comes from so they best I can say is to put your code in a ruby_block resource and use the ignore_failure property. You can also use normal Ruby rescue blocks for imperative code but be aware of how that interacts (or rather, doesn't) with resources, see https://coderanger.net/two-pass/ for some details on the loading process Chef uses.