📦
unwrapit
  • 👋Welcome to unwrapit
  • Intro
    • 💡Why use it?
    • ✨Getting Started
  • APIs
    • 🔨wrap
    • 🔨unwrap
    • 🔨unwrapOr
    • 🔨unwrapOrElse
    • 🔨expect
    • 🔨mapErr
    • 🔨defineWrapConfig
    • 🔨ok
    • 🔨err
  • Type
    • 🔧Result
    • 🔧WrapConfig
    • 🔧WrapOption
    • 🔧Panic
  • Recipe
    • 🎨Export Wrapped Functions
    • 🎨Return Result Without `wrap` it
Powered by GitBook
On this page
  • Set Panic By Default
  • Customize Panic Function
  1. APIs

defineWrapConfig

Set Panic By Default

By default, when a user trying to unwrap a wrapped function, unwrapit will throw an error no matter in a browser or in Node.js. However, you may want the Node program to exit when an error occurs.

One way is that you can use the {panic: true} option

(await wrap(Promise.reject('error'))).unwrap({panic: true})

If you want the program to exit by default, then can set the panic by default

import {defineWrapConfig} from 'unwrapit'

defineWrapConfig({panic: true})

Customize Panic Function

Sometimes, you may want to have a central place to handle all the errors, you can set your do this simply by providing your own panic function which conforms to the below signature.

(message: any, opt?: {cause?: any; shouldExit?: boolean; exitCode?: number}) => never

Here's an example

import {defineWrapConfig} from 'unwrapit'

class MyError extends Error {}

function myPanic(message: any): never {
  throw new MyError(message)
}

defineWrapConfig({panicFn: myPanic})
PreviousmapErrNextok

Last updated 1 year ago

🔨