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

@grimen/mybad

v0.3.2

Published

My friendly error base class - for Node/JavaScript.

Downloads

140

Readme

js-mybad NPM version Build Status Coverage Status

My friendly error base class - for Node/JavaScript.

Introduction

One in general always needs a application/library specific error base class, but the native errors are very limited in what meta/debugging information they can hold at time they are raised. For better debugging and error reporting/inspection this error base class allows to attach some additonal error context information that can be used to better understand the issue - without having to create custom error formatters, or run debugger.

Install

Install using npm:

$ npm install @grimen/mybad

Install using yarn:

$ yarn add @grimen/mybad

Use

Very basic example:

const mybad = require('@grimen/mybad')

class ToMuchError extends mybad.Error {}

function printMoney(stash) {
    try {
        if (typeof stash === 'string' && stash.length > 13) {
            throw new RangeError(`Too much money to print: ${stash}`)
        }

        console.log(`PRINT ${stash.join('')}`)

    } catch (error) {
        throw new ToMuchError(error, {
            message: 'Out of money printing ink...',
            id: Date.now(), // well, should maybe be unique...
            key: 'too_much',
            code: 400,
            details: {
                stash,
            },
        })
    }
}

let amount = ''

const range = (to) => {
    return [...Array(to).keys()]
}

for (let dollar of range(42)) {
    amount += '$'

    try {
        printMoney(amount)

    } catch (error) {
        process.env.ERROR_VERBOSE = false
        process.env.ERROR_COLORS = false

        console.log()
        console.log('========================================================')
        console.log('     error.toString({verbose: false})')
        console.log('-----------------------------------------------------')
        console.log()
        console.log(error.toString({verbose: false}))
        console.log()

        console.log()
        console.log('========================================================')
        console.log('     error.toString({verbose: true})')
        console.log('-----------------------------------------------------')
        console.log()
        console.log(error.toString({verbose: true}))
        console.log()

        console.log()
        console.log('========================================================')
        console.log('     error.toJSON()')
        console.log('-----------------------------------------------------')
        console.log()
        console.log(error.toString({verbose: true}))
        console.log()

        throw error
    }
}

Run this with optional environment variables COLORS / ERROR_COLORS and/or VERBOSE / ERROR_VERBOSE set too truthy or falsy values, so see various error info formatting in terminal. Also (NO_COLOR)[https://no-color.org] is respected.

Something like this (imagine some colorized formatting):

PRINT $
PRINT $$
PRINT $$$
PRINT $$$$
PRINT $$$$$
PRINT $$$$$$
PRINT $$$$$$$
PRINT $$$$$$$$
PRINT $$$$$$$$$
PRINT $$$$$$$$$$
PRINT $$$$$$$$$$$
PRINT $$$$$$$$$$$$
PRINT $$$$$$$$$$$$$


========================================================
     error.toString({verbose: false})
-----------------------------------------------------

Out of money printing ink...


========================================================
     error.toString({verbose: true})
-----------------------------------------------------

Out of money printing ink... { stash: '$$$$$$$$$$$$$$' }


========================================================
     error.toJSON()
-----------------------------------------------------

{
  type: 'ToMuchError',
  id: 1618487081556,
  code: 400,
  key: 'too_much',
  message: 'Out of money printing ink...',
  details: { stash: '$$$$$$$$$$$$$$' },
  stack: [
    {
      file: '/Users/jonas/Dev/Private/js-mybad/examples/basic.js',
      function: 'printMoney',
      line: 23,
      column: 19,
      source: undefined
    },
    {
      file: '/Users/jonas/Dev/Private/js-mybad/examples/basic.js',
      function: 'Object.<anonymous>',
      line: 57,
      column: 9,
      source: undefined
    },
    {
      file: 'internal/modules/cjs/loader.js',
      function: 'Module._compile',
      line: 1063,
      column: 30,
      source: undefined
    },
    {
      file: 'internal/modules/cjs/loader.js',
      function: 'Object.Module._extensions..js',
      line: 1092,
      column: 10,
      source: undefined
    },
    {
      file: 'internal/modules/cjs/loader.js',
      function: 'Module.load',
      line: 928,
      column: 32,
      source: undefined
    },
    {
      file: 'internal/modules/cjs/loader.js',
      function: 'Function.Module._load',
      line: 769,
      column: 14,
      source: undefined
    },
    {
      file: 'internal/modules/run_main.js',
      function: 'Function.executeUserEntryPoint [as runMain]',
      line: 72,
      column: 12,
      source: undefined
    },
    {
      file: '17',
      function: 'internal/main/run_main_module.js',
      line: 47,
      column: undefined,
      source: undefined
    }
  ]
}

Test

Clone down source code:

$ make install

Run colorful tests using jest:

$ make test

Related

About

This project was mainly initiated - in lack of solid existing alternatives - to be used at our work at Markable.ai to have common code conventions between various programming environments where Node.js (for I/O heavy operations) is heavily used.

License

Released under the MIT license.