pycontribs / jira

Python Jira library. Development chat available on https://matrix.to/#/#pycontribs:matrix.org
https://jira.readthedocs.io
BSD 2-Clause "Simplified" License
1.92k stars 857 forks source link

Parse errors does not reach correct block #1846

Open kmosti opened 3 months ago

kmosti commented 3 months ago

Bug summary

https://github.com/pycontribs/jira/blob/eb0ec90e08ae24823e266b0128b852022d212982/jira/resilientsession.py#L108-L121

elifstatements here causes the final block to not be executed when e.g. errorMessages is present, empty, but there is an errors array also in the returned error text.

For example, with the below content within error.text, parse_errors will always return an empty List:

{"errorMessages":[],"errors":{"assignee":"User \'foo@bar.xyz\' does not exist."}}

Changing the elif statements to if would ensure that the parser reaches the if "errors" in resp_data: block.

Is there an existing issue for this?

Jira Instance type

Jira Server or Data Center (Self-hosted)

Jira instance version

v9.14.0

jira-python version

3.6.0

Python Interpreter version

3.11.8

Which operating systems have you used?

Reproduction steps

# 1. When I try to update an issue with a non-existing user as the assignee
# I want to catch that we have a "User foo@bar does not exist" type error, so I need to parse the returned error messages.

def update_assigned(self, incident_id: str, owner: Owner):
    self._update_token_silent()
    self._logger.info(
        "(TEST) Changing assignee for incident with id '%s' in JIRA: %s",
        incident_id,
        owner.email,
    )
    update: Dict = {"assignee": [{"set": {"name": owner.email}}]}
    try:
        jira_incident = self._get_issue(incident_id)
        jira_incident.update(update=update)
    except JIRAError as e:
        parsed_errors = self._parse_errors(e.response)
        # if pattern in parsed_errors, handle in some way
        self._logger.error("Error during update of JIRA issue: %s", e)
        return
    self._logger.info("Updated issue: %s", jira_incident.permalink())

# 2. When I call the function with a non existing user, parsed_errors is always empty

Stack trace

N/A

Expected behaviour

I would expect the function to return a list of error messages from the server error response.

Additional Context

No response

kmosti commented 3 months ago

I made a PR here with a proposed solution:
https://github.com/pycontribs/jira/pull/1854