question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Throwing a custom error objects sets culprit / last stack frame to error message

See original GitHub issue

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

A project I work on (in typescript) defines custom ES6 errors as such:

export class CustomError extends Error {
  name = 'CustomError';
  constructor(...args) {
    super(...args);
    Error.captureStackTrace(this, Error);
  }
}

Now, throwing the custom error (throw new CustomError('my error message')) and inspecting the data sent (through dataCallback, we noticed that both data.culprit and the last frame in data.exception.values[0].stacktrace.frames both point to the filename CustomError my error message, with the file which throws the error being the second last entry in the stacktrace.

In comparison, throwing a standard error yields a data.culprit and last stacktrace frame both pointing to whichever file threw the error.

What is the expected behavior?

Custom errors should be handled the same way as standard errors. We believe the impact of this is that it prevents proper grouping of those custom errors, and we went around this by popping the last stacktrace frame and overriding the culprit in the dataCallback when this scenario is detected.

Using Raven.JS 3.22.1, installed through NPM.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kamilogorekcommented, Feb 9, 2018

Hey @vicrep, this should be covered by TraceKit that we use. However, I see that you’re not creating CustomError correctly (you are missing prototype chain assignments). Could you give this code a shot and let me know if you still have the same issue? (also, please provide stacktrace and culprit screenshots if possible).

export class SentryError extends Error {
  public name: string;

  constructor(public message: string) {
    super(message);
    this.name = new.target.prototype.constructor.name;
    Object.setPrototypeOf(this, new.target.prototype);
  }
}
1reaction
vicrepcommented, Feb 13, 2018

@kamilogorek thanks for getting back to me.

So I took the time to try your suggested fix, and ended up discovering what was causing the actual issue.

Prototype chain assignments were actually being handled by the typescript compiler, so doing Object.setPrototypeOf(this, new.target.prototype); wasn’t necessary.

In my issue request, I wrote CustomError as a generic example, but in fact, the custom error I was actually using in my project was called HttpError. Further investigation led me to discover that having the class name begin with Http was actual the culprit, and causes sentry’s stack trace collection (i.e. tracekit?) to fail.

Here’s an example, where I have two custom errors, declared in the same file, which share the exact same implementation – one is named HttpError, the other HttError (no p).

Throwing HttpError somewhere in my project, this is the culprit and last stack frames I get from sentry’s dataCallback: screen shot 2018-02-09 at 10 25 46 am screen shot 2018-02-09 at 10 26 05 am

Now, throwing the error HttError from the exact same place, here’s what I get: screen shot 2018-02-09 at 10 27 32 am screen shot 2018-02-09 at 10 27 47 am

So it seems something is happening in the pipeline when errors’ class names begin with Http

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom errors, extending Error - The Modern JavaScript Tutorial
JavaScript allows to use throw with any argument, so technically our custom error classes don't need to inherit from Error .
Read more >
Error.prototype.stack - JavaScript - MDN Web Docs - Mozilla
The non-standard stack property of an Error instance offers a trace of which functions were called, in what order, from which line and...
Read more >
Don't Be Afraid of the JavaScript Stack Trace - DigitalOcean
A beginner-friendly overview of the JavaScript stack trace and tips on how to use it.
Read more >
Errors | Kibana Guide [master] - Elastic
Errors are groups of exceptions with a similar exception or log message. The Errors overview provides a high-level view of the exceptions that...
Read more >
JavaScript Errors: Anatomy of the Error | Bugsnag Blog
prototype.stack property contains the stacktrace for the error. The stacktrace is stored on the error as a simple string where each function in ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found