Set status and headers as soon as they are known, so they are available in case of an error.