quirkey / resque-status

resque-status is an extension to the resque queue system that provides simple trackable jobs.
MIT License
515 stars 169 forks source link

Encoding::InvalidByteSequenceError when handling exception in safe_perform #118

Open koenigma1 opened 10 years ago

koenigma1 commented 10 years ago

Summary: safe_perform! catches exceptions, and then records the exception through the failed method: failed("The task failed because of an error: #{e}") Eventually we attempt to dump the exception string to JSON and get this error: Encoding::InvalidByteSequenceError: "\xC7" on US-ASCII activesupport-3.2.19/lib/active_support/json/encoding.rb" line 123 in encode activesupport-3.2.19/lib/active_support/json/encoding.rb" line 123 in escape activesupport-3.2.19/lib/active_support/json/encoding.rb" line 69 in escape activesupport-3.2.19/lib/active_support/json/encoding.rb" line 177 in encode_json activesupport-3.2.19/lib/active_support/json/encoding.rb" line 48 in block in encode activesupport-3.2.19/lib/active_support/json/encoding.rb" line 77 in check_for_circular_references activesupport-3.2.19/lib/active_support/json/encoding.rb" line 46 in encode activesupport-3.2.19/lib/active_support/json/encoding.rb" line 252 in block in encode_json activesupport-3.2.19/lib/active_support/json/encoding.rb" line 252 in each activesupport-3.2.19/lib/active_support/json/encoding.rb" line 252 in map activesupport-3.2.19/lib/active_support/json/encoding.rb" line 252 in encode_json activesupport-3.2.19/lib/active_support/json/encoding.rb" line 48 in block in encode activesupport-3.2.19/lib/active_support/json/encoding.rb" line 77 in check_for_circular_references activesupport-3.2.19/lib/active_support/json/encoding.rb" line 46 in encode activesupport-3.2.19/lib/active_support/json/encoding.rb" line 31 in encode activesupport-3.2.19/lib/active_support/core_ext/object/to_json.rb" line 16 in to_json multi_json-1.3.7/lib/multi_json/adapters/json_common.rb" line 11 in dump multi_json-1.3.7/lib/multi_json.rb" line 115 in dump resque-1.25.2/lib/resque.rb" line 31 in encode resque-status-d8b2bbba92ed/lib/resque/plugins/status/hash.rb" line 193 in block (2 levels) in singletonclass resque-status-d8b2bbba92ed/lib/resque/plugins/status/hash.rb" line 41 in set resque-status-d8b2bbba92ed/lib/resque/plugins/status.rb" line 184 in status= resque-status-d8b2bbba92ed/lib/resque/plugins/status.rb" line 249 in set_status resque-status-d8b2bbba92ed/lib/resque/plugins/status.rb" line 227 in failed resque-status-d8b2bbba92ed/lib/resque/plugins/status.rb" line 173 in rescue in safe_perform! resque-status-d8b2bbba92ed/lib/resque/plugins/status.rb" line 160 in safe_perform!

I monkey-patched a workaround for this issue, but I don't think this is good solution. Looking for a suggestion of a good way to fix this so I can create a PR. https://github.com/quirkey/resque-status/blob/master/lib/resque/plugins/status.rb#L173

start monkey patch

    msg = "The task failed because of an error: #{e}"
    encoding_options = {
        :invalid           => :replace,  # Replace invalid byte sequences
        :undef             => :replace,  # Replace anything not defined in ASCII
        :replace           => '?',       # Use a ? for those replacements
        :universal_newline => true       # Always break lines with \n
    }
    msg.encode!(Encoding.find('ASCII'), encoding_options)
    failed(msg)
    # end monkey patch

Thanks.