Skip to content

Commit

Permalink
improve: add exit codes for upload connection/timeout/HTTPauthorizati…
Browse files Browse the repository at this point in the history
…on errors respectively (#610)

* improve: add exit codes for upload connection/timeout/HTTPauthorization errors respectively

* refactor
  • Loading branch information
Tao Peng authored Mar 18, 2023
1 parent 3274a50 commit fe5009b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
6 changes: 2 additions & 4 deletions mapillary_tools/api_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,15 @@ def fetch_organization(
]


def logging(
access_token: str, action_type: ActionType, properties: T.Dict
) -> requests.Response:
def logging(action_type: ActionType, properties: T.Dict) -> requests.Response:
resp = requests.post(
f"{MAPILLARY_GRAPH_API_ENDPOINT}/logging",
json={
"action_type": action_type,
"properties": properties,
},
headers={
"Authorization": f"OAuth {access_token}",
"Authorization": f"OAuth {MAPILLARY_CLIENT_TOKEN}",
},
timeout=REQUESTS_TIMEOUT,
)
Expand Down
12 changes: 12 additions & 0 deletions mapillary_tools/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,15 @@ def __init__(
self.desc = desc
self.distance = distance
self.angle_diff = angle_diff


class MapillaryUploadConnectionError(MapillaryUserError):
exit_code = 12


class MapillaryUploadTimeoutError(MapillaryUserError):
exit_code = 13


class MapillaryUploadUnauthorizedError(MapillaryUserError):
exit_code = 14
49 changes: 35 additions & 14 deletions mapillary_tools/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,15 +486,14 @@ def _show_upload_summary(stats: T.Sequence[_APIStats]):
LOG.info("%8.1fs upload time", summary["time"])


def _api_logging_finished(user_items: types.UserItem, summary: T.Dict):
def _api_logging_finished(summary: T.Dict):
if MAPILLARY_DISABLE_API_LOGGING:
return

action: api_v4.ActionType = "upload_finished_upload"
LOG.debug("API Logging for action %s: %s", action, summary)
try:
api_v4.logging(
user_items["user_upload_token"],
action,
summary,
)
Expand All @@ -508,7 +507,7 @@ def _api_logging_finished(user_items: types.UserItem, summary: T.Dict):
LOG.warning("Error from API Logging for action %s", action, exc_info=True)


def _api_logging_failed(user_items: types.UserItem, payload: T.Dict, exc: Exception):
def _api_logging_failed(payload: T.Dict, exc: Exception):
if MAPILLARY_DISABLE_API_LOGGING:
return

Expand All @@ -517,7 +516,6 @@ def _api_logging_failed(user_items: types.UserItem, payload: T.Dict, exc: Except
LOG.debug("API Logging for action %s: %s", action, payload)
try:
api_v4.logging(
user_items["user_upload_token"],
action,
payload_with_reason,
)
Expand Down Expand Up @@ -686,10 +684,14 @@ def upload(
image_metadatas, image_paths
)
if specified_image_metadatas:
clusters = mly_uploader.upload_images(
specified_image_metadatas,
event_payload={"file_type": FileType.IMAGE.value},
)
try:
clusters = mly_uploader.upload_images(
specified_image_metadatas,
event_payload={"file_type": FileType.IMAGE.value},
)
except Exception as ex:
raise UploadError(ex) from ex

if clusters:
LOG.debug(f"Uploaded to cluster: %s", clusters)

Expand Down Expand Up @@ -756,16 +758,35 @@ def upload(
_upload_zipfiles(mly_uploader, zip_paths)

except UploadError as ex:
inner_ex = ex.inner_ex

if not dry_run:
_api_logging_failed(mly_uploader.user_items, _summarize(stats), ex.inner_ex)
if isinstance(ex.inner_ex, requests.HTTPError):
raise wrap_http_exception(ex.inner_ex) from ex.inner_ex
else:
raise ex
_api_logging_failed(_summarize(stats), inner_ex)

if isinstance(inner_ex, requests.ConnectionError):
raise exceptions.MapillaryUploadConnectionError(str(inner_ex)) from inner_ex

if isinstance(inner_ex, requests.Timeout):
raise exceptions.MapillaryUploadTimeoutError(str(inner_ex)) from inner_ex

if isinstance(inner_ex, requests.HTTPError):
if inner_ex.response.status_code in [400, 401]:
try:
error_body = inner_ex.response.json()
except Exception:
error_body = {}
debug_info = error_body.get("debug_info", {})
if debug_info.get("type") in ["NotAuthorizedError"]:
raise exceptions.MapillaryUploadUnauthorizedError(
debug_info.get("message")
) from inner_ex
raise wrap_http_exception(inner_ex) from inner_ex

raise inner_ex

if stats:
if not dry_run:
_api_logging_finished(user_items, _summarize(stats))
_api_logging_finished(_summarize(stats))
_show_upload_summary(stats)
else:
LOG.info("Nothing uploaded. Bye.")
Expand Down

0 comments on commit fe5009b

Please sign in to comment.