npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

auto-trace

v3.3.2

Published

A library that fixes all your stack trace problems.

Downloads

709

Readme

auto-trace

A library that fixes all your stack trace problems.

This library helps transform garbage errors into beautiful objects filled with insight and understanding. This is especially useful for apps that use an error reporting service such as bug-snag or sentry.

  • Provide a meaningful Stack Trace (for optimum error blameage)
  • Prevent irresponsible throwing of Non-Error objects (which result in Error:[object Object])
  • Attach extra context to errors
  • Encourage a consistent pattern for error handling

Resource File

return $http
  .get()
  .then()
  .catch(catchAsyncStacktrace(extraContext))

Observable

return observable
  .then()
  .catch(catchAsyncStacktrace(extraContext))

Error Life cycle

There are two parts to the error life cycle

  • Error Created new Error()
  • Error Thrown throw err

These events do not always occur at the same time. $http is an example of this. Since $http makes an async request, an error stacktrace will contain the call stack of the invoker as the request comes in (this is the syncStacktrace). Often the more useful stacktrace is the call stack as the request went out Controller->Service->Resource (this is the asyncStacktrace).

API

catchError

catchError is a wrapper around asyncStacktrace which returns a method that will handle error processing and throwing. Pass this as your onError arg in RxJS subscriptions or as a callback in catch.

RxJS Subscription example: myObs.subscribe(onComplete, catchError())

If a callback is provided then that will be called and the error will have to be thrown manually:

myObs.subscribe(onComplete, catchError((error, throwError) => {
  error.showToast = false
  throwError(error)
}))

Asynchronous Stack-Trace

The asynchronous stacktrace is often the most useful, in the case of http requests, this is the stacktrace as the request is going out.

asyncStacktrace(callback, extraContext)

Returns a function that will wrap the caught response in an error object that contains the asynchronous stacktrace. Will append extraContext and call callback with wrapped error. This should be called as a function so that return value function will be passed into the catch statement.

  • callback (optional) function that will be called with the wrapped error
  • extraContext (optional) String or Object that will be stringified and appended to the error message
return $http
  .get()
  .then()
  .catch(asyncStacktrace(callback, {state: 'extra info'}))

catchAsyncStacktrace(extraContext)

Returns a function that will wrap caught response in an error object that contains the asynchronous stacktrace. Will append extraContext and throw the wrapped error. This should be called as a function so that return value function will be passed into the catch statement (see example).

  • extraContext (optional) String or Object that will be stringified and appended to the error message

This function uses setTimeout(() => {throw err}) to throw the error.

The error will be caught be window.onerror and can be logged by reporting services like sentry and bugsnag, but will not disrupt normal code execution (and cannot be caught elsewhere within the app).

This is especially helpful when working in angular land - as throwing an error within a promise catch handler will cause a rootScope:digest Error.

return $http
  .get()
  .then()
  .catch(catchAsyncStacktrace({state: 'extra info'}))

Synchronous Stack-Trace

In the case of http requests, the synchronous stacktrace is the stacktrace as the request is response comes in. This is the normal, but less useful, stack-trace included by response errors. Often this trace follows the application function that serviced the request.

syncStacktrace

First order function, will wrap caught response in an error object that contains the asynchronous stacktrace and return the wrapped error. This should be passed (not called) as a function into the catch statement.

return $http
  .get()
  .then()
  .catch(syncStacktrace)

catchSyncStacktrace

First order function, will wrap caught response in an error object that contains the asynchronous stacktrace and throw the wrapped error. This should be passed (not called) as a function into the catch statement (see example).

This function uses setTimeout(() => {throw err}) to throw the error.

The error will be caught be window.onerror and can be logged by reporting services like sentry and bugsnag, but will not disrupt normal code execution (and cannot be caught elsewhere within the app).

This is especially helpful when working in angular land - as throwing an error within a promise catch handler will cause a rootScope:digest Error.

return $http
  .get()
  .then()
  .catch(catchSyncStacktrace)

Middleware

Looking for more useful information about your errors? Wish you had the data from both parts of the error life cycle. Look no further! Middlewares allow you to create higher order functions that will execute in both life cycle contexts.

addGlobalMiddleware(middlewareFn)

Adds global middleware function that will be called on all autoTrace errors.

Middlewares must be of the form asyncErr => syncRawErr => errToReturn

  • asyncErr is an Error object with the Async stacktrace
  • syncRawErr is the rawError passed to the handler, this could be any type of object (make sure to perform a type check).
  • errToReturn will passed as the syncRawErr to the next middleware, and finally wrapped in an error object (if needed) and thrown (or passed into a callback).

removeAllGlobalMiddlewares()

Deletes all global middleware functions.

Middleware Examples

Let's say you want to record how long it takes for a request to fail. This requires context surrounding when the error was created and when the error was thrown.

const middleware = asyncErr => {
  const startTime = new Date()
  return syncErr => {
    const errorTime = new Date() - startTime;
    if(typeof syncErr === Error)
      syncErr.message += ' -TimeToFail: ' + errorTime
    else
      syncErr = new Error(JSON.stringify(syncErr) + ' -TimeToFail: ' + errorTime)
    return syncErr
  }
}

addGlobalMiddleware(middleware);

Resource File


const extraContext = '-More info'

return $http
  .get()
  .then()
  .catch(throwAsyncStacktrace(extraContext))

This will create Error: {message: 'original error message -TimeToFail: 10s -More info', trace: ...}

installation

npm install auto-trace