@@ -86,7 +86,8 @@ def check_id(cls, id: str | int | None, *, allow_empty: bool = False) -> None:
8686 :raises TypeError: When *id* is invalid.
8787 """
8888 if (id is not None or not allow_empty ) and not isinstance (id , (int , str )):
89- raise TypeError (f"id must be an integer or string, got { id } ({ type (id )} )" )
89+ msg = f"id must be an integer or string, got { id } ({ type (id )} )"
90+ raise TypeError (msg )
9091
9192 @classmethod
9293 def check_method (cls , method : str , / ) -> None :
@@ -98,7 +99,8 @@ def check_method(cls, method: str, /) -> None:
9899 :raises TypeError: When *method* is invalid.
99100 """
100101 if not isinstance (method , str ):
101- raise TypeError (f"method must be a string, got { method } ({ type (method )} )" )
102+ msg = f"method must be a string, got { method } ({ type (method )} )"
103+ raise TypeError (msg )
102104
103105 @classmethod
104106 def check_code (cls , code : int , / ) -> None :
@@ -113,7 +115,8 @@ def check_code(cls, code: int, /) -> None:
113115 try :
114116 get_error (code )
115117 except Exception :
116- raise TypeError (f"invalid error code, got { code } ({ type (code )} )" )
118+ msg = f"invalid error code, got { code } ({ type (code )} )"
119+ raise TypeError (msg )
117120
118121 @classmethod
119122 def request (
@@ -502,7 +505,8 @@ def _handle_method(self, req: dict[str, Any]) -> None:
502505 try :
503506 method = req ["method" ]
504507 if method not in self .method_handlers :
505- raise ValueError (f"No handler defined for method: { method } " )
508+ msg = f"No handler defined for method: { method } "
509+ raise ValueError (msg )
506510 result = self .method_handlers [method ](req ["params" ])
507511 if "id" in req :
508512 res = Spec .response (req ["id" ], result )
@@ -752,7 +756,8 @@ def run(self) -> None:
752756 print (f"Incoming jsonrpc message: { decoded_msg } " )
753757 self .rpc ._handle (decoded_msg )
754758 else :
755- raise ValueError (f"Don't know how to handle: { decoded_line } " )
759+ msg = f"Don't know how to handle: { decoded_line } "
760+ raise ValueError (msg )
756761 else :
757762 self ._stopper .wait (self .interval )
758763
@@ -836,13 +841,16 @@ class MyCustomRPCError(RPCError):
836841 title = "My custom error"
837842 """
838843 if not issubclass (cls , RPCError ):
839- raise TypeError (f"'{ cls } ' is not a subclass of RPCError" )
844+ msg = f"'{ cls } ' is not a subclass of RPCError"
845+ raise TypeError (msg )
840846
841847 # check duplicates
842848 if cls .code in error_map_code :
843- raise AttributeError (f"duplicate RPC error code { cls .code } " )
849+ msg = f"duplicate RPC error code { cls .code } "
850+ raise AttributeError (msg )
844851 if cls .code_range in error_map_code_range :
845- raise AttributeError (f"duplicate RPC error code range { cls .code_range } " )
852+ msg = f"duplicate RPC error code range { cls .code_range } "
853+ raise AttributeError (msg )
846854
847855 # register
848856 error_map_code [cls .code ] = cls
@@ -867,7 +875,8 @@ def get_error(code: int) -> type[RPCError]:
867875 if lower <= code <= upper :
868876 return cls
869877
870- raise ValueError (f"unknown error code '{ code } ' ({ type (code )} )" )
878+ msg = f"unknown error code '{ code } ' ({ type (code )} )"
879+ raise ValueError (msg )
871880
872881
873882@register_error
0 commit comments