错误处理和会话SqlAlchemy

时间:2014-10-30 18:01:25

标签: python sqlalchemy pyramid

我有一个用户必须填写的表格,包括整数/十进制数字。我在try/catch块中有“保存”功能,这样如果发生任何故障,我可以捕获异常消息并将其返回给用户。

    try:
        UpdateMyList(my_list, request)

        return exc.HTTPFound(request.route_url("edit"))
    except Exception as ex:
        return default_edit_page(request, "Unexpected error:"+str(ex))

似乎正在发生的事情是,当我遇到错误,而不是使用我的错误消息返回到同一页面时,它会给我一个“InvalidRequestError:由于之前的刷新异常,此会话的事务已被回滚”。所有这一切的重点是避免获取此屏幕enter image description here,我基本上试图优雅地失败,而不是只是让用户在他意外输入字符串而不是int时显示该屏幕

堆栈追踪:

2014-10-31 08:39:06,933 ERROR [waitress][Dummy-2] Exception when serving /edit
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\waitress-0.8.8-py2.7.egg\waitress\channel.py", line 337, in service
    task.service()
  File "C:\Python27\lib\site-packages\waitress-0.8.8-py2.7.egg\waitress\task.py", line 173, in service
    self.execute()
  File "C:\Python27\lib\site-packages\waitress-0.8.8-py2.7.egg\waitress\task.py", line 392, in execute
    app_iter = self.channel.server.application(env, start_response)
  File "C:\Python27\lib\site-packages\pyramid-1.5-py2.7.egg\pyramid\router.py", line 242, in __call__
    response = self.invoke_subrequest(request, use_tweens=True)
  File "C:\Python27\lib\site-packages\pyramid-1.5-py2.7.egg\pyramid\router.py", line 217, in invoke_subrequest
    response = handle_request(request)
  File "C:\Python27\lib\site-packages\pyramid_debugtoolbar-2.0.2-py2.7.egg\pyramid_debugtoolbar\toolbar.py", line 160, in toolbar_tween
    return handler(request)
  File "C:\Python27\lib\site-packages\pyramid-1.5-py2.7.egg\pyramid\tweens.py", line 21, in excview_tween
    response = handler(request)
  File "C:\Python27\lib\site-packages\pyramid_tm-0.7-py2.7.egg\pyramid_tm\__init__.py", line 82, in tm_tween
    reraise(*exc_info)
  File "C:\Python27\lib\site-packages\pyramid_tm-0.7-py2.7.egg\pyramid_tm\__init__.py", line 63, in tm_tween
    response = handler(request)
  File "C:\Python27\lib\site-packages\pyramid-1.5-py2.7.egg\pyramid\router.py", line 163, in handle_request
    response = view_callable(context, request)
  File "C:\Python27\lib\site-packages\pyramid-1.5-py2.7.egg\pyramid\config\views.py", line 355, in rendered_view
    result = view(context, request)
  File "C:\Python27\lib\site-packages\pyramid-1.5-py2.7.egg\pyramid\config\views.py", line 501, in _requestonly_view
    response = view(request)
  File "C:\Users\pce-coop\Desktop\testingz\PCE Testsheets VS2012\PCE Testsheets VS2012\pce_test_sheet_program\views.py", line 2297, in edit_view
    return default_edit_page(request, "Unexpected error:"+str(ex))
  File "C:\Users\pce-coop\Desktop\testingz\PCE Testsheets VS2012\PCE Testsheets VS2012\pce_test_sheet_program\views.py", line 1896, in default_edit_page
    for id, val in DBSession.query(TJob.ixJob, TJob.sJobNumber).all():
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\query.py", line 2300, in all
    return list(self)
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\query.py", line 2412, in __iter__
    return self._execute_and_instances(context)
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\query.py", line 2425, in _execute_and_instances
    close_with_result=True)
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\query.py", line 2416, in _connection_from_session
    **kw)
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\session.py", line 854, in connection
    close_with_result=close_with_result)
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\session.py", line 858, in _connection_for_bind
    return self.transaction._connection_for_bind(engine)
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\session.py", line 305, in _connection_for_bind
    self._assert_active()
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\session.py", line 214, in _assert_active
    % self._rollback_exception
InvalidRequestError: This Session's transaction has been rolled back due to a previous exception during flush. To begin a new transaction with this Session, first issue Session.rollback(). Original exception was: (ProgrammingError) ('42000', '[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Error converting data type nvarchar to numeric. (8114) (SQLExecDirectW)') u'UPDATE [tCableSet] SET [decCableSetThree]=? WHERE [tCableSet].[ixCableSet] = ?' (u'ee', 60)

0 个答案:

没有答案