question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement better error handling

See original GitHub issue

For some reason the JIRA server is randomly throwing authentication errors. I’ll take this up with my IT group. however the python API doesn’t throw any exceptions it just returns a string of the html of the error. For example if I do

jira = Jira(
    url=host,
    username=user,
    password=passwd)

data = jira.jql(q)

if there is an auth error then data comes back as the string content of the html error from jira. It would be nice if the python api would just detect this internally and throw some kind of exception. Otherwise there is a cryptic error later on where I try to index the data structure and get a failure because it is not the correct type.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:15 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
rlmaerscommented, Feb 3, 2020

You would like to incorporate Atlassian specific errors into the raised exception, right? For instance making it clear that a HTTP 403 response on a GET /rest/api/2/permissions request really means that the user doesn’t have administrative privileges?

If so, then I suggest making a base exception class such as AtlassianException or similar, and a subclass exception for all of the different types of errors that can occur. Using the example above, let’s say we have an exception class AtlassianPermissionError that is subclassed from AtlassianException. The get_all_permissions method in the Jira module could then be rewritten to the following in order to translate and convey the correct meaning of the HTTP 403 response.

diff --git a/atlassian/jira.py b/atlassian/jira.py
index 8e332f0..ac93097 100644
--- a/atlassian/jira.py
+++ b/atlassian/jira.py
@@ -1525,7 +1525,13 @@ class Jira(AtlassianRestAPI):
         :return: All permissions
         """
         url = 'rest/api/2/permissions'
-        return self.get(url)
+        try:
+            return self.get(url)
+        except requests.exception.HTTPError as err:
+            if err.response.status_code == 403:
+                raise AtlassianPermissionError("User doesn't have administrative privileges") from err
+
+            raise

     def get_all_permissionschemes(self, expand=None):
         """

The advantages of doing it this way is that the original exception is preserved and that the correct information (i.e. lacking administrative privileges) is conveyed to the user and/or developer. The disadvantage is that exception chaining requires Python 3. However, as Python 2 is EOL, I assume this isn’t a problem.

0reactions
ganeshafcommented, Apr 2, 2020

Any idea when this error handling will be fixed.

i’m facing the same issue. Below is the portion output.

ERROR:atlassian.rest_client:Received: 401
 "UNAUTHORIZED" response
ERROR:atlassian.rest_client:Expecting value: line 1 column 1 (char 0)
Traceback (most recent call last):
  File "/root/system_config/system_config.py", line 411, in <module>
    main()
  File "/root/system_config/system_config.py", line 370, in main
    space, parent_page_id = get_parent_page_id_and_space(conf, parent_page_link, logger)
  File "/root/system_config/system_config.py", line 74, in get_parent_page_id_and_space
    parent_page_id = confluence.get_page_id(space=space, title=parent_page_title)
  File "/usr/local/lib/python3.7/site-packages/atlassian/confluence.py", line 87, in get_page_id
    return (self.get_page_by_title(space, title) or {}).get('id')
  File "/usr/local/lib/python3.7/site-packages/atlassian/confluence.py", line 151, in get_page_by_title
    response = (self.get(url, params=params) or {}).get('results')
AttributeError: 'str' object has no attribute 'get'
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to implement complete error handling? - Codegrip
Error handling is when your software is capable to counter or confiscate errors that may have been brought on by a programmer mistake...
Read more >
Error Handling Best Practices - Auth0
Error Handling Best Practices · Send error logs to an external service · Use error objects in rules · Use meaningful error code...
Read more >
Better error handling in JavaScript | by Iain Collins - Medium
Establishing good error handling conventions in a project can make it easier to improve the user experience of your software, squash mysterious 'unknown...
Read more >
Better Error Handling In NodeJS With Error Classes
Use error classes specifically set up for your application;; Implement abstract error handlers;; Always use async/await;; Make errors expressive ...
Read more >
Clean Code and the Art of Exception Handling - Toptal
Exception Handling: It's a Good Thing · Always create your own ApplicationError hierarchy · Never rescue Exception · Never rescue more exceptions than...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found