Skip to content

Instantly share code, notes, and snippets.

@amtal
Created July 14, 2011 20:31
Show Gist options
  • Save amtal/1083364 to your computer and use it in GitHub Desktop.
Save amtal/1083364 to your computer and use it in GitHub Desktop.
Erlang early-returns via syntax sugar rather than throw-catch.
;; ([{atom(),binary()}], pid()) -> {missing,atom()}
;; | {bad_value,atom()}
;; | {unescaped,|atom()}
;; | malicious
;; | ok.
(defn sanitize [args logger-pid]
(ablock user-err
; check for user screwing up the input, inform them
(check-missing args)
(if (/= '() it)
(return-from user-err (tuple 'missing it)))
(proplists:get_value 'id args)
(if (not-numeric? it)
(return-from user-err (tuple 'bad_value 'id)))
(proplists:get_value 'email args)
(if (invalid-email? it)
(return-from user-err (tuple 'bad_value 'email)))
; check for malicious-looking screwups, don't inform
; them and log to the user's logging process
(ablock malicious
(orddict:map (fun check-sql-escapes 1) args)
(orddict:filter (cut /= 'ok <>) it)
(if (== 'email (element 1 it))
(return-from user-err (tuple 'unescaped 'email))
(return-from malicious it))
(if (size-too-big? args)
(return-from malicious 'size_dos))
'ok)
(if (/= 'ok it)
(progn (: user_logger malicious logger-pid it)
(return-from user-err 'malicious)))
(tuple 'ok args)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment