From 1f61bd07fcd46269802b434ec273708f92515cdf Mon Sep 17 00:00:00 2001 From: Piotr Ksel <73847352+rifel123@users.noreply.github.com> Date: Tue, 9 Nov 2021 07:47:09 +0100 Subject: [PATCH] Issue #332: Hide traceback from UnprocessableEntity error (#336) * hide UnprocessableEntityError traceback from user * extract message from HTTPError * remove suggestion to open issue when 422 is responsed --- git_machete/exceptions.py | 4 ++-- git_machete/github.py | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/git_machete/exceptions.py b/git_machete/exceptions.py index 8283ad03a..a1f9893d3 100644 --- a/git_machete/exceptions.py +++ b/git_machete/exceptions.py @@ -14,14 +14,14 @@ def __init__(self) -> None: pass -class UnprocessableEntityHTTPError(Exception): +class UnprocessableEntityHTTPError(MacheteException): """This exception is raised when Github API returns HTTP status code 422 - Unprocessable Entity. Such a situation occurs when trying to do something not allowed by GitHub, e.g. assigning someone from outside organization as a reviewer or creating a pull request for a branch that already has a PR. """ def __init__(self, msg: str) -> None: - self.msg: str = msg + "\nPlease open an issue regarding this topic under link: https://github.com/VirtusLab/git-machete/issues/new" + self.msg: str = 'UnprocessableEntityHTTPError: ' + msg def __str__(self) -> str: return str(self.msg) diff --git a/git_machete/github.py b/git_machete/github.py index 85531db1e..6772f61ac 100644 --- a/git_machete/github.py +++ b/git_machete/github.py @@ -147,7 +147,9 @@ def __fire_github_api_request(method: str, path: str, token: Optional[str], requ return parsed_response_body except HTTPError as err: if err.code == http.HTTPStatus.UNPROCESSABLE_ENTITY: - raise UnprocessableEntityHTTPError(str(err.reason)) + error_response = json.loads(err.read().decode()) + error_reason: str = error_response['errors'][0]['message'] + raise UnprocessableEntityHTTPError(error_reason) elif err.code in (http.HTTPStatus.UNAUTHORIZED, http.HTTPStatus.FORBIDDEN): first_line = f'GitHub API returned {err.code} HTTP status with error message: `{err.reason}`\n' if token: