Better javascript handling of errors