Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to catch exceptions in JIRA REST API - Python SOLVED

draganvucanovic April 9, 2019 edited

I want part in except block to be executed if error faced in try block, error is raised if customfield_10005 has incorrect value, so i purposely set wrong value for customfile_10005 but again except block is not executed, what's wrong with this code ?

customfile_10005 is JIRA Epic ID and i want not to raise errors if that ID is wrong:

 

from jira import JIRA
from jira.exceptions import JIRAError
try: headers = {"Content-Type": "application/json"} data = {"fields":{"project":{"key":"CP"},"issuetype":{"name":"Task"},"reporter":{"name":reporter},"assignee":{"name":reporter}, "summary":"Pre-Sales - Placeholder - 1/5 {this_month} - {reporter}".format(**locals()), "description":"descriptions", "customfield_10107":{"id":"10400"}, "customfield_11300":{"value":"Engineering"}, "customfield_11301":{"value":"York"}, "customfield_10005":200 }} response = requests.post('https://jira.company.com/rest/api/latest/issue/', data = json.dumps(data),auth=('user', password), headers= headers) print (response.content) except JIRAError as e:
print ("some errors")

Instead of  "some errors" i specified, i'm getting default error message

b'{"errorMessages":["Issue can be assigned only active or future sprints."],"errors":{}}''

 

1 answer

1 accepted

0 votes
Answer accepted
draganvucanovic April 10, 2019

I used response output to create "error handler" (because exception block doesn't work), if in response output there is string errorMessages, run "exception block"

headers = {"Content-Type": "application/json"}
data = {"fields":{"project":{"key":"CP"},"issuetype":{"name":"Task"},"reporter":{"name":reporter},"assignee":{"name":reporter},
"summary":"Pre-Sales - Placeholder - 1/5 {this_month} - {reporter}".format(**locals()),
"description":"descriptions",
"customfield_10107":{"id":"10400"},
"customfield_11300":{"value":"Engineering"},
"customfield_11301":{"value":"York"},
"customfield_10005":200}}
 response = requests.post('https://jira.company.com/rest/api/latest/issue/', data = json.dumps(data),auth=('user', password), headers= headers)
print(response.content)
out = response.content
if b'errorMessages' in out:
   headers = {"Content-Type": "application/json"}
   data = {"fields":{"project":{"key":"CP"},"issuetype":   {"name":"Task"},"reporter":{"name":reporter},"assignee":{"name":reporter},
   "summary":"Pre-Sales - Placeholder - 1/5 {this_month} - {reporter}".format(**locals()),
   "description":description",
   "customfield_10107":{"id":"10400"},
   "customfield_11300":{"value":"Engineering"},
   "customfield_11301":{"value":"York"}
    }}
   response = requests.post('https://jira.company.com/rest/api/latest/issue/', data = json.dumps(data),auth=('user', password), headers= headers)
  print (response.content)

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, likes for trees, atlassian community, social impact, tree planting campaign, community kudos, atlassian giving, environmental impact, sustainability, likes for good, atlassian social responsibility, community challenge

Make every click count—help us plant 50,000 trees! 🌳

Want to make your everyday Community actions directly contribute to reforestation? The Atlassian Community can achieve this goal by liking a post, attending an ACE, sending your peers kudos, and so much more!

Help us plant more trees
AUG Leaders

Atlassian Community Events