Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.

Commit 25c7ae5

Browse files
authored
Merge pull request #519 from staticdev/bug/error-handling-merge-prs
Fix error handling merge PRs
2 parents 8ffdaef + 2c0a81a commit 25c7ae5

File tree

4 files changed

+115
-59
lines changed

4 files changed

+115
-59
lines changed

src/git_portfolio/github_service.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
import git_portfolio.request_objects.issue_list as il
1414

1515

16+
class GithubServiceError(Exception):
17+
"""Generic error for GithubService."""
18+
19+
pass
20+
21+
1622
class GithubService:
1723
"""Github service class."""
1824

@@ -46,11 +52,11 @@ def _test_connection(
4652
try:
4753
return connection.me()
4854
except github3.exceptions.AuthenticationFailed:
49-
raise AttributeError("Invalid token.") from None
55+
raise GithubServiceError("Invalid token.") from None
5056
except github3.exceptions.ConnectionError as github_error:
5157
raise ConnectionError() from github_error
5258
except github3.exceptions.IncompleteResponse:
53-
raise AttributeError(
59+
raise GithubServiceError(
5460
"Invalid response. Your token might not be properly scoped."
5561
) from None
5662

@@ -94,7 +100,7 @@ def create_issue_from_repo(self, github_repo: str, issue: i.Issue) -> str:
94100
else:
95101
return f"{github_repo}: {client_error.msg}.\n"
96102
except github3.exceptions.GitHubError as github_error:
97-
raise AttributeError(
103+
raise GithubServiceError(
98104
f"{github_repo}: {github_error.msg}\n"
99105
) from github_error
100106

@@ -202,7 +208,7 @@ def create_pull_request_from_repo(
202208
extra += f" Invalid field {error['field']}."
203209
return f"{github_repo}: {github_exception.msg}.{extra}\n"
204210
except github3.exceptions.GitHubError as github_error:
205-
raise AttributeError(
211+
raise GithubServiceError(
206212
f"{github_repo}: {github_error.msg}\n"
207213
) from github_error
208214

@@ -233,7 +239,7 @@ def delete_branch_from_repo(self, github_repo: str, branch: str) -> str:
233239
except github3.exceptions.NotFoundError as github_exception:
234240
return f"{github_repo}: {github_exception.msg}.\n"
235241
except github3.exceptions.GitHubError as github_error:
236-
raise AttributeError(
242+
raise GithubServiceError(
237243
f"{github_repo}: {github_error.msg}\n"
238244
) from github_error
239245

@@ -256,9 +262,14 @@ def merge_pull_request_from_repo(
256262
elif len(pulls) == 1:
257263
pull = pulls[0]
258264
output = ""
259-
pull.merge()
260-
output += f"{github_repo}: merge PR successful.\n"
261-
return output
265+
try:
266+
pull.merge()
267+
output += f"{github_repo}: merge PR successful.\n"
268+
return output
269+
except github3.exceptions.MethodNotAllowed as github_exception:
270+
raise GithubServiceError(
271+
f"{github_repo}: {github_exception.msg}\n"
272+
) from github_exception
262273
else:
263274
return (
264275
f"{github_repo}: unexpected number of PRs for "

src/git_portfolio/use_cases/gh.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Base Github use case."""
2+
import traceback
23
from typing import Any
34
from typing import Union
45

@@ -28,9 +29,16 @@ def call_github_service(self, method: str, *args: Any, **kwargs: Any) -> None:
2829
try:
2930
method_to_call = getattr(self.github_service, method)
3031
self.output += method_to_call(*args, **kwargs)
31-
except AttributeError as ae:
32+
except ghs.GithubServiceError as gse:
3233
self.error = True
33-
self.output += str(ae)
34+
self.output += str(gse)
35+
except Exception:
36+
self.error = True
37+
self.output += (
38+
"An unexpected error occured. Please report at "
39+
"https://github.com/staticdev/git-portfolio/issues/new "
40+
f"with the following info:\n{traceback.format_exc()}"
41+
)
3442

3543
def generate_response(self) -> Union[res.ResponseFailure, res.ResponseSuccess]:
3644
"""Create appropriate response object."""

0 commit comments

Comments
 (0)