Python 分布式系统:在客户端的服务器端引发错误

Python 分布式系统:在客户端的服务器端引发错误,python,python-3.x,error-handling,distributed-system,raiserror,Python,Python 3.x,Error Handling,Distributed System,Raiserror,我刚刚开始创建一个分布式系统,现在只有一台服务器和一组客户端。使用的语言是python,通信是使用套接字和JSON完成的。当服务器端发生错误时,我会将错误类名和错误参数发送到客户端,如下所示: except Exception as e: jsonResult = {"error":

我刚刚开始创建一个分布式系统,现在只有一台服务器和一组客户端。使用的语言是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)
然后试着在客户端像这样提出它:

     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
我在这里读到:


看起来我必须将它声明为类。有什么办法可以解决这个问题吗

根据python的说法,您提出的东西并不例外:

>>> type(exec("ValueError('ABC')"))
<class 'NoneType'>
>类型(exec(“ValueError('ABC'))
您需要重写代码以实现以下目标:

>>> 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
errorFunc=“ValueError('ABC')” >>>执行('raise'+errorFunc) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 文件“”,第1行,在 值错误:ABC
能否显示
打印的输出(“此处:“+errorFunc”)
此处:ValueError('ABC')
是输出。这个错误是我在服务器端手动提出的。检查我的回答耐心是一种美德:P