Skip to content

Latest commit

ย 

History

History

handle-rejection

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

handle-rejection

๐Ÿ’€ Handle unhandled rejections

Built in single handlers

require('handle-rejection').console(); // console.error(error)
Name Details
console console.error
exit process.exit
throw throws error
collect Prints all errors before process exists

Combine handlers

require('handle-rejection')(
	'console',
	'collect'
);

This one is a good combination for long test suits (see below) โ€ 

Custom handlers

const errobj = require('errobj');

require('handle-rejection')(
	error => fetch(
		'/report-error',
		{
			method: 'POST',
			body: JSON.stringify(errobj(error))
		}
	)
);

Combined handlers: built in and custom

require('handle-rejection')(
	error => Raven.captureException(error) // custom
	'exit', // built in
);

โ€  Example for a combination of "console" and "collect" in a testing environment

> mocha

  some function
    โœ“ Should be okay
TypeError: Cannot read property 'ok' of undefined
    at ok (/workspace/package/spec.js:21:11)
    at tryCatch (/workspace/package/node_modules/regenerator-runtime/runtime.js:45:40)
    at Generator.invoke [as _invoke] (/workspace/package/node_modules/regenerator-runtime/runtime.js:271:22)
    at Generator.prototype.(anonymous function) [as next] (/workspace/package/node_modules/regenerator-runtime/runtime.js:97:21)
    at asyncGeneratorStep (/workspace/package/spec.js:14:103)
    at _next (/workspace/package/spec.js:16:194)
    at process._tickCallback (internal/process/next_tick.js:68:7)

  some other function
    โœ“ Should success
    โœ“ Should also success
    โœ“ Should really work

  another bulk of tests
    โœ“ Should be fine
    โœ“ Should pass the test

  6 passing (110ms)

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ Unhandled Rejection Summary โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

โ—๏ธ Rejection 1:
TypeError: Cannot read property 'ok' of undefined
    at ok (/workspace/package/spec.js:21:11)
    at tryCatch (/workspace/package/node_modules/regenerator-runtime/runtime.js:45:40)
    at Generator.invoke [as _invoke] (/workspace/package/node_modules/regenerator-runtime/runtime.js:271:22)
    at Generator.prototype.(anonymous function) [as next] (/workspace/package/node_modules/regenerator-runtime/runtime.js:97:21)
    at asyncGeneratorStep (/workspace/package/spec.js:14:103)
    at _next (/workspace/package/spec.js:16:194)
    at process._tickCallback (internal/process/next_tick.js:68:7)