From 388a34013c84df81674572b0584fb2c1fdf6bf9f Mon Sep 17 00:00:00 2001 From: Alfredo Altamirano <8353891+Ahuahuachi@users.noreply.github.com> Date: Thu, 29 Aug 2024 04:24:29 -0600 Subject: [PATCH] Service Desk: add dedicated error handler (#1434) Co-authored-by: Alfredo Altamirano --- atlassian/service_desk.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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)