1

今や、単一のサーバーと多数のクライアントを持つ分散システムを作成し始めました。使用される言語はPythonで、通信はソケットとJSONを使用して行われます。分散システム:クライアント側でサーバー側で発生するエラーを発生させよう

 except Exception as e: 
     jsonResult = {"error":                     
         { 
          "name":e.__class__.__name__,               
          "args": e.args                  
         }                       
     }                          

     jsonResult = json.dumps(jsonResult)                  
     jsonResult += "\n"                      

     return jsonResult 

そして、このように、クライアント側でそれを上げてみてください:

エラーは、サーバー側で発生した場合、私はこのように、クライアント側にエラークラス名とエラー引数を送ります
  errorFunc = decodedReply["error"]["name"] 
      args = decodedReply["error"]["args"] 
      print (args) 

      # Builds and appends the argumentstring to the error class-name. 
      errorFunc += '(' 
      for arg in args: 
       # Handles when the argument is a string. 
       if isinstance(arg, str): 
        errorFunc += '\'' + arg + '\',' 
       else: 
        errorFunc += arg + ',' 

      # Removes the extra ',' from the string before adding the last ')'. 
      errorFunc = errorFunc[:-1] 
      errorFunc += ')' 

      # Debugging print. 
      print ("Here: " + errorFunc) 

      raise exec(errorFunc) 

私はこれを行うと、私は私がここに読んだから、エラー

TypeError: exceptions must derive from BaseException 

を得る:Error exception must derive from BaseException even when it does (Python 2.7)

私はクラスとして宣言しなければならないようです。それを回避するためにとにかくありますか?例外ではなく、あなたが何かを上げているのpythonによると

+0

あなたは「(プリント 'の出力を表示することができますここでは: "+ errorFunc)'? – Saksow

+0

'ここに:ValueError( 'ABC')' が出力されます。このエラーは、サーバー側で手動で発生させたエラーです。 –

+0

私の答えをチェック – Saksow

答えて

1

>>> type(exec("ValueError('ABC')")) 
<class 'NoneType'> 

あなたが持っているあなたのコードを書き換える必要があるが、この:

>>> errorFunc = "ValueError('ABC')" 
>>> exec('raise ' + errorFunc) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<string>", line 1, in <module> 
ValueError: ABC 
+1

これは素晴らしく働いた!本当にありがとう。 –

+0

問題ありません、乾杯! – Saksow

関連する問題