Skip to content

Instantly share code, notes, and snippets.

View indexzero's full-sized avatar
🌎
Always bet on Open Source

Charlie Robbins indexzero

🌎
Always bet on Open Source
View GitHub Profile

Node Error Handling and Domains

"Occurrences in this domain are beyond the reach of exact prediction because of the variety of factors in operation, not because of any lack of order in nature." - Albert Einstein

"Master of my domain" - Larry David

Error handling in an asynchronous language works in a unique way and presents many challenges, some unexpected. This tutorial reviews the various error handling patterns available in node, and explains how domains can be used to improve it.

There are four main error handling patterns in node:

  • Error return value
@indexzero
indexzero / range-error-ftw.js
Created November 28, 2012 09:41 — forked from dherman/range-error-ftw.js
fun with JS arrays
var a = [];
a[Math.pow(2, 32) - 2] = "max index"; // highest non-expando indexed property
console.log(a.length === Math.pow(2, 32) - 1); // true
try {
a.push("whoa", "EVEN MOAR WHOA");
} catch (e) {
console.log(e instanceof RangeError); // true
}
@indexzero
indexzero / index.html
Created October 24, 2012 08:31 — forked from dfm/LICENSE
XKCD-style plots in d3
<!DOCTYPE HTML>
<html>
<head>
<title>XKCD plots in d3</title>
<script src="http://d3js.org/d3.v2.min.js?2.10.0"></script>
<script src="xkcd.js"></script>
<style>
@indexzero
indexzero / LICENSE.txt
Created August 15, 2012 02:34 — forked from aemkei/LICENSE.txt
Lorenz Attractor - 140byt.es
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
@indexzero
indexzero / gist:3299220
Created August 8, 2012 22:04 — forked from Marak/gist:3240974
WIP - Flatironisms

WIP

log all incoming http requests

app.http.before.push(function(req, res){
  app.log.info(req.url);
  res.emit('next');
})
@indexzero
indexzero / seal.js
Created July 10, 2012 03:01 — forked from zaach/seal.js
#!/usr/bin/env node
/*
* This module can verify that packages installed during development are
* identical to those installed during deployment. The standard npm shrinkwrap
* only ensures that package versions are the same, but does not verify contents.
* This module checks the shasum of the package tarballs downloaded by npm during
* development and deployment to ensure they are the same.
*
* Usage:
@indexzero
indexzero / node-static-test.js
Created June 30, 2012 05:33 — forked from phstc/node-static-test.js
vows test/integration/node-static-test.js --spec
var vows = require('vows')
, request = require('request')
, assert = require('assert')
, httpMocks = require('node-mocks-http')
static = require('../../lib/node-static');
var fileServer = new(static.Server)(__dirname + '/../fixtures', {});
var suite = vows.describe('file-server.js');
@indexzero
indexzero / example-user.js
Created May 4, 2012 16:46 — forked from nijikokun/example-user.js
Beautiful Validation... Why have I never thought of this before?!
var user = {
validateCredentials: function (username, password) {
return (
(!(username += '') || username === '') ? { error: "No Username Given.", field: 'name' }
: (!(username += '') || password === '') ? { error: "No Password Given.", field: 'pass' }
: (username.length < 3) ? { error: "Username is less than 3 Characters.", field: 'name' }
: (password.length < 4) ? { error: "Password is less than 4 Characters.", field: 'pass' }
: (!/^([a-z0-9-_]+)$/i.test(username)) ? { error: "Username contains invalid characters.", field: 'name' }
: false
);
@indexzero
indexzero / gist:2331488
Created April 7, 2012 19:18 — forked from mikeal/gist:2331127
safe .toJSON()
function toString (obj) {
function _toString (i) {
if (obj[i] === null) return 'null'
if (typeof obj[i] === 'undefined') return 'undefined'
if (obj[i].toString) return obj[i].toString()
else return ''
}
return _toString
}
@indexzero
indexzero / app.js
Created February 25, 2012 06:29 — forked from jfhbrook/app.js
THE FRAMEWORK YOUR FRAMEWORK COULD CODE LIKE
var flatiron = require('flatiron'),
app = flatiron.app;
console.log('What\'s that in your hand? Look down,');
app.use(require('./tickets'));
console.log('look up. I HAVE IT. It\'s ' + app.qty + ' TICKETS to ' + app.description + '!');
console.log('Look again.');
app.use(require('./diamonds'));