Use err.status==='invalid' instead of checking each err.code-- more concise.  Still could be improved further.