2017-03-11 4 views
1

私はかなりPythonを使い慣れていません。私はハグを使用して簡単なAPIを構築しています。私はデコレータを使用して、以下のコードのように処理されない例外をすべて処理しようとしています。しかし、私はHugが必要とする入力をデコレータに正しく渡していないようです。ここでPythonデコレータをHug APIフレームワークで動作させる

auth.py

from functools import wraps 

import hug 
from falcon import HTTP_400, HTTP_500 

import store 
import validator 
from user_entity import UserEntity 


def _error(dict, response, status=HTTP_400): 
    response.status = status 
    return {'errors': dict} 


def handle_exceptions(f): 
    """Handle all non-handled exceptions.""" 
    @wraps(f) 
    def decorated(*args, **kwargs): 
     try: 
      return f(*args, **kwargs) 
     except Exception as e: 
      return _error({'message': str(e)}, HTTP_500) 
    return decorated 


@hug.post('/') 
@handle_exceptions 
def create_user(username, password, response): 
    """Validate and create a user in the database.""" 
    is_valid, vres = validator.validate_user(username, password) 
    if not is_valid: 
     return _error(
      {k: v for k, v in vres.items() if v is not None}, response) 

    user = UserEntity(username=username, password=password) 
    urn, usr = user.db_view() 
    store.create_user(urn, usr) 

    return user.public_view() 

は私が取得エラーです:

Traceback (most recent call last): 
    File "auth.py", line 23, in decorated 
    return f(*args, **kwargs) 
TypeError: create_user() missing 1 required positional argument: 'response' 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/wsgiref/handlers.py", line 137, in run 
    self.result = application(self.environ, self.start_response) 
    File "/Users/munjal/.virtualenvs/utl-identity-auth-server/lib/python3.6/site-packages/falcon/api.py", line 189, in __call__ 
    responder(req, resp, **params) 
    File "/Users/munjal/.virtualenvs/utl-identity-auth-server/lib/python3.6/site-packages/hug/interface.py", line 651, in __call__ 
    self.render_content(self.call_function(**input_parameters), request, response, **kwargs) 
    File "/Users/munjal/.virtualenvs/utl-identity-auth-server/lib/python3.6/site-packages/hug/interface.py", line 595, in call_function 
    return self.interface(**parameters) 
    File "/Users/munjal/.virtualenvs/utl-identity-auth-server/lib/python3.6/site-packages/hug/interface.py", line 117, in __call__ 
    return __hug_internal_self._function(*args, **kwargs) 
    File "auth.py", line 25, in decorated 
    return _error({'message': str(e)}, HTTP_500) 
    File "auth.py", line 14, in _error 
    response.status = status 
AttributeError: 'str' object has no attribute 'status' 
+0

デコレータは正常です。デコレータを外してもこの機能はありますか? – zvone

答えて

1

あなたは

return _error({'message': str(e)}, HTTP_500) 

responseパラメータを忘れてしまったと私は考えていませんデコレータは一般的に動作します。ハグは、必要なパラメータをfunction.__code__.co_varnamesで識別します。これはfunctools.wrapsで修正されていません。デコレータを使用した後、hug.postには、引数が*args, *kwargsの関数が表示されますが、これは役に立ちません。

あなたは、私はまったく同じニーズを持っているExceptionRouter

0

でルーターをチェーンことがあります。私はそれを行う方法を見つけましたが、私はそれが理想的であると確信していません。

def handle_exceptions(f): 
    @wraps(f) 
    def decorated(*args, response, **kwargs): 
     try: 
      return f(*args, response=response, **kwargs) 
     except MyError as e: 
      response.status = HTTP_400 
      return {'errors': str(e)} 
     except Exception as e: 
      response.status = HTTP_500 
      return {'errors': 'internal server error'} 
    return decorated 


    @hug.post('/login') 
    def login(user_id: btext, user_email: btext, request, response): 
     load_session_data(request, response) 
     validate_user(user_id, user_email) 
     assert 3 < 2 
     update_session_data(request, response, user_id=user_id, user_email=user_email) 

テスト '主張' 返されたコンテンツは、代わりにデフォルト'A server error occurred. Please contact the administrator.'

wrapshug.decorators.wraps、ないfunctools.wrapsでの'internal server error'であることを示しています。後者はデフォルトを返します'A server error occurred. Please contact the administrator.'

関連する問題