More correct and consistent error handling.

Throw errors from sync functions, and return error in callback for async functions. Also catch errors from JSON.parse.