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

fsp-nice-error

v1.3.0

Published

A better extension for TS/JS Error Object, brings you better development experience.

Downloads

2

Readme

fsp.NiceError

An extension for TS/JS Error Object, brings you better development experience.

Features:

  • traceable, intuitional error chain
  • support extra infomation
  • zero dependency
  • support both nodejs and deno runtime

Import to your project

For Node.js

Install it first:

 # pay attention to the package name 'fsp-nice-error'
npm install fsp-nice-error
 # or
yarn add fsp-nice-error

Then import it:

// CommonJS
const { NiceError } = require('fsp-nice-error')
// ES Module or TypeScript, need bundlers(rollup/webpack/parcel...) support for current nodejs version
import { NiceError } from 'fsp-nice-error'
// TypeScript
import { NiceError } from 'fsp-nice-error/mod'

For Deno

Option 1

Just copy src/NiceError.ts file to your project, and then import it:

import { NiceError } from "path/to/NiceError.ts"

Option 2

Directly import it from remote sources:

// from denopkg
import { NiceError } from "https://denopkg.com/FullStackPlayer/fsp.NiceError@master/mod.ts"
// from deno.land
import { NiceError } from "https://deno.land/x/fsp_nice_error/mod.ts"

Usage

For better experience

Set static property execPath for NiceError will shorten the trace stack info.

// nodejs
NiceError.execPath = process.cwd()
// deno
NiceError.execPath = Deno.cwd()

Handling inner error

// a normal js Error
const err = new Error('This is a normal error')

// a NiceError which takes err as it's inner error
const ne1 = new NiceError('A normal error was caught!',{
    name: 'NiceError',
    cause: err,
    info: {
        foo: 'foo'
    }
})

// another NiceError which takes ne1 as it's inner error
const ne2 = new NiceError('An inner NiceError was caught!',{
    name: 'AppError',
    cause: ne1,
    info: {
        bar: 'bar'
    }
})

console.log(ne2.message)
/**
output:
------------------------------
An inner NiceError was caught!
------------------------------
*/

console.log(ne2.fullMessage())
/**
output:
------------------------------
[AppError]: An inner NiceError was caught! <= [NiceError]: A normal error was caught! <= [Error]: This is a normal error
------------------------------
*/

console.log(ne2.fullStack())
/**
output:
------------------------------
[AppError]: An inner NiceError was caught! <= [NiceError]: A normal error was caught! <= [Error]: This is a normal error
    at Object.<anonymous> (./sample/nodeEnv.js:16:11)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
Caused by [NiceError]: A normal error was caught! <= [Error]: This is a normal error
    at Object.<anonymous> (./sample/nodeEnv.js:7:11)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
Caused by [Error]: This is a normal error
    at Object.<anonymous> (./sample/nodeEnv.js:4:11)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
*/

console.log(ne2.fullInfo())
/**
output:
------------------------------
{
    foo: 'foo',
    bar: 'bar'
}
------------------------------
*/

Handling unexpected thown object as inner error

const err = { foo: 'bar'}
try {
    // just throw an object
    throw err
}
catch(err) {
    // a NiceError which takes the thrown object as inner err
    const ne1 = new NiceError('An object was thrown',{
        name: 'NiceError',
        cause: err
    })

    console.log(ne1.fullMessage())
    /**
    output:
    ------------------------------
    [NiceError]: An object was thrown <= [Throw]: type = object, content = {"foo":"bar"}
    ------------------------------
    */

    console.log(ne1.fullStack())
    /**
    output:
    ------------------------------
    [NiceError]: An object was thrown <= [Throw]: type = object, content = {"foo":"bar"}
        at Object.<anonymous> (./sample/nodeEnv.js:39:15)
        at Module._compile (internal/modules/cjs/loader.js:1063:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
        at Module.load (internal/modules/cjs/loader.js:928:32)
        at Function.Module._load (internal/modules/cjs/loader.js:769:14)
        at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
        at internal/main/run_main_module.js:17:47
    Caused by [Throw]: type = object, content = {"foo":"bar"}
        at Object.<anonymous> (./sample/nodeEnv.js:46:21)
        at Module._compile (internal/modules/cjs/loader.js:1063:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
        at Module.load (internal/modules/cjs/loader.js:928:32)
        at Function.Module._load (internal/modules/cjs/loader.js:769:14)
        at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
        at internal/main/run_main_module.js:17:47
    ------------------------------
    */
}

NiceError with chain property

try {
    throw new NiceError('NiceError With Chain', {
        chain: ['root','folder']
    })
}
catch(ex) {
    console.log(ex.fullMessage())
    /**
    output:
    ------------------------------
    [NiceError@root/folder]: NiceError With Chain
    ------------------------------
    */
}

API

Class NiceError

  • Static Property
    • execPath - String - script base path for replacing it from stack info
  • Property
    • name - String - error name
    • message - String - error message
    • chain - String[] - error chain array
    • info - Object - context infomation about the error
    • stack - String - stack trace of the error
  • Method
    • fullMessage() - String - get all the messages of the error chain path
    • fullStack() - String - get all the stack infomations of the error chain path
    • fullInfo() - Object - merge all the info objects in this error chain into one big info object

Enjoy it :-)