MindscapeHQ / raygun4node

Node.JS SDK for Raygun
https://raygun.com
MIT License
31 stars 29 forks source link

Can't Handle Symbol() Attribute On Error Object #68

Closed ki4jnq closed 4 months ago

ki4jnq commented 6 years ago

We attach Symbol() type attributes to our known error conditions in our Express application. When Raygun tries to serialize these attributes the following error is raised:

TypeError: Cannot convert a Symbol value to a string
    at toHumanString (/Users/myusername/Projects/myProjectName/node_modules/object-to-human-string/lib/index.js:29:63)
    at RaygunMessageBuilder.setErrorDetails (/Users/myusername/Projects/myProjectName/node_modules/raygun/lib/raygun.messageBuilder.js:54:15)
    at Raygun.raygun.send (/Users/myusername/Projects/myProjectName/node_modules/raygun/lib/raygun.js:98:14)
    at Raygun.raygun.expressHandler (/Users/myusername/Projects/myProjectName/node_modules/raygun/lib/raygun.js:144:16)
    at WebFrameworkShim.applySegment (/Users/myusername/Projects/myProjectName/node_modules/newrelic/lib/shim/shim.js:1332:17)
    at _applyRecorderSegment (/Users/myusername/Projects/myProjectName/node_modules/newrelic/lib/shim/shim.js:949:20)
    at _doRecord (/Users/myusername/Projects/myProjectName/node_modules/newrelic/lib/shim/shim.js:928:17)
    at /Users/myusername/Projects/myProjectName/node_modules/newrelic/lib/shim/shim.js:913:24
    at Layer.handle_error (/Users/myusername/Projects/myProjectName/node_modules/express/lib/router/layer.js:71:5)
    at trim_prefix (/Users/myusername/Projects/myProjectName/node_modules/express/lib/router/index.js:315:13)

It appears that object-to-human-string tries to concatenate strings using the + operator, which specifically throws the above type error. It seems to me that attaching Symbol() metadata to error objects should not cause an error to be thrown.

Thanks for a great tool!

UberMouse commented 6 years ago

Hi @ki4jnq,

We will see what we can do to actually fix this and get back to you. But for now, a workaround might be to set useHumanStringForObject to false in the options object passed to init.

Regards, Taylor

miquelbeltran commented 5 months ago

The issue seems to still be present, the object-to-human-string package didn't really have a new release for 9 years.

I was able to reproduce this in unit tests:

    var builder = new MessageBuilder({ useHumanStringForObject: true });
    builder.setErrorDetails({ symbol: Symbol("Test") }); // <-- error details has Symbol
    var message = builder.build(); // <-- build will fail

And I can see the error:

 ✖ basic builder tests > humanise Symbol object string > Cannot convert a Symbol value to a string
    node_modules/object-to-human-string/lib/index.js                                                                           
    26          if (typeof value[item] === "object" && level < opts.maxLevels) {                                                      
    27                  errorString += opts.separator + item + opts.valueSeparator + "[" + toHumanString(value[item], level + 1, opts) + ']';
    28          } else {                                                                                                              
    29          errorString += opts.separator + item + opts.valueSeparator + value[item];                                            
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛                                                         
    30      }                                                                                                                     
    31  }                                                                                                                      
    32                                                                                                                         
    33  return errorString.substring(opts.separator.length);                                                                   
    type: TypeError
    tapCaught: testFunctionThrow
    toHumanString (node_modules/object-to-human-string/lib/index.js:29:63)
    RaygunMessageBuilder.setErrorDetails (lib/raygun.messageBuilder.ts:131:21)
    Test.<anonymous> (test/raygun.messageBuilder_test.js:64:13)

Probably the easier is just to build our own object to human string function.