comparison sat/plugins/plugin_misc_download.py @ 3186:84b0c8b4dee0

plugin download, aesgcm: fixed handling of HTTP errors
author Goffi <goffi@goffi.org>
date Wed, 26 Feb 2020 15:54:43 +0100
parents 9d0df638c8b4
children d92a144f3589
comparison
equal deleted inserted replaced
3185:554b3b632378 3186:84b0c8b4dee0
167 try: 167 try:
168 del self._download_callbacks[scheme] 168 del self._download_callbacks[scheme]
169 except KeyError: 169 except KeyError:
170 raise exceptions.NotFound(f"No callback registered for scheme {scheme!r}") 170 raise exceptions.NotFound(f"No callback registered for scheme {scheme!r}")
171 171
172 def errbackDownload(self, file_obj, download_d, resp):
173 """Set file_obj and download deferred appropriatly after a network error
174
175 @param file_obj(SatFile): file where the download must be done
176 @param download_d(Deferred): deffered which must be fired on complete download
177 @param resp(treq.response.IResponse): treq response
178 """
179 msg = f"HTTP error ({resp.code}): {resp.phrase.decode()}"
180 file_obj.close(error=msg)
181 download_d.errback(exceptions.NetworkError(msg))
182
172 async def downloadHTTP(self, client, uri_parsed, dest_path, options): 183 async def downloadHTTP(self, client, uri_parsed, dest_path, options):
173 url = uri_parsed.geturl() 184 url = uri_parsed.geturl()
174 185
175 head_data = await treq.head(url) 186 head_data = await treq.head(url)
176 try: 187 try:
187 ) 198 )
188 199
189 progress_id = file_obj.uid 200 progress_id = file_obj.uid
190 201
191 resp = await treq.get(url, unbuffered=True) 202 resp = await treq.get(url, unbuffered=True)
192 d = treq.collect(resp, file_obj.write) 203 if resp.code == 200:
193 d.addBoth(lambda _: file_obj.close()) 204 d = treq.collect(resp, file_obj.write)
205 d.addBoth(lambda _: file_obj.close())
206 else:
207 d = defer.Deferred()
208 self.errbackDownload(file_obj, d, resp)
194 return progress_id, d 209 return progress_id, d