All Projects → sindresorhus → P Catch If

sindresorhus / P Catch If

Licence: mit
Conditional promise catch handler

Programming Languages

javascript
184084 projects - #8 most used programming language

p-catch-if

Conditional promise catch handler

Useful for handling only some types of errors and let the rest pass through.

Install

$ npm install p-catch-if

Usage

const pCatchIf = require('p-catch-if');

// Error constructor
getData().catch(pCatchIf(TimeoutError, () => retry(getData)));

// Multiple error constructors
getData().catch(pCatchIf([NetworkError, TimeoutError], () => retry(getData)));

// Boolean
getData().catch(pCatchIf(isProduction, error => recordError(error)));

// Function
const hasUnicornMessage = error => error.message.includes('unicorn');
getData().catch(pCatchIf(hasUnicornMessage, console.error));

// Promise-returning function
const handler = error => sendError(error).then(checkResults);
getData().catch(pCatchIf(handler, console.error));

// Can also be nested
const validateMessage = error => error.message === 'Too many rainbows';
getData().catch(pCatchIf(UnicornError, pCatchIf(validateMessage, console.error)));

API

pCatchIf(predicate, catchHandler)

Returns a thunk that returns a Promise.

predicate

Type: Error.constructor Error.constructor[] boolean Function -> Promise<boolean>|boolean

Specify either an error constructor, array of error constructors, boolean, or function that returns a promise for a boolean or a boolean.

If the function returns a promise, it's awaited.

catchHandler

Type: Function

Called if predicate passes.

This is what you would normally pass to .catch().

Related

  • p-if - Conditional promise chains
  • p-tap - Tap into a promise chain without affecting its value or state
  • p-log - Log the value/error of a promise
  • More…

License

MIT © Sindre Sorhus

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].