Skip to content

Instantly share code, notes, and snippets.

@brianleroux
Last active February 5, 2017 05:22
Show Gist options
  • Save brianleroux/33ed82ee20aa0f4b11d630a9d423b2df to your computer and use it in GitHub Desktop.
Save brianleroux/33ed82ee20aa0f4b11d630a9d423b2df to your computer and use it in GitHub Desktop.
Exceptional circumstances: preventing programmer failure at runtime
var assert = require('assert')
function add(a, b) {
assert(typeof a === 'number', 'a is not a number got ' + typeof a)
assert(typeof b === 'number', 'b is not a number got ' + typeof b)
return a + b
}
add(1, 2)
add('1', 2)#! node node-add.js
/* logs:
AssertionError: a is not a number got string
at add (/Users/brianleroux/Repo/33ed82ee20aa0f4b11d630a9d423b2df/node-add.js:4:3)
at Object.<anonymous> (/Users/brianleroux/Repo/33ed82ee20aa0f4b11d630a9d423b2df/node-add.js:11:1)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:134:18)
at node.js:962:3
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment