Catching a jira-python exception
Asked Answered
T

4

5

I am trying to handle jira-python exception but my try, except does not seem to catch it. I also need to add more lines in order to be able to post this. So there they are, the lines.

try:
    new_issue = jira.create_issue(fields=issue_dict)
    stdout.write(str(new_issue.id))
except jira.exceptions.JIRAError:
    stdout.write("JIRAError")
    exit(1)

Here is the code that raises the exception:

import json


class JIRAError(Exception):
    """General error raised for all problems in operation of the client."""
    def __init__(self, status_code=None, text=None, url=None):
        self.status_code = status_code
        self.text = text
        self.url = url

    def __str__(self):
        if self.text:
            return 'HTTP {0}: "{1}"\n{2}'.format(self.status_code, self.text, self.url)
        else:
            return 'HTTP {0}: {1}'.format(self.status_code, self.url)


def raise_on_error(r):
    if r.status_code >= 400:
        error = ''
        if r.text:
            try:
                response = json.loads(r.text)
                if 'message' in response:
                    # JIRA 5.1 errors
                    error = 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 = errorMessages[0]
                    else:
                        error = errorMessages
                elif 'errors' in response and len(response['errors']) > 0:
                    # JIRA 6.x error messages are found in this array.
                    error = response['errors']
                else:
                    error = r.text
            except ValueError:
                error = r.text
        raise JIRAError(r.status_code, error, r.url)
Templas answered 21/8, 2013 at 22:16 Comment(1)
My biggest problem with this library is no one seems to know how to use it! Did you ever get this resolved?Cutlet
P
11

I know I'm not answering the question, but I feel I need to warn people that could get confused by that code (as I did)... Maybe you are trying to write your own version of jira-python or it's an old version?

In any case, here the link to the jira-python code for the JIRAError class and here the list of codes

to catch the exception from that package I use the below code

from jira import JIRA, JIRAError
try:
   ...
except JIRAError as e:
   print e.status_code, e.text
Planetoid answered 18/8, 2016 at 22:29 Comment(0)
L
5

Maybe it is obvious and that's why you don't have it in your code paste, just in case, you do have

from jira.exceptions import JIRAError

somewhere in your code right?

Don't have enough reputation for comments so I'll add it answer for @arynhard: I found the docs very light in particular in terms of example, you might find the scripts in this repo useful since they're all leveraging jira-python in someway or another. https://github.com/eucalyptus/jira-scripts/

Leff answered 5/1, 2015 at 6:29 Comment(0)
R
2

I might be wrong, but looks like you're catching jira.exceptions.JIRAError, while raising JIRAError - those are different types. You need to either remove "jira.exceptions." part from your except statement, or raise jira.exceptions.JIRAError instead.

Radiobroadcast answered 5/10, 2013 at 4:22 Comment(0)
M
1

Ok it's a very old one but I faced the same problem and this page is still showing up.

Here is how I trap the Exception, I used the Exception object.

 try:
        issue = jira.issue('jira-1')
 except  Exception as e:
        if 'EXIST' in e.text:
                print 'The issue does not exist'
                exit(1)

regards

Multifaceted answered 22/4, 2015 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.