exceptions: use python3 compatible exception handling

This commit is contained in:
Marcin Kuzminski 2018-10-02 11:03:22 +02:00
parent 25148be103
commit df27625f07
4 changed files with 13 additions and 9 deletions

View file

@ -20,7 +20,9 @@
class JSONRPCBaseError(Exception):
pass
def __init__(self, message='', *args):
self.message = message
super(JSONRPCBaseError, self).__init__(message, *args)
class JSONRPCError(JSONRPCBaseError):
@ -31,7 +33,8 @@ class JSONRPCValidationError(JSONRPCBaseError):
def __init__(self, *args, **kwargs):
self.colander_exception = kwargs.pop('colander_exc')
super(JSONRPCValidationError, self).__init__(*args, **kwargs)
super(JSONRPCValidationError, self).__init__(
message=self.colander_exception, *args)
class JSONRPCForbidden(JSONRPCBaseError):

View file

@ -33,7 +33,7 @@ from rhodecode.lib import audit_logger
from rhodecode.lib import repo_maintenance
from rhodecode.lib.auth import HasPermissionAnyApi, HasUserGroupPermissionAnyApi
from rhodecode.lib.celerylib.utils import get_task_id
from rhodecode.lib.utils2 import str2bool, time_to_datetime
from rhodecode.lib.utils2 import str2bool, time_to_datetime, safe_str
from rhodecode.lib.ext_json import json
from rhodecode.lib.exceptions import StatusChangeOnClosedPullRequestError
from rhodecode.model.changeset_status import ChangesetStatusModel
@ -316,7 +316,7 @@ def get_repo_changeset(request, apiuser, repoid, revision,
try:
cs = repo.get_commit(commit_id=revision, pre_load=pre_load)
except TypeError as e:
raise JSONRPCError(e.message)
raise JSONRPCError(safe_str(e))
_cs_json = cs.__json__()
_cs_json['diff'] = build_commit_data(cs, changes_details)
if changes_details == 'full':
@ -382,7 +382,7 @@ def get_repo_changesets(request, apiuser, repoid, start_rev, limit,
commits = vcs_repo.get_commits(
start_id=start_rev, pre_load=pre_load)
except TypeError as e:
raise JSONRPCError(e.message)
raise JSONRPCError(safe_str(e))
except Exception:
log.exception('Fetching of commits failed')
raise JSONRPCError('Error occurred during commit fetching')
@ -1433,7 +1433,7 @@ def comment_commit(
commit_id = repo.scm_instance().get_commit(commit_id=commit_id).raw_id
except Exception as e:
log.exception('Failed to fetch commit')
raise JSONRPCError(e.message)
raise JSONRPCError(safe_str(e))
if isinstance(userid, Optional):
userid = apiuser.user_id

View file

@ -25,7 +25,8 @@ import operator
from pyramid.httpexceptions import HTTPFound, HTTPForbidden, HTTPBadRequest
from rhodecode.lib import helpers as h, diffs
from rhodecode.lib.utils2 import StrictAttributeDict, safe_int, datetime_to_time
from rhodecode.lib.utils2 import (
StrictAttributeDict, safe_int, datetime_to_time, safe_unicode)
from rhodecode.lib.vcs.exceptions import RepositoryRequirementError
from rhodecode.model import repo
from rhodecode.model import repo_group
@ -202,7 +203,7 @@ class RepoAppView(BaseAppView):
def _handle_missing_requirements(self, error):
log.error(
'Requirements are missing for repository %s: %s',
self.db_repo_name, error.message)
self.db_repo_name, safe_unicode(error))
def _get_local_tmpl_context(self, include_app_defaults=True):
_ = self.request.translate

View file

@ -28,7 +28,7 @@ class TestSSHWrapper(object):
ssh_wrapper.serve(
vcs='microsoft-tfs', repo='test-repo', mode=None, user='test',
permissions={}, branch_permissions={})
assert exc_info.value.message == 'Unrecognised VCS: microsoft-tfs'
assert str(exc_info.value) == 'Unrecognised VCS: microsoft-tfs'
def test_parse_config(self, ssh_wrapper):
config = ssh_wrapper.parse_config(ssh_wrapper.ini_path)