Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle exceptions raised by request.urlopen in HttpClient.post #17

Merged
merged 1 commit into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
bohan-amplitude marked this conversation as resolved.
Show resolved Hide resolved
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