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

@twreporter/errors

v1.1.2

Published

[![NPM version](https://img.shields.io/npm/v/@twreporter/core.svg)](https://www.npmjs.com/package/@twreporter/errors)

Downloads

297

Readme

@twreporter/errors

NPM version

This package provides the helpers for handling errors in the The Reporter website.

Inspired by package errors for Go language and the related article by Dave Cheney.

Reference:

Use

errors.helpers.wrap

Annotate the error with name, message and payload. It will return a new error that is an instance of errors.AnnotatingError.

See source code and JSDoc for all parameters

Example:

import errors from '@twreporter/errors'

function doSomething() {
  try {
    const someArguments = {
      /* ... */
    }
    invokeSomethingWithError(...someArguments)
  } catch (error) {
    console.error(
      errors.helpers.wrap(
        error,
        'ErrorType',
        'failed to invoke something',
        { args: someArguments },
        invokeSomethingWithError
      )
    )
  }
}

errors.helpers.unwrap

It will return the previous error in the chain that the input error belongs to.

If there is no pointer of previous error saved in the input error, it will return null.

It works like the Unwrap method of errors in Go@^1.13.

See source code and JSDoc for all parameters

Example:

import errors from '@twreporter/errors'

function doSomething() {
  try {
    const someArguments = {
      /* ... */
    }
    invokeSomethingWithError(...someArguments)
  } catch (error) {
    /* list all error.name in errors chain */
    let _error = error
    const chain = []
    while (_error) {
      chain.push(_error)
      _error = errors.helpers.unwrap(_error)
    }
    /* ... handle the chain */
  }
}

errors.helpers.cause

It will return the earliest error in the chain that the input error belongs to.

See source code and JSDoc for all parameters

import errors from '@twreporter/errors'

function nestedFailedTask() {
  throw new Error('nested failure')
}

function invokeSomething() {
  const someArguments = { bar: 'bar' }
  try {
    nestedFailedTask()
  } catch (error) {
    throw errors.helpers.wrap(
      error,
      'ErrorType',
      'failed to invoke nested task',
      { args: someArguments }
    )
  }
}

function doSomething() {
  const someArguments = { foo: 'foo' }
  try {
    invokeSomething(someArguments)
  } catch (error) {
    const annotatingError = errors.helpers.wrap(
      error,
      'ErrorType',
      'failed to invoke something',
      { args: someArguments }
    )
    errors.helpers.cause(annotatingError) === 'nested failure' /* true */
  }
}

log out:

ErrorType: failed to invoke something
    at doSomething (/test.js:12:44)
  payload {"args":{"foo":"foo"}}

errors.helpers.printAll

See source code and JSDoc for all parameters

import errors from '@twreporter/errors'

function nestedFailedTask() {
  throw new Error('nested failure')
}

function invokeSomething() {
  const someArguments = { bar: 'bar' }
  try {
    nestedFailedTask()
  } catch (error) {
    throw errors.helpers.wrap(
      error,
      'ErrorType',
      'failed to invoke nested task',
      { args: someArguments }
    )
  }
}

function doSomething() {
  const someArguments = { foo: 'foo' }
  try {
    invokeSomething(someArguments)
  } catch (error) {
    const annotatingError = errors.helpers.wrap(
      error,
      'ErrorType',
      'failed to invoke something',
      { args: someArguments }
    )
    const message = errors.helpers.printAll(annotatingError, {
      withStack: true,
      withPayload: true,
    })
    console.error(message)
  }
}

doSomething()

log out:

Error: nested failure
 at nestedFailedTask (/test.js:4:9)
ErrorType: failed to invoke nested task
 at invokeSomething (/test.js:12:26)
 > payload {"args":{"bar":"bar"}}
ErrorType: failed to invoke something
 at doSomething (/test.js:21:44)
 > payload {"args":{"foo":"foo"}}

errors.helpers.printOne

See source code and JSDoc for all parameters

import errors from '@twreporter/errors'

function invokeSomethingWithError() {
  throw new Error('nested error')
}

function doSomething() {
  const someArguments = { foo: 'foo' }
  try {
    invokeSomethingWithError(someArguments)
  } catch (error) {
    const annotatingError = errors.helpers.wrap(
      error,
      'ErrorType',
      'failed to invoke something',
      { args: someArguments }
    )
    const message = errors.helpers.printOne(annotatingError, {
      withStack: true,
      withPayload: true,
    })
    console.error(message)
  }
}

doSomething()

log out:

ErrorType: failed to invoke something
 at doSomething (/test.js:12:44)
 > payload {"args":{"foo":"foo"}}

errors.helpers.annotateAxiosError

See source code and JSDoc for all parameters

import errors from '@twreporter/errors'
import axios from 'axios'

async function doSomething() {
  try {
    const someArguments = {
      /* ... */
    }
    await axios.get(someArguments)
  } catch (axiosError) {
    throw errors.helpers.annotateAxiosError(axiosError)
  }
}

errors.AnnotatingError

public properties

  • name {string} the name of this error
  • message {string} the message of this error
  • payload {Object} the context information for better debugging
  • stack {string} the stack trace from where the error be constructed. it contains the name and message of the error in the V8 engine

See source code and JSDoc for all parameters

Development

Dev

make dev

Build

make build

Publish

make publish