Do not trust the on screen representations of types since they may be more complex

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the types category.

Last Updated: 2021-05-16

I had production issues with something like the following:

response = HTTP.get('http://www.google.com')
raise ArgumentError, response.code, response.body

It blew up in an unexpected way, complaining (via the set_backtrace method) TypeError: backtrace must be Array of String

The type of response.body, although the method name (and its printed representation on screen) suggests a string, but in fact it isn't, instead being a HTTP::Response::Body object.

The solution was either to explicitly call to_s on this response.body object before passing to raise ArgumentError, or to wrap in string interpolation, which does as much anyway.

# Both work
raise ArgumentError, response.code response.body.to_s
# or
raise ArgumentError, "#{response.code} #{response.body}"

I had a similar issue recur in JavaScript:

const object = {age: 10}
if ( ! found) { 
  throw new Error(`Object was ${object}`)
}
// just prints "Object was [Object object]" instead of the actual objct

Get a readable representation with the following:

throw new Error(`Object was ${JSON.stringify(object)}`)