diff --git a/atlassian/service_desk.py b/atlassian/service_desk.py index 13146ac7f..ff59ea237 100644 --- a/atlassian/service_desk.py +++ b/atlassian/service_desk.py @@ -1,6 +1,8 @@ # coding=utf-8 import logging +from requests import HTTPError + from .rest_client import AtlassianRestAPI log = logging.getLogger(__name__) @@ -909,3 +911,22 @@ def create_request_type( url = "rest/servicedeskapi/servicedesk/{}/requesttype".format(service_desk_id) return self.post(url, headers=self.experimental_headers, data=data) + + def raise_for_status(self, response): + """ + Checks the response for an error status and raises an exception with the error message provided by the server + :param response: + :return: + """ + if response.status_code == 401 and response.headers.get("Content-Type") != "application/json;charset=UTF-8": + raise HTTPError("Unauthorized (401)", response=response) + + if 400 <= response.status_code < 600: + try: + j = response.json() + error_msg = j["errorMessage"] + except Exception as e: + log.error(e) + response.raise_for_status() + else: + raise HTTPError(error_msg, response=response)