Simplify process_exception to allow better callback management

process_exception should only be used for RPCProgress exception and other
should let be managed by sys.excepthook.

issue6731
review36691002
This commit is contained in:
C?dric Krier 2017-10-25 18:47:21 +02:00
parent 74fe0f2d93
commit 568c1dd1fc
4 changed files with 22 additions and 62 deletions

View file

@ -38,8 +38,7 @@ except ImportError:
from threading import Lock
import dateutil.tz
from tryton.exceptions import (TrytonServerError, TrytonError,
TrytonServerUnavailable)
from tryton.exceptions import TrytonServerError, TrytonError
from tryton.pyson import PYSONEncoder
_ = gettext.gettext
@ -997,20 +996,7 @@ def process_exception(exception, *args, **kwargs):
rpc_execute = kwargs.get('rpc_execute', rpc.execute)
if isinstance(exception, TrytonError):
if exception.faultCode == 'BadFingerprint':
warning(
_('The server fingerprint has changed since last connection.\n'
'The application will stop connecting to this server '
'until its fingerprint is fixed.'), _('Security risk.'))
from tryton.gui.main import Main
Main.sig_quit()
elif exception.faultCode.startswith('403'):
if rpc.CONNECTION is None:
message(_('Connection error.\n'
'Unable to connect to the server.'))
return False
elif isinstance(exception, TrytonServerError):
if isinstance(exception, TrytonServerError):
if exception.faultCode == 'UserWarning':
name, msg, description = exception.args
res = userwarning(description, msg)
@ -1030,12 +1016,9 @@ def process_exception(exception, *args, **kwargs):
except TrytonServerError, exception:
return process_exception(exception, *args,
rpc_execute=rpc_execute)
return True
return False
elif exception.faultCode == 'UserError':
msg, description = exception.args
warning(description, msg)
return False
elif exception.faultCode == 'ConcurrencyException':
if len(args) >= 6:
if concurrency(args[1], args[3][0], args[5]):
@ -1046,15 +1029,13 @@ def process_exception(exception, *args, **kwargs):
except TrytonServerError, exception:
return process_exception(exception, *args,
rpc_execute=rpc_execute)
return False
else:
message(_('Concurrency Exception'), msg_type=gtk.MESSAGE_ERROR)
return False
elif (exception.faultCode.startswith('403')
or exception.faultCode.startswith('401')):
from tryton.gui.main import Main
if not PLOCK.acquire(False):
return False
return
language = CONFIG['client.lang']
func = lambda parameters: rpc.login(
rpc._HOST, rpc._PORT, rpc._DATABASE, rpc._USERNAME, parameters,
@ -1073,17 +1054,7 @@ def process_exception(exception, *args, **kwargs):
except TrytonServerError, exception:
return process_exception(exception, *args,
rpc_execute=rpc_execute)
elif isinstance(exception, (socket.error, TrytonServerUnavailable)):
warning(str(exception), _('Network Error.'))
return False
if isinstance(exception, TrytonServerError):
error_title, error_detail = exception.faultCode, exception.faultString
else:
error_title = str(exception)
error_detail = traceback.format_exc()
error(error_title, error_detail)
return False
raise RPCException(exception)
class Login(object):
@ -1220,14 +1191,9 @@ class RPCProgress(object):
if self.process_exception_p:
def rpc_execute(*args):
return RPCProgress('execute',
args).run(self.process_exception_p)
result = process_exception(self.exception, *self.args,
args).run(self.process_exception_p, self.callback)
return process_exception(self.exception, *self.args,
rpc_execute=rpc_execute)
if result is False:
self.exception = RPCException(self.exception)
else:
self.exception = None
self.res = result
def return_():
if self.exception:

View file

@ -874,15 +874,14 @@ class Main(object):
self.set_title() # Adds username/profile while password is asked
try:
common.Login(func)
except Exception, exception:
if (isinstance(exception, TrytonError)
and exception.faultCode == 'QueryCanceled'):
except TrytonError, exception:
if exception.faultCode == 'QueryCanceled':
return
if (isinstance(exception, TrytonServerError)
and exception.faultCode.startswith('404')):
raise
except TrytonServerError, exception:
if exception.faultCode.startswith('404'):
return self.sig_login()
common.process_exception(exception)
return
raise
self.get_preferences()
self.favorite_unset()
self.menuitem_favorite.set_sensitive(True)

View file

@ -605,18 +605,14 @@ class DBLogin(object):
host = self.entry_host.get_text()
hostname = common.get_hostname(host)
port = common.get_port(host)
try:
test = DBListEditor.test_server_version(hostname, port)
if not test:
if test is False:
common.warning('',
_(u'Incompatible version of the server'))
else:
common.warning('',
_(u'Could not connect to the server'))
continue
except Exception, exception:
common.process_exception(exception)
test = DBListEditor.test_server_version(hostname, port)
if not test:
if test is False:
common.warning('',
_(u'Incompatible version of the server'))
else:
common.warning('',
_(u'Could not connect to the server'))
continue
database = self.entry_database.get_text()
login = self.entry_login.get_text()

View file

@ -6,7 +6,7 @@ import gtk
import copy
from tryton.gui.window.view_form.screen import Screen
from tryton.config import TRYTON_ICON
from tryton.common import RPCExecute, RPCException, process_exception, Login
from tryton.common import RPCExecute, RPCException, Login
from tryton.gui.window.nomodal import NoModal
from tryton.exceptions import TrytonError
import tryton.rpc as rpc
@ -93,8 +93,7 @@ class Preference(NoModal):
except TrytonError, exception:
if exception.faultCode == 'QueryCanceled':
return
process_exception(exception)
return
raise
self.parent.present()
self.destroy()
self.callback()