Skip to content

Commit

Permalink
Moved JIRAError to exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorin Sbarnea committed Oct 11, 2015
1 parent 39fe9e9 commit adf8e07
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 67 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ pypi:
python2.7 setup.py bdist_wheel upload
#python3.4 setup.py bdist_wheel upload

pypitest:
python setup.py check --restructuredtext --strict
python2.7 setup.py bdist_wheel upload

docs:
pip install sphinx
sphinx-build -b html docs/ docs/build/
Expand Down
2 changes: 1 addition & 1 deletion jira/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
from .version import __version__
from .config import get_jira
from .client import JIRA, Priority, Comment, Worklog, Watchers, User, Role, Issue, Project
from .utils import JIRAError
from .exceptions import JIRAError
35 changes: 35 additions & 0 deletions jira/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class JIRAError(Exception):

"""General error raised for all problems in operation of the client."""

def __init__(self, status_code=None, text=None, url=None, request=None, response=None, **kwargs):
self.status_code = status_code
self.text = text
self.url = url
self.request = request
self.response = response
self.headers = kwargs.get('headers', None)

def __str__(self):
t = "JiraError HTTP %s" % self.status_code
if self.text:
t += "\n\ttext: %s" % self.text
if self.url:
t += "\n\turl: %s" % self.url

if self.request is not None and hasattr(self.request, 'headers'):
t += "\n\trequest headers = %s" % self.request.headers

if self.request is not None and hasattr(self.request, 'text'):
t += "\n\trequest text = %s" % self.request.text

if self.response is not None and hasattr(self.response, 'headers'):
t += "\n\tresponse headers = %s" % self.response.headers

if self.response is not None and hasattr(self.response, 'text'):
t += "\n\tresponse text = %s" % self.response.text

t += '\n'
return t


1 change: 1 addition & 0 deletions jira/resilientsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import time
import json
from .exceptions import JIRAError


def raise_on_error(r, verb='???', **kwargs):
Expand Down
31 changes: 29 additions & 2 deletions jira/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,35 @@

from six import iteritems, string_types, text_type

from .utils import threaded_requests, json_loads, get_error_list, CaseInsensitiveDict

from .utils import threaded_requests, json_loads, CaseInsensitiveDict

def get_error_list(r):
error_list = []
if r.status_code >= 400:
if r.status_code == 403 and "x-authentication-denied-reason" in r.headers:
error_list = [r.headers["x-authentication-denied-reason"]]
elif r.text:
try:
response = json_loads(r)
if 'message' in response:
# JIRA 5.1 errors
error_list = [response['message']]
elif 'errorMessages' in response and len(response['errorMessages']) > 0:
# JIRA 5.0.x error messages sometimes come wrapped in this array
# Sometimes this is present but empty
errorMessages = response['errorMessages']
if isinstance(errorMessages, (list, tuple)):
error_list = errorMessages
else:
error_list = [errorMessages]
elif 'errors' in response and len(response['errors']) > 0:
# JIRA 6.x error messages are found in this array.
error_list = response['errors'].values()
else:
error_list = [r.text]
except ValueError:
error_list = [r.text]
return error_list

class Resource(object):

Expand Down
62 changes: 0 additions & 62 deletions jira/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,65 +84,3 @@ def json_loads(r):
return {}


class JIRAError(Exception):

"""General error raised for all problems in operation of the client."""

def __init__(self, status_code=None, text=None, url=None, request=None, response=None, **kwargs):
self.status_code = status_code
self.text = text
self.url = url
self.request = request
self.response = response
self.headers = kwargs.get('headers', None)

def __str__(self):
t = "JiraError HTTP %s" % self.status_code
if self.text:
t += "\n\ttext: %s" % self.text
if self.url:
t += "\n\turl: %s" % self.url

if self.request is not None and hasattr(self.request, 'headers'):
t += "\n\trequest headers = %s" % self.request.headers

if self.request is not None and hasattr(self.request, 'text'):
t += "\n\trequest text = %s" % self.request.text

if self.response is not None and hasattr(self.response, 'headers'):
t += "\n\tresponse headers = %s" % self.response.headers

if self.response is not None and hasattr(self.response, 'text'):
t += "\n\tresponse text = %s" % self.response.text

t += '\n'
return t


def get_error_list(r):
error_list = []
if r.status_code >= 400:
if r.status_code == 403 and "x-authentication-denied-reason" in r.headers:
error_list = [r.headers["x-authentication-denied-reason"]]
elif r.text:
try:
response = json_loads(r)
if 'message' in response:
# JIRA 5.1 errors
error_list = [response['message']]
elif 'errorMessages' in response and len(response['errorMessages']) > 0:
# JIRA 5.0.x error messages sometimes come wrapped in this array
# Sometimes this is present but empty
errorMessages = response['errorMessages']
if isinstance(errorMessages, (list, tuple)):
error_list = errorMessages
else:
error_list = [errorMessages]
elif 'errors' in response and len(response['errors']) > 0:
# JIRA 6.x error messages are found in this array.
error_list = response['errors'].values()
else:
error_list = [r.text]
except ValueError:
error_list = [r.text]
return error_list
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pep8
pytest>=2.6.0
pytest-cov
pytest-pep8
pytest-cache
pytest-xdist
pytest-instafail
requests_oauthlib>=0.3.3
Expand Down
3 changes: 1 addition & 2 deletions requirements-opt.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
ipython>=0.13
pytest-cache
ipython>=4.0.0
unittest2
ordereddict
PyJWT
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# this should help getting annoying warnings from inside distutils
warnings.simplefilter('ignore', UserWarning)


def _is_ordereddict_needed():
''' Check if `ordereddict` package really needed '''
try:
Expand Down

0 comments on commit adf8e07

Please sign in to comment.