2011-06-13 10 views
1

flask-sqlalchemy에서 생성 된 오류를 잡으려고합니다. 디버거가 먼저 catch하므로 코드가 불행히도이 오류를 처리 할 수 ​​없습니다. 고유해야하지만 복제 된 필드가있는 데이터베이스에 레코드를 삽입하려고 할 때 발생합니다.이 문제를 해결할 수있는 방법이 있습니까? 이것은 역 추적입니다.플라스크 오류 처리

sqlalchemy.exc.IntegrityError 

IntegrityError: (IntegrityError) column username is not unique u'INSERT INTO user (username, password, karma) VALUES (?, ?, ?)' (u'Test', u'test', 0) 
Traceback (most recent call last) 

    * 
     File "/home/josh/Projects/masterpieces_of_code/env/lib/python2.6/site-packages/flask/app.py", line 889, in __call__ 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] return self.wsgi_app(environ, start_response) 

    * 
     File "/home/josh/Projects/masterpieces_of_code/env/lib/python2.6/site-packages/flask/app.py", line 879, in wsgi_app 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] response = self.make_response(self.handle_exception(e)) 

    * 
     File "/home/josh/Projects/masterpieces_of_code/env/lib/python2.6/site-packages/flask/app.py", line 876, in wsgi_app 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] rv = self.dispatch_request() 

    * 
     File "/home/josh/Projects/masterpieces_of_code/env/lib/python2.6/site-packages/flask/app.py", line 695, in dispatch_request 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] return self.view_functions[rule.endpoint](**req.view_args) 

    * 
     File "/home/josh/Projects/masterpieces_of_code/app.py", line 70, in create_user 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] db.session.commit() 

    * 
     File "/usr/share/pyshared/sqlalchemy/orm/scoping.py", line 129, in do 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] return getattr(self.registry(), name)(*args, **kwargs) 

    * 
     File "/usr/share/pyshared/sqlalchemy/orm/session.py", line 653, in commit 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] self.transaction.commit() 

    * 
     File "/usr/share/pyshared/sqlalchemy/orm/session.py", line 364, in commit 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] self._prepare_impl() 

    * 
     File "/usr/share/pyshared/sqlalchemy/orm/session.py", line 348, in _prepare_impl 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] self.session.flush() 

    * 
     File "/usr/share/pyshared/sqlalchemy/orm/session.py", line 1346, in flush 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] self._flush(objects) 

    * 
     File "/usr/share/pyshared/sqlalchemy/orm/session.py", line 1427, in _flush 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] flush_context.execute() 

    * 
     File "/usr/share/pyshared/sqlalchemy/orm/unitofwork.py", line 299, in execute 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] rec.execute(self) 

    * 
     File "/usr/share/pyshared/sqlalchemy/orm/unitofwork.py", line 443, in execute 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] uow 

    * 
     File "/usr/share/pyshared/sqlalchemy/orm/mapper.py", line 1833, in _save_obj 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] execute(statement, params) 

    * 
     File "/usr/share/pyshared/sqlalchemy/engine/base.py", line 1794, in execute 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] object, *multiparams, **params) 

    * 
     File "/usr/share/pyshared/sqlalchemy/interfaces.py", line 149, in execute 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] return execute(clauseelement, *multiparams, **params) 

    * 
     File "/usr/share/pyshared/sqlalchemy/engine/base.py", line 1157, in execute 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] params) 

    * 
     File "/usr/share/pyshared/sqlalchemy/engine/base.py", line 1237, in _execute_clauseelement 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] return self.__execute_context(context) 

    * 
     File "/usr/share/pyshared/sqlalchemy/engine/base.py", line 1268, in __execute_context 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] context.parameters[0], context=context) 

    * 
     File "/usr/share/pyshared/sqlalchemy/engine/base.py", line 1806, in _cursor_execute 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] cursor, statement, parameters, context, False) 

    * 
     File "/home/josh/Projects/masterpieces_of_code/env/lib/python2.6/site-packages/flaskext/sqlalchemy.py", line 115, in cursor_execute 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] return execute(cursor, statement, parameters, context) 

    * 
     File "/usr/share/pyshared/sqlalchemy/engine/base.py", line 1367, in _cursor_execute 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] context) 

    * 
     File "/usr/share/pyshared/sqlalchemy/engine/base.py", line 1360, in _cursor_execute 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] context) 

    * 
     File "/usr/share/pyshared/sqlalchemy/engine/default.py", line 288, in do_execute 

     [Display the sourcecode for this frame] [Open an interactive python shell in this frame] cursor.execute(statement, parameters) 

    IntegrityError: (IntegrityError) column username is not unique u'INSERT INTO user (username, password, karma) VALUES (?, ?, ?)' (u'Test', u'test', 0) 

This is the Copy/Paste friendly version of the traceback. You can also paste this traceback into the public lodgeit pastebin: 

