Customizing Object Display

By default, Honeybadger supports displaying the following core Ruby objects (uncoincidentally, these objects are also supported by JSON):

Hash
Array
Set
Numeric
TrueClass
FalseClas
NilClass
String

When an object of a different class is sent as data to Honeybadger (via context, request data, local variables, etc.), it's first converted to a string using the String() function.

For instance, given a User object which defines the #to_s method to return the user's email address:

class User < ApplicationRecord
  def to_s
    email
  end
end

user = User.create(email: "user@example.com")

Honeybadger.context({ user: user })

...Honeybadger will display the context as:

{
  user: "user@example.com"
}

If this value is undesirable (since there's no way to know the class of the object), the #to_honeybadger method can be defined to customize the value that is reported to Honeybadger:

class User < ApplicationRecord
  def to_s
    email
  end

  def to_honeybadger
    "#<User email=#{ email }>"
  end
end

...now the context will display as:

{
  user: "#<User email=user@example.com>"
}

Note that while by default the contents of #inspect are filtered to prevent leaking sensitive attributes, attributes are not filtered when returning #inspect from #to_honeybadger, so it's always best to explicitly interpolate the attributes that you want to display unless you know that the inspected output will never contain sensitive information.