Skip to content

Commit

Permalink
Handle for oauth2client errors in Google Play API client (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
priitlatt authored Dec 1, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 9ade139 commit 39d4c62
Showing 3 changed files with 21 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ Version 0.47.4
- Do not require `--device-ids` for action `app-store-connect create-profile` when not creating development or ad-hoc provisioning profiles. [PR #377](https://github.com/codemagic-ci-cd/cli-tools/pull/377)
- Fix error handling if device IDs are missing and development or ad-hoc provisioning profiles are being created (applies to actions `app-store-connect create-profile` and `app-store-connect fetch-signing-files`). [PR #377](https://github.com/codemagic-ci-cd/cli-tools/pull/377)
- Fix resolving certificate type for Mac Catalyst and In-House provisioning profiles. [PR #378](https://github.com/codemagic-ci-cd/cli-tools/pull/378)
- Improve error handling for `google-play` actions. Capture `oauth2client.client` errors in Google Play API client so that the action fails gracefully with appropriate error message. [PR #379](https://github.com/codemagic-ci-cd/cli-tools/pull/379)

**Docs**
- Update option `--device-ids` documentation for action `app-store-connect create-profile`. [PR #377](https://github.com/codemagic-ci-cd/cli-tools/pull/377)
13 changes: 7 additions & 6 deletions src/codemagic/google_play/api_client.py
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
import httplib2
from googleapiclient import discovery
from googleapiclient import errors
from oauth2client import client
from oauth2client.service_account import ServiceAccountCredentials

from codemagic.utilities import log
@@ -68,7 +69,7 @@ def _get_android_publisher_service(self):
http=http,
cache_discovery=False,
)
except (errors.Error, errors.HttpError) as e:
except (errors.Error, client.Error) as e:
raise AuthorizationError(e) from e

@property
@@ -85,7 +86,7 @@ def create_edit(self, package_name: str) -> Edit:
edit_request = self.edits_service.insert(body={}, packageName=package_name)
edit_response = edit_request.execute()
self._logger.debug(f"Created edit {edit_response} for package {package_name!r}")
except (errors.Error, errors.HttpError) as e:
except (errors.Error, client.Error) as e:
raise EditError("create", package_name, e) from e
else:
return Edit(**edit_response)
@@ -100,7 +101,7 @@ def delete_edit(self, edit: Union[str, Edit], package_name: str) -> None:
)
delete_request.execute()
self._logger.debug(f"Deleted edit {edit_id} for package {package_name!r}")
except (errors.Error, errors.HttpError) as e:
except (errors.Error, client.Error) as e:
if isinstance(e, errors.HttpError) and "edit has already been successfully committed" in e.error_details:
# This can be ignored as the commit has already been exhausted
return
@@ -136,7 +137,7 @@ def _get_track(self, package_name: str, track_name: str, edit_id: str) -> Track:
)
track_response = track_request.execute()
self._logger.debug(f"Got track {track_name!r} for package {package_name!r}: {track_response}")
except (errors.Error, errors.HttpError) as e:
except (errors.Error, client.Error) as e:
raise GetResourceError("track", package_name, e) from e
else:
return Track(**track_response)
@@ -161,7 +162,7 @@ def _list_tracks(self, package_name: str, edit_id: str) -> List[Track]:
)
tracks_response = tracks_request.execute()
self._logger.debug(f"Got tracks for package {package_name!r}: {tracks_response}")
except (errors.Error, errors.HttpError) as e:
except (errors.Error, client.Error) as e:
raise ListResourcesError("tracks", package_name, e) from e
else:
return [Track(**track) for track in tracks_response["tracks"]]
@@ -189,7 +190,7 @@ def update_track(
try:
track_response = track_request.execute()
commit_response = commit_request.execute()
except (errors.Error, errors.HttpError) as e:
except (errors.Error, client.Error) as e:
raise UpdateResourceError("track", package_name, e) from e

self._logger.debug(f"Track {track.track!r} update response for package {package_name!r}: {track_response}")
22 changes: 13 additions & 9 deletions src/codemagic/google_play/api_error.py
Original file line number Diff line number Diff line change
@@ -3,26 +3,30 @@
from typing import Union

from googleapiclient import errors
from oauth2client import client


class GooglePlayDeveloperAPIClientError(Exception):
@classmethod
def _get_error_reason(cls, error: Union[errors.Error, errors.HttpError]) -> str:
if isinstance(error, errors.Error):
return str(error)
else:
reason = error._get_reason()
return reason or "Http Error"
def _get_error_reason(cls, error: Union[errors.Error, client.Error]) -> str:
if isinstance(error, errors.HttpError):
return error._get_reason() or "Http Error"
return str(error)


class AuthorizationError(GooglePlayDeveloperAPIClientError):
def __init__(self, error: Union[errors.Error, errors.HttpError]):
def __init__(self, error: Union[errors.Error, client.Error]):
reason = self._get_error_reason(error)
super().__init__(f"Unable to authorize with provided credentials. {reason}")


class EditError(GooglePlayDeveloperAPIClientError):
def __init__(self, action: str, package_name: str, error: Union[errors.Error, errors.HttpError]):
def __init__(
self,
action: str,
package_name: str,
error: Union[errors.Error, client.Error],
):
reason = self._get_error_reason(error)
super().__init__(f'Unable to {action} an edit for package "{package_name}". {reason}')

@@ -32,7 +36,7 @@ def __init__(
self,
resource_description: str,
package_name: str,
request_error: Union[errors.Error, errors.HttpError],
request_error: Union[errors.Error, client.Error],
):
self.package_name = package_name
self.request_error = request_error

0 comments on commit 39d4c62

Please sign in to comment.