handle http errors and prevent empty file from being created when download fails