New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

blooper

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

blooper

Handle all your errors in one place.

  • 0.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Blooper

If you've written Node, you've written this:

doSomething((error, data) => {
	if (error) {
		// handle error
	} else {
		// handle data
	}
});

With Blooper, you write one error handler, and your code looks like this:

doSomething((error, data) => {
	blooper.handle(error).then(() => {
		// handle data
	});
});

This makes it easy to surface errors in the UI, log them and continue, or crash when appropriate.

Usage

There are three things to know:

  • To initialize Blooper, require it and pass it an error handler.
  • The returned object has a handle method which will resolve a promise if the error is falsy, or else handle the error.
  • There is also an attempt method that wraps try-catch (see below).
let response;

const http = require('http'),
	blooper = require('blooper')((error, status = 500) => {
		// Put your error handling logic here
		console.error(error);
		response.writeHead(status, 'text/html; charset=UTF-8');
		response.end(error.stack || error);
	});

http.createServer((req, res) => {

	response = res;

	doSomething((err, data) => {
		blooper.handle(err).then(() => {
			// use data
		});
	});

	// You can wrap try/catch too!
	// By default, Blooper will catch using your error handler
	blooper.attempt(() => {
		doSomethingDangerous();
	});

	// Or you can pass a custom catcher
	blooper.attempt(() => {
		doSomethingDangerous();
	}, err => {
		// handle error here
	});

}).listen(3000);

Using Blooper where you don't have a res object

The above examples only work because Blooper is configured in the same file that accepts requests. In order to use Blooper in other modules in your app, you need to give it access to the res object (assuming your error handler sends responses sometimes). For that, you have two options:

Attach Blooper to the request

Most apps pass around the request object already, so you can stick Blooper on there.

let response;

const http = require('http'),
	blooper = require('blooper')((error, status = 500) => {
		// Put your error handling logic here
		console.error(error);
		response.writeHead(status, 'text/html; charset=UTF-8');
		response.end(error.stack || error);
	}),
	doSomething = require('./doSomething');

http.createServer((req, res) => {

	response = res;

	req.blooper = blooper;

	doSomething(req);

}).listen(3000);
Make Blooper global

Globals are generally a last resort, but I'll let you make an exception for Blooper, since it's really intended to be global in nature - its use is to replace language constructs, after all.

let response;

const http = require('http'),
	blooper = require('blooper')((error, status = 500) => {
		// Put your error handling logic here
		console.error(error);
		response.writeHead(status, 'text/html; charset=UTF-8');
		response.end(error.stack || error);
	});

// This makes the global.handle method available in all modules
global.handle = blooper.handle;

http.createServer((req, res) => {

	// ...

}).listen(3000);

Keywords

FAQs

Package last updated on 25 Nov 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc