From 30b8e880c4e192559b565c4bff97ff1c4d44bbb8 Mon Sep 17 00:00:00 2001
From: Abhijeet Kasurde <akasurde@redhat.com>
Date: Mar 05 2017 03:30:38 +0000
Subject: Handle bare exception with appropriate exception


This fix adds exceptions to appropriate locations.

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>

---

diff --git a/pagure-loadjson/pagure_loadjson_server.py b/pagure-loadjson/pagure_loadjson_server.py
index 113d6a9..1a10a52 100644
--- a/pagure-loadjson/pagure_loadjson_server.py
+++ b/pagure-loadjson/pagure_loadjson_server.py
@@ -188,7 +188,7 @@ def handle_messages():
             if data and not filename.startswith('files/'):
                 try:
                     json_data = json.loads(data)
-                except:
+                except ValueError:
                     pass
             if json_data:
                 try:
diff --git a/pagure/hooks/fedmsg.py b/pagure/hooks/fedmsg.py
index 0946f74..3778179 100644
--- a/pagure/hooks/fedmsg.py
+++ b/pagure/hooks/fedmsg.py
@@ -12,7 +12,7 @@ import sqlalchemy as sa
 import wtforms
 try:
     from flask_wtf import FlaskForm
-except:
+except ImportError:
     from flask_wtf import Form as FlaskForm
 from sqlalchemy.orm import relation
 from sqlalchemy.orm import backref
diff --git a/pagure/hooks/files/pagure_hook_requests.py b/pagure/hooks/files/pagure_hook_requests.py
index 107b0e7..e8f4dd5 100755
--- a/pagure/hooks/files/pagure_hook_requests.py
+++ b/pagure/hooks/files/pagure_hook_requests.py
@@ -80,7 +80,7 @@ def run_as_post_receive_hook():
         if data:
             try:
                 json_data = json.loads(data)
-            except:
+            except ValueError:
                 pass
         if json_data:
             pagure.lib.git.update_request_from_git(
diff --git a/pagure/hooks/irc.py b/pagure/hooks/irc.py
index 2e61ce0..9a1033f 100644
--- a/pagure/hooks/irc.py
+++ b/pagure/hooks/irc.py
@@ -15,7 +15,7 @@ import pygit2
 import wtforms
 try:
     from flask_wtf import FlaskForm
-except:
+except ImportError:
     from flask_wtf import Form as FlaskForm
 from sqlalchemy.orm import relation
 from sqlalchemy.orm import backref
diff --git a/pagure/internal/__init__.py b/pagure/internal/__init__.py
index af41f4d..134688b 100644
--- a/pagure/internal/__init__.py
+++ b/pagure/internal/__init__.py
@@ -449,7 +449,7 @@ def get_branches_of_commit():
 
     try:
         commit_id in repo_obj
-    except:
+    except ValueError:
         response = flask.jsonify({
             'code': 'ERROR',
             'message': 'This commit could not be found in this repo',
diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py
index 926f8a1..69e2686 100644
--- a/pagure/lib/__init__.py
+++ b/pagure/lib/__init__.py
@@ -1623,7 +1623,7 @@ def edit_issue(session, issue, ticketfolder, user, repo=None,
     if priority != -1:
         try:
             priority = int(priority)
-        except:
+        except (ValueError, TypeError):
             priority = None
         if priority != issue.priority:
             old_priority = issue.priority
@@ -1705,7 +1705,7 @@ def update_project_settings(session, repo, settings, user):
                     try:
                         settings[key] = int(settings[key]) \
                             if settings[key] else -1
-                    except ValueError:
+                    except (ValueError, TypeError):
                         raise pagure.exceptions.PagureException(
                             "Please enter a numeric value for the 'minimum "
                             "score to merge pull request' field.")
@@ -3367,7 +3367,7 @@ def could_be_text(text):
     try:
         text.decode('utf-8')
         return True
-    except:
+    except UnicodeDecodeError:
         return False
 
 
diff --git a/pagure/lib/model.py b/pagure/lib/model.py
index b151450..34c2bc6 100644
--- a/pagure/lib/model.py
+++ b/pagure/lib/model.py
@@ -891,7 +891,7 @@ class Issue(BASE):
                     display_name = filename[:50] + "..."
                 else:
                     display_name = filename
-            except:
+            except AttributeError:
                 # Search failed, return the original string
                 return (text, text, text)
             return (link, filename, display_name)
diff --git a/pagure/mail_logging.py b/pagure/mail_logging.py
index 187cbf8..4f99308 100644
--- a/pagure/mail_logging.py
+++ b/pagure/mail_logging.py
@@ -89,19 +89,11 @@ class ContextInjector(logging.Filter):  # pragma: no cover
 
         record.callstack = self.format_callstack()
 
-        record.url = '-'
-        record.args = '-'
+        record.url = getattr(flask.request, 'url', '-')
+        record.args = getattr(flask.request, 'args', '-')
         record.form = '-'
         record.username = '-'
         try:
-            record.url = flask.request.url
-        except RuntimeError:
-            pass
-        try:
-            record.args = flask.request.args
-        except RuntimeError:
-            pass
-        try:
             record.form = dict(flask.request.form)
             if 'csrf_token' in record.form:
                 record.form['csrf_token'] = 'Was present, is cleaned up'
diff --git a/pagure/pfmarkdown.py b/pagure/pfmarkdown.py
index 525dba1..cb05477 100644
--- a/pagure/pfmarkdown.py
+++ b/pagure/pfmarkdown.py
@@ -103,7 +103,7 @@ class ExplicitLinkPattern(markdown.inlinepatterns.Pattern):
 
         try:
             idx = int(idx)
-        except:
+        except (ValueError, TypeError):
             return text
 
         issue = _issue_exists(user, namespace, repo, idx)
@@ -161,7 +161,7 @@ class ImplicitIssuePattern(markdown.inlinepatterns.Pattern):
         text = ' #%s' % idx
         try:
             idx = int(idx)
-        except:
+        except (ValueError, TypeError):
             return text
 
         try:
@@ -200,7 +200,7 @@ class ImplicitPRPattern(markdown.inlinepatterns.Pattern):
         text = ' PR#%s' % idx
         try:
             idx = int(idx)
-        except:
+        except (ValueError, TypeError):
             return text
 
         try:
diff --git a/pagure/ui/issues.py b/pagure/ui/issues.py
index b33a7be..82457bc 100644
--- a/pagure/ui/issues.py
+++ b/pagure/ui/issues.py
@@ -176,7 +176,7 @@ def update_issue(repo, issueid, username=None, namespace=None):
         new_priority = None
         try:
             new_priority = int(form.priority.data)
-        except:
+        except (ValueError, TypeError):
             pass
         tags = [
             tag.strip()
@@ -187,7 +187,7 @@ def update_issue(repo, issueid, username=None, namespace=None):
         try:
             if repo.milestones:
                 new_milestone = form.milestone.data.strip() or None
-        except:
+        except AttributeError:
             pass
 
         try:
@@ -618,7 +618,7 @@ def view_issues(repo, username=None, namespace=None):
 
     try:
         priority = int(priority)
-    except:
+    except (ValueError, TypeError):
         priority = None
 
     # Hide private tickets
diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py
index d24e35d..e4f70a7 100644
--- a/pagure/ui/repo.py
+++ b/pagure/ui/repo.py
@@ -298,7 +298,7 @@ def view_commits(repo, branchname=None, username=None, namespace=None):
 
     try:
         page = int(flask.request.args.get('page', 1))
-    except ValueError:
+    except (ValueError, TypeError):
         page = 1
 
     author = flask.request.args.get('author', None)
@@ -306,7 +306,7 @@ def view_commits(repo, branchname=None, username=None, namespace=None):
     if author:
         try:
             author_obj = pagure.lib.get_user(SESSION, author)
-        except:
+        except pagure.exceptions.PagureException:
             pass
         if not author_obj:
             flask.flash(
@@ -1271,7 +1271,7 @@ def update_priorities(repo, username=None, namespace=None):
         ]
         try:
             weights = [int(w) for w in weights]
-        except:
+        except (ValueError, TypeError):
             flask.flash(
                 'Priorities weights must be numbers',
                 'error')
diff --git a/tests/test_pagure_flask_ui_issues.py b/tests/test_pagure_flask_ui_issues.py
index 14b1d66..b52b350 100644
--- a/tests/test_pagure_flask_ui_issues.py
+++ b/tests/test_pagure_flask_ui_issues.py
@@ -19,7 +19,7 @@ import sys
 import os
 try:
     import pyclamd
-except:
+except ImportError:
     pyclamd = None
 import tempfile
 
diff --git a/tests/test_pagure_flask_ui_issues_acl_checks.py b/tests/test_pagure_flask_ui_issues_acl_checks.py
index 31e0a8e..d9a45b0 100644
--- a/tests/test_pagure_flask_ui_issues_acl_checks.py
+++ b/tests/test_pagure_flask_ui_issues_acl_checks.py
@@ -20,7 +20,7 @@ import sys
 import os
 try:
     import pyclamd
-except:
+except ImportError:
     pyclamd = None
 import tempfile
 