Traceback (most recent call last): 
    File "/home/josh/Projects/masterpieces_of_code/env/lib/python2.6/site-packages/flask/app.py", line 889, in __call__ 
    return self.wsgi_app(environ, start_response) 
    File "/home/josh/Projects/masterpieces_of_code/env/lib/python2.6/site-packages/flask/app.py", line 879, in wsgi_app 
    response = self.make_response(self.handle_exception(e)) 
    File "/home/josh/Projects/masterpieces_of_code/env/lib/python2.6/site-packages/flask/app.py", line 876, in wsgi_app 
    rv = self.dispatch_request() 
    File "/home/josh/Projects/masterpieces_of_code/env/lib/python2.6/site-packages/flask/app.py", line 695, in dispatch_request 
    return self.view_functions[rule.endpoint](**req.view_args) 
    File "/home/josh/Projects/masterpieces_of_code/app.py", line 70, in create_user 
    db.session.commit() 
    File "/usr/share/pyshared/sqlalchemy/orm/scoping.py", line 129, in do 
    return getattr(self.registry(), name)(*args, **kwargs) 
    File "/usr/share/pyshared/sqlalchemy/orm/session.py", line 653, in commit 
    self.transaction.commit() 
    File "/usr/share/pyshared/sqlalchemy/orm/session.py", line 364, in commit 
    self._prepare_impl() 
    File "/usr/share/pyshared/sqlalchemy/orm/session.py", line 348, in _prepare_impl 
    self.session.flush() 
    File "/usr/share/pyshared/sqlalchemy/orm/session.py", line 1346, in flush 
    self._flush(objects) 
    File "/usr/share/pyshared/sqlalchemy/orm/session.py", line 1427, in _flush 
    flush_context.execute() 
    File "/usr/share/pyshared/sqlalchemy/orm/unitofwork.py", line 299, in execute 
    rec.execute(self) 
    File "/usr/share/pyshared/sqlalchemy/orm/unitofwork.py", line 443, in execute 
    uow 
    File "/usr/share/pyshared/sqlalchemy/orm/mapper.py", line 1833, in _save_obj 
    execute(statement, params) 
    File "/usr/share/pyshared/sqlalchemy/engine/base.py", line 1794, in execute 
    object, *multiparams, **params) 
    File "/usr/share/pyshared/sqlalchemy/interfaces.py", line 149, in execute 
    return execute(clauseelement, *multiparams, **params) 
    File "/usr/share/pyshared/sqlalchemy/engine/base.py", line 1157, in execute 
    params) 
    File "/usr/share/pyshared/sqlalchemy/engine/base.py", line 1237, in _execute_clauseelement 
    return self.__execute_context(context) 
    File "/usr/share/pyshared/sqlalchemy/engine/base.py", line 1268, in __execute_context 
    context.parameters[0], context=context) 
    File "/usr/share/pyshared/sqlalchemy/engine/base.py", line 1806, in _cursor_execute 
    cursor, statement, parameters, context, False) 
    File "/home/josh/Projects/masterpieces_of_code/env/lib/python2.6/site-packages/flaskext/sqlalchemy.py", line 115, in cursor_execute 
    return execute(cursor, statement, parameters, context) 
    File "/usr/share/pyshared/sqlalchemy/engine/base.py", line 1367, in _cursor_execute 
    context) 
    File "/usr/share/pyshared/sqlalchemy/engine/base.py", line 1360, in _cursor_execute 
    context) 
    File "/usr/share/pyshared/sqlalchemy/engine/default.py", line 288, in do_execute 
    cursor.execute(statement, parameters) 
IntegrityError: (IntegrityError) column username is not unique u'INSERT INTO user (username, password, karma) VALUES (?, ?, ?)' (u'Test', u'test', 0) 

The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error. 

To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side. 

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection: 

    * dump() shows all variables in the frame 
    * dump(obj) dumps all that's known about the object 

Brought to you by DON'T PANIC, your friendly Werkzeug powered traceback interpreter. 

나는 오류를 처리하려고 할 때 플라스크 반응하는 방법을 언급하는 것을 잊었다. 나는 정의되지 않은 전역과 관련된 것을 얻는다. 여기에 대한 추적이 있습니다.

Traceback (most recent call last): 
    File "/home/josh/Projects/masterpieces_of_code/env/lib/python2.6/site-packages/flask/app.py", line 889, in __call__ 
    return self.wsgi_app(environ, start_response) 
    File "/home/josh/Projects/masterpieces_of_code/env/lib/python2.6/site-packages/flask/app.py", line 879, in wsgi_app 
    response = self.make_response(self.handle_exception(e)) 
    File "/home/josh/Projects/masterpieces_of_code/env/lib/python2.6/site-packages/flask/app.py", line 876, in wsgi_app 
    rv = self.dispatch_request() 
    File "/home/josh/Projects/masterpieces_of_code/env/lib/python2.6/site-packages/flask/app.py", line 695, in dispatch_request 
    return self.view_functions[rule.endpoint](**req.view_args) 
    File "/home/josh/Projects/masterpieces_of_code/app.py", line 72, in create_user 
    except(IntegrityError): 
NameError: global name 'IntegrityError' is not defined 
+0

귀하의 의견에 따라 답변을 업데이트했습니다. –

답변

9

당신은 db.session.commit()을 수행하는 코드 블록에 create_user 기능 (masterpieces_of_code/app.py)에서 시도/캐치를 놓고, 거기에 예외를 처리해야합니다.

업데이트 : 것은 당신이 IntegrityError에 대한 NameError을 받고하다면 그것은 당신이 당신의 app.py

from sqlalchemy.exc import IntegrityError 

누락 될 가능성을 의미한다. 이것은 Flask의 메시지가 아닙니다. Python이 app.py의 코드를 해석 할 때 알 수없는 이름 (가져 오기 누락으로 인해)에 대해 알려줍니다.

+0

고마워, 나는 그것을 이미 시도한 것을 언급하는 것을 잊었다. 어떤 이유로 든 Flask는 IntegrityError가 정의되지 않은 전역이라고 말합니다. 이를 반영하기 위해 원본 게시물을 업데이트했습니다. –

+0

달콤한, 고마워. 저는 파이썬을 처음 접했습니다. –