Skip to content

educate people on how to add custom exceptions #324

Closed
@kevinburkeshyp

Description

@kevinburkeshyp

So, borrowed from somewhere on the Internet, we were doing custom exceptions that looked something like this:

var ValueError;

ValueError = (function() {
  function ValueError() {}
  ValueError.prototype = Object.create(Error.prototype);
  return ValueError;
})();

module.exports = ValueError;

Unfortunately if you call sentry.client.captureError(new ValueError("foobar")), Sentry doesn't have the stack trace.

Instead you want something like this

var ValueError;

ValueError = (function() {
  function ValueError(message) {
    this.message = message;
    this.stack = new Error(this.message).stack;
  }
  ValueError.prototype = Object.create(Error.prototype);
  return ValueError;
})();

module.exports = ValueError;

It's pretty hacky (well, subclassing exceptions in general is a shit show), but creating a fake error and copying the stack will set the stack trace properly.

Was thinking raven could take the lead here and actually document the right way to create/subclass exceptions so this kind of data doesn't get lost / your Sentry data is more useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions