Skip to content

Instantly share code, notes, and snippets.

@shesek
Created October 20, 2012 07:48
Show Gist options
  • Save shesek/3922578 to your computer and use it in GitHub Desktop.
Save shesek/3922578 to your computer and use it in GitHub Desktop.
Throwing early errors for unexpected empty values
# A quick way to assert that you get a non-null-or-undefined value when you don't expect ONE, and throw early
# errors when it does happen, rather than letting that value go elsewhere.
# Just a simple function that returns the value when its non-null-or-undefined, or throws an error otherwise..
non_empty = (v) -> v ? (throw new Error 'null or undefined encountered where it shouldn\'t')
# (the extra parenthesis are there to make CoffeeScript compilation only execute an IIFE when we're throwing)
# Than, simply:
user_id = non_empty user.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment