Better error handling in js