Skip to content

Commit

Permalink
fix: Handles exceptions raised by request.urlopen in `HttpClient.po…
Browse files Browse the repository at this point in the history
…st` (#17)
  • Loading branch information
Sija authored Apr 21, 2022
1 parent e819e68 commit 4150ea0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/amplitude/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from enum import Enum
from socket import timeout
from typing import Optional
from urllib import request, response
from urllib import request, response, error

from amplitude import constants

Expand Down Expand Up @@ -134,4 +134,14 @@ def post(url: str, payload: bytes, header=None) -> Response:
except timeout:
result.code = 408
result.status = HttpStatus.TIMEOUT
except error.HTTPError as e:
try:
result.parse(e)
except:
result = Response()
result.code = e.code
result.status = Response.get_status(e.code)
result.body = {'error': e.reason}
except error.URLError as e:
result.body = {'error': str(e.reason)}
return result
2 changes: 1 addition & 1 deletion src/amplitude/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def process_response(self, res, events):
self.push_to_storage(events_for_retry_delay, 30000, res)
self.push_to_storage(events_for_retry, 0, res)
else:
self.callback(events, res.code, "Unknown error")
self.callback(events, res.code, res.error or "Unknown error")

def push_to_storage(self, events, delay, res):
for event in events:
Expand Down

0 comments on commit 4150ea0

Please sign in to comment.