MatrixAI / js-async-init

Asynchronous initialization and deinitialization decorators for JavaScript/TypeScript applications

Home Page:https://polykey.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Get a useful stack when exceptions are thrown

CMCDragonkai opened this issue · comments

Specification

The decorators currently take error objects instead of error classes.

This causes the stack trace to be set to when the error object is constructed. These stack traces are not useful for debugging.

We should replace the stack property with a stack that would be created when something goes wrong.

To do this, everywhere we are doing throw ...; we should replace it with:

errorDestroyed.stack = new Error().stack ?? '';
throw errorDestroyed;

We can wrap this with a utility function.

Additional context

Tasks

  1. create utility function for updating the stack information in the error.
  2. Update all @ready, @startstop, ect decorators to provide the useful stack information when throwing the error.
  3. add test to check if the updated stack information is provided.

I've made a utility function called updateErrorStack that updates the stack for the error and updated each decorator to use this just before throwing the errors. I've confirmed manually that we're getting a useful stack from the Error now. I don't see the need to expand or add tests for this.

Should I make a PR for this or just push it to master?

I'm thinking you can skip having a separate function, and instead just inline those 2 lines. Because that way you don't end up with an additional useless stack from the utility function.

LOC will be the same anyway.