catch-safe
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

catch-safe

Inspired by and adapted from the example code in this article by Kent C. Dodds

Since TypeScript 4.4, catch clause variables default to the unknown type. This library provides a zero-dependency utility function which allows you to extract the error message in a type-safe manner when the variable is of type Error.

Installation

Install the package in your project:

# NPM
npm install catch-safe

# Yarn
yarn add catch-safe

# PNPM
pnpm add catch-safe

Usage

Import the getErrorMessage function from the catch-safe module and use it to get the error message (if it exists) from a catch block variable :

import { getErrorMessage } from 'catch-safe'

async function someAsyncFunction() {
  try {
    await runAsyncTask()
  } catch (err) {
    throw new Error(getErrorMessage(err))
  }
}

Readme

Keywords

none

Package Sidebar

Install

npm i catch-safe

Weekly Downloads

12

Version

0.1.1

License

MIT

Unpacked Size

4.61 kB

Total Files

5

Last publish

Collaborators

  • tundera