diff --git a/atlassian/confluence.py b/atlassian/confluence.py index d81fd0547..509b538d3 100644 --- a/atlassian/confluence.py +++ b/atlassian/confluence.py @@ -78,8 +78,8 @@ def get_page_child_by_type(self, page_id, type='page', start=None, limit=None): # Raise ApiError as the documented reason is ambiguous raise ApiError( "There is no content with the given id, " - "or the calling user does not have permission to view the content" - )(e) + "or the calling user does not have permission to view the content", + reason=e) raise @@ -168,8 +168,8 @@ def get_page_by_title(self, space, title, start=0, limit=1, expand=None): except HTTPError as e: if e.response.status_code == 404: raise ApiPermissionError( - "The calling user does not have permission to view the content" - )(e) + "The calling user does not have permission to view the content", + reason=e) raise try: @@ -211,8 +211,8 @@ def get_page_by_id(self, page_id, expand=None, status=None, version=None): # Raise ApiError as the documented reason is ambiguous raise ApiError( "There is no content with the given id, " - "or the calling user does not have permission to view the content" - )(e) + "or the calling user does not have permission to view the content", + reason=e) raise @@ -246,8 +246,8 @@ def get_page_labels(self, page_id, prefix=None, start=None, limit=None): # Raise ApiError as the documented reason is ambiguous raise ApiError( "There is no content with the given id, " - "or the calling user does not have permission to view the content" - )(e) + "or the calling user does not have permission to view the content", + reason=e) raise @@ -284,8 +284,8 @@ def get_page_comments(self, content_id, expand=None, parent_version=None, start= # Raise ApiError as the documented reason is ambiguous raise ApiError( "There is no content with the given id, " - "or the calling user does not have permission to view the content" - )(e) + "or the calling user does not have permission to view the content", + reason=e) raise @@ -305,8 +305,8 @@ def get_draft_page_by_id(self, page_id, status='draft'): except HTTPError as e: if e.response.status_code == 404: raise ApiPermissionError( - "The calling user does not have permission to view the content" - )(e) + "The calling user does not have permission to view the content", + reason=e) raise @@ -335,7 +335,7 @@ def get_all_pages_by_label(self, label, start=0, limit=50): response = self.get(url, params=params) except HTTPError as e: if e.response.status_code == 400: - raise ApiValueError("The CQL is invalid or missing")(e) + raise ApiValueError("The CQL is invalid or missing", reason=e) raise @@ -378,8 +378,8 @@ def get_all_pages_from_space(self, space, start=0, limit=50, status=None, expand except HTTPError as e: if e.response.status_code == 404: raise ApiPermissionError( - "The calling user does not have permission to view the content" - )(e) + "The calling user does not have permission to view the content", + reason=e) raise @@ -435,8 +435,8 @@ def get_all_draft_pages_from_space_through_cql(self, space, start=0, limit=500, except HTTPError as e: if e.response.status_code == 404: raise ApiPermissionError( - "The calling user does not have permission to view the content" - )(e) + "The calling user does not have permission to view the content", + reason=e) raise @@ -485,12 +485,12 @@ def remove_content(self, content_id): # Raise ApiError as the documented reason is ambiguous raise ApiError( "There is no content with the given id, or the calling " - "user does not have permission to trash or purge the content" - )(e) + "user does not have permission to trash or purge the content", + reason=e) if e.response.status_code == 409: raise ApiConflictError( - "There is a stale data object conflict when trying to delete a draft" - )(e) + "There is a stale data object conflict when trying to delete a draft", + reason=e) raise @@ -520,12 +520,12 @@ def remove_page(self, page_id, status=None, recursive=False): # Raise ApiError as the documented reason is ambiguous raise ApiError( "There is no content with the given id, or the calling " - "user does not have permission to trash or purge the content" - )(e) + "user does not have permission to trash or purge the content", + reason=e) if e.response.status_code == 409: raise ApiConflictError( - "There is a stale data object conflict when trying to delete a draft" - )(e) + "There is a stale data object conflict when trying to delete a draft", + reason=e) raise @@ -560,8 +560,8 @@ def create_page(self, space, title, body, parent_id=None, type='page', except HTTPError as e: if e.response.status_code == 404: raise ApiPermissionError( - "The calling user does not have permission to view the content" - )(e) + "The calling user does not have permission to view the content", + reason=e) raise @@ -620,8 +620,8 @@ def add_comment(self, page_id, text): except HTTPError as e: if e.response.status_code == 404: raise ApiPermissionError( - "The calling user does not have permission to view the content" - )(e) + "The calling user does not have permission to view the content", + reason=e) raise @@ -674,15 +674,15 @@ def attach_content(self, content, name, content_type='application/binary', page_ # Raise ApiError as the documented reason is ambiguous raise ApiError( "Attachments are disabled or the calling user does " - "not have permission to add attachments to this content" - )(e) + "not have permission to add attachments to this content", + reason=e) if e.response.status_code == 404: # Raise ApiError as the documented reason is ambiguous raise ApiError( "The requested content is not found, the user does not have " "permission to view it, or the attachments exceeds the maximum " - "configured attachment size" - )(e) + "configured attachment size", + reason=e) raise @@ -811,8 +811,8 @@ def get_attachments_from_content(self, page_id, start=0, limit=50, expand=None, # Raise ApiError as the documented reason is ambiguous raise ApiError( "There is no content with the given id, " - "or the calling user does not have permission to view the content" - )(e) + "or the calling user does not have permission to view the content", + reason=e) raise @@ -836,8 +836,8 @@ def set_page_label(self, page_id, label): # Raise ApiError as the documented reason is ambiguous raise ApiError( "There is no content with the given id, " - "or the calling user does not have permission to view the content" - )(e) + "or the calling user does not have permission to view the content", + reason=e) raise @@ -860,14 +860,14 @@ def remove_page_label(self, page_id, label): if e.response.status_code == 403: raise ApiPermissionError( "The user has view permission, " - "but no edit permission to the content" - )(e) + "but no edit permission to the content", + reason=e) if e.response.status_code == 404: # Raise ApiError as the documented reason is ambiguous raise ApiError( "The content or label doesn't exist, " - "or the calling user doesn't have view permission to the content" - )(e) + "or the calling user doesn't have view permission to the content", + reason=e) raise @@ -882,8 +882,8 @@ def history(self, page_id): # Raise ApiError as the documented reason is ambiguous raise ApiError( "There is no content with the given id, " - "or the calling user does not have permission to view the content" - )(e) + "or the calling user does not have permission to view the content", + reason=e) raise @@ -1061,12 +1061,12 @@ def update_page(self, page_id, title, body, parent_id=None, type='page', represe raise ApiValueError( "No space or no content type, or setup a wrong version " "type set to content, or status param is not draft and " - "status content is current" - )(e) + "status content is current", + reason=e) if e.response.status_code == 404: raise ApiNotFoundError( - "Can not find draft with current content" - )(e) + "Can not find draft with current content", + reason=e) raise @@ -1120,12 +1120,12 @@ def _insert_to_existing_page(self, page_id, title, insert_body, parent_id=None, raise ApiValueError( "No space or no content type, or setup a wrong version " "type set to content, or status param is not draft and " - "status content is current" - )(e) + "status content is current", + reason=e) if e.response.status_code == 404: raise ApiNotFoundError( - "Can not find draft with current content" - )(e) + "Can not find draft with current content", + reason=e) raise @@ -1237,15 +1237,15 @@ def set_page_property(self, page_id, data): raise ApiValueError( "The given property has a different content id to the one in the " "path, or the content already has a value with the given key, or " - "the value is missing, or the value is too long" - )(e) + "the value is missing, or the value is too long", + reason=e) if e.response.status_code == 403: raise ApiPermissionError( "The user does not have permission to " - "edit the content with the given id" - )(e) + "edit the content with the given id", + reason=e) if e.response.status_code == 413: - raise ApiValueError("The value is too long")(e) + raise ApiValueError("The value is too long", reason=e) raise @@ -1267,8 +1267,8 @@ def delete_page_property(self, page_id, page_property): # Raise ApiError as the documented reason is ambiguous raise ApiError( "There is no content with the given id, " - "or the calling user does not have permission to view the content" - )(e) + "or the calling user does not have permission to view the content", + reason=e) raise @@ -1292,8 +1292,8 @@ def get_page_property(self, page_id, page_property_key): raise ApiError( "There is no content with the given id, or no property with the " "given key, or the calling user does not have permission to view " - "the content" - )(e) + "the content", + reason=e) raise @@ -1314,8 +1314,8 @@ def get_page_properties(self, page_id): # Raise ApiError as the documented reason is ambiguous raise ApiError( "There is no content with the given id, " - "or the calling user does not have permission to view the content" - )(e) + "or the calling user does not have permission to view the content", + reason=e) raise @@ -1334,8 +1334,8 @@ def get_page_ancestors(self, page_id): except HTTPError as e: if e.response.status_code == 404: raise ApiPermissionError( - "The calling user does not have permission to view the content" - )(e) + "The calling user does not have permission to view the content", + reason=e) raise @@ -1372,8 +1372,8 @@ def get_all_groups(self, start=0, limit=1000): except HTTPError as e: if e.response.status_code == 403: raise ApiPermissionError( - "The calling user does not have permission to view groups" - )(e) + "The calling user does not have permission to view groups", + reason=e) raise @@ -1400,8 +1400,8 @@ def get_group_members(self, group_name='confluence-users', start=0, limit=1000, except HTTPError as e: if e.response.status_code == 403: raise ApiPermissionError( - "The calling user does not have permission to view users" - )(e) + "The calling user does not have permission to view users", + reason=e) raise @@ -1425,8 +1425,8 @@ def get_space(self, space_key, expand='description.plain,homepage'): # Raise ApiError as the documented reason is ambiguous raise ApiError( "There is no space with the given key, " - "or the calling user does not have permission to view the space" - )(e) + "or the calling user does not have permission to view the space", + reason=e) raise return response @@ -1466,8 +1466,8 @@ def delete_space(self, space_key): # Raise ApiError as the documented reason is ambiguous raise ApiError( "There is no space with the given key, " - "or the calling user does not have permission to delete it" - )(e) + "or the calling user does not have permission to delete it", + reason=e) raise @@ -1486,8 +1486,8 @@ def get_space_property(self, space_key, expand=None): # Raise ApiError as the documented reason is ambiguous raise ApiError( "There is no space with the given key, " - "or the calling user does not have permission to view the space" - )(e) + "or the calling user does not have permission to view the space", + reason=e) raise @@ -1511,12 +1511,12 @@ def get_user_details_by_username(self, username, expand=None): except HTTPError as e: if e.response.status_code == 403: raise ApiPermissionError( - "The calling user does not have permission to view users" - )(e) + "The calling user does not have permission to view users", + reason=e) if e.response.status_code == 404: raise ApiNotFoundError( - "The user with the given username or userkey does not exist" - )(e) + "The user with the given username or userkey does not exist", + reason=e) raise @@ -1540,12 +1540,12 @@ def get_user_details_by_userkey(self, userkey, expand=None): except HTTPError as e: if e.response.status_code == 403: raise ApiPermissionError( - "The calling user does not have permission to view users" - )(e) + "The calling user does not have permission to view users", + reason=e) if e.response.status_code == 404: raise ApiNotFoundError( - "The user with the given username or userkey does not exist" - )(e) + "The user with the given username or userkey does not exist", + reason=e) raise @@ -1585,7 +1585,7 @@ def cql(self, cql, start=0, limit=None, expand=None, include_archived_spaces=Non response = self.get('rest/api/search', params=params) except HTTPError as e: if e.response.status_code == 400: - raise ApiValueError("The query cannot be parsed")(e) + raise ApiValueError("The query cannot be parsed", reason=e) raise @@ -1640,7 +1640,7 @@ def get_descendant_page_id(self, space, parent_id, title): response = self.get(url, {}) except HTTPError as e: if e.response.status_code == 400: - raise ApiValueError("The CQL is invalid or missing")(e) + raise ApiValueError("The CQL is invalid or missing", reason=e) raise @@ -1710,8 +1710,8 @@ def anonymous(self): except HTTPError as e: if e.response.status_code == 403: raise ApiPermissionError( - "The calling user does not have permission to use Confluence" - )(e) + "The calling user does not have permission to use Confluence", + reason=e) raise @@ -1777,8 +1777,8 @@ def check_long_task_result(self, task_id, expand=None): # Raise ApiError as the documented reason is ambiguous raise ApiError( "There is no task with the given key, " - "or the calling user does not have permission to view it" - )(e) + "or the calling user does not have permission to view it", + reason=e) raise diff --git a/atlassian/errors.py b/atlassian/errors.py index 7fbec9bbc..dfb4eb8fd 100644 --- a/atlassian/errors.py +++ b/atlassian/errors.py @@ -2,7 +2,9 @@ class ApiError(Exception): - pass + def __init__(self, *args, **kwargs): + self.reason = kwargs.get("reason") + super(ApiError, self).__init__(*args) class ApiNotFoundError(ApiError): diff --git a/examples/confluence/confluence-nested-error.py b/examples/confluence/confluence-nested-error.py new file mode 100644 index 000000000..903308f8b --- /dev/null +++ b/examples/confluence/confluence-nested-error.py @@ -0,0 +1,15 @@ +# coding=utf-8 +from atlassian import Confluence +from atlassian.confluence import ApiError + +""" This example shows a way to get the real reason for an exception""" +try: + confluence = Confluence( + url='http://some_site_without_permission.com', + username='admin', + password='admin') + result = confluence.get_user_details_by_username(username="gonchik.tsymzhitov", expand="status") +except ApiError as e: + print("FAILURE: {}, caused by {}".format(e, e.reason if e.reason is not None else "unknown reason")) +else: + print(result)