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:
npm install fsp-nice-error
yarn add fsp-nice-error
Then import it:
const { NiceError } = require('fsp-nice-error')
import { NiceError } from 'fsp-nice-error'
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:
import { NiceError } from "https://denopkg.com/FullStackPlayer/fsp.NiceError@master/mod.ts"
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.
NiceError.execPath = process.cwd()
NiceError.execPath = Deno.cwd()
Handling inner error
const err = new Error('This is a normal error')
const ne1 = new NiceError('A normal error was caught!',{
name: 'NiceError',
cause: err,
info: {
foo: 'foo'
}
})
const ne2 = new NiceError('An inner NiceError was caught!',{
name: 'AppError',
cause: ne1,
info: {
bar: 'bar'
}
})
console.log(ne2.message)
console.log(ne2.fullMessage())
console.log(ne2.fullStack())
console.log(ne2.fullInfo())
Handling unexpected thown object as inner error
const err = { foo: 'bar'}
try {
throw err
}
catch(err) {
const ne1 = new NiceError('An object was thrown',{
name: 'NiceError',
cause: err
})
console.log(ne1.fullMessage())
console.log(ne1.fullStack())
}
NiceError with chain property
try {
throw new NiceError('NiceError With Chain', {
chain: ['root','folder']
})
}
catch(ex) {
console.log(ex.fullMessage())
}
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 :-)