Exit or throw error immediately.
npm install panicitpanicit is a small library that could let you exit or throw error with helpful
messages. It works in both browser and Node.js.
``shell`
npm i panicit
`ts
import {panic} from 'panicit'
panic('some error')
`
It will print
`txt`
some reason
Uncaught Error: some reason
at n (
at
with cause
You can provide the cause as well.
`ts
import {panic} from 'panicit'
panic('some error', {cause: 'some cause'})
`
It will print
`txt`
some reason
[Cause] some cause
Error: some reason
at n (
at
If you are using Node.js, you can also provide exit code.
`ts
import {panic} from 'panicit'
panic('some error', {cause: 'some cause', exitCode: 2})
`
Result
`txt
ā node
> panic('some reason', {cause: 'some cause', exitCode: 2})
some reason
[Cause] some cause
ā echo $?
2
`
By default, panic will exit the program in Node.js, but you can set exit
option to false to disable this behavior.
`ts`
panic('some error', {exit: false})
> Note that exit program can only be used in Node environment.
You can define whether should the program exit by default or not by using
definePanicitConfig.
`ts
import {definePanicitConfig} from 'panicit'
definePanicitConfig({exit: false})
// this won't exit the program
panic('some error')
// you can still exit the program by set exit to true explicitly`
panic('some error', {exit: true})
Inline panic option, used as the second parameter of panic function.
`ts`
type PanicOption = {
/* Note the cause of why the error is triggered. /
cause?: any
/* Should log panic message. /
silent?: boolean
/* Only works in Node.js env. /
exit?: boolean
/* Only works in Node.js env. /
exitCode?: number
}
Global panic config.
`ts``
type PanicConfig = {
/* Should log panic message. /
silent?: boolean
/* Only works in Node.js env. /
exit?: boolean
/* Only works in Node.js env. /
exitCode?: number
}