@@ -311,12 +311,12 @@ def build_jm_client(self, **kwargs):
311311 """
312312 return self .new_jm_client (** kwargs )
313313
314- def new_jm_client (self , domain = None , impl = None , ** kwargs ) -> JmcomicClient :
314+ def new_jm_client (self , domain = None , impl = None , cache = None , ** kwargs ) -> JmcomicClient :
315315 # 所有需要用到的 self.client 配置项如下
316316 postman_conf : dict = self .client .postman .src_dict # postman dsl 配置
317317 impl : str = impl or self .client .impl # client_key
318318 retry_times : int = self .client .retry_times # 重试次数
319- cache : str = self .client .cache # 启用缓存
319+ cache : str = cache or self .client .cache # 启用缓存
320320
321321 # domain
322322 def decide_domain ():
@@ -340,7 +340,10 @@ def decide_domain():
340340 # headers
341341 meta_data = postman_conf ['meta_data' ]
342342 if meta_data ['headers' ] is None :
343- meta_data ['headers' ] = self .decide_postman_headers (impl , domain [0 ])
343+ headers = self .decide_postman_headers (impl , domain [0 ])
344+ # if headers is None:
345+ # postman_conf['type'] = 'requests'
346+ meta_data ['headers' ] = headers
344347
345348 # postman
346349 postman = Postmans .create (data = postman_conf )
@@ -349,7 +352,8 @@ def decide_domain():
349352 clazz = JmModuleConfig .client_impl_class (impl )
350353 if clazz == AbstractJmClient or not issubclass (clazz , AbstractJmClient ):
351354 raise NotImplementedError (clazz )
352- client = clazz (
355+
356+ client : AbstractJmClient = clazz (
353357 postman ,
354358 retry_times ,
355359 fallback_domain_list = decide_domain (),
@@ -442,36 +446,77 @@ def call_all_plugin(self, group: str, **extra):
442446
443447 ExceptionTool .require_true (plugin_class is not None , f'[{ group } ] 未注册的plugin: { key } ' )
444448
445- self .invoke_plugin (plugin_class , kwargs , extra )
449+ self .invoke_plugin (plugin_class , kwargs , extra , pinfo )
450+
451+ def invoke_plugin (self , plugin_class , kwargs : Any , extra : dict , pinfo : dict ):
452+ # 检查插件的参数类型
453+ kwargs = self .fix_kwargs (kwargs )
454+ # 把插件的配置数据kwargs和附加数据extra合并,extra会覆盖kwargs
455+ if len (extra ) != 0 :
456+ kwargs .update (extra )
446457
447- def invoke_plugin (self , plugin_class , kwargs : Any , extra : dict ):
448458 # 保证 jm_plugin.py 被加载
449- from .jm_plugin import JmOptionPlugin
459+ from .jm_plugin import JmOptionPlugin , PluginValidationException
450460
461+ plugin = plugin_class
451462 plugin_class : Type [JmOptionPlugin ]
452- pkey = plugin_class .plugin_key
453463
454464 try :
455- # 检查插件的参数类型
456- kwargs = self .fix_kwargs (kwargs )
457- # 把插件的配置数据kwargs和附加数据extra合并
458- # extra会覆盖kwargs
459- if len (extra ) != 0 :
460- kwargs .update (extra )
461465 # 构建插件对象
462- plugin = plugin_class .build (self )
466+ plugin : JmOptionPlugin = plugin_class .build (self )
467+
468+ jm_debug ('plugin.invoke' , f'调用插件: [{ plugin_class .plugin_key } ]' )
463469 # 调用插件功能
464- jm_debug ('plugin.invoke' , f'调用插件: [{ pkey } ]' )
465470 plugin .invoke (** kwargs )
471+
472+ except PluginValidationException as e :
473+ # 插件抛出的参数校验异常
474+ self .handle_plugin_valid_exception (e , pinfo , kwargs , plugin )
475+
466476 except JmcomicException as e :
467- msg = str ( e )
468- jm_debug ( 'plugin.exception' , f'插件[ { pkey } ]调用失败,异常信息: { msg } ' )
469- raise e
477+ # 模块内部异常,通过不是插件抛出的,而是插件调用了例如Client,Client请求失败抛出的
478+ self . handle_plugin_exception ( e , pinfo , kwargs , plugin )
479+
470480 except BaseException as e :
471- msg = str (e )
472- jm_debug ('plugin.error' , f'插件[{ pkey } ]运行遇到未捕获异常,异常信息: { msg } ' )
481+ # 为插件兜底,捕获其他所有异常
482+ self .handle_plugin_unexpected_error (e , pinfo , kwargs , plugin )
483+
484+ # noinspection PyMethodMayBeStatic,PyUnusedLocal
485+ def handle_plugin_valid_exception (self , e , pinfo : dict , kwargs : dict , plugin ):
486+ from .jm_plugin import PluginValidationException
487+ e : PluginValidationException
488+
489+ mode = pinfo .get ('valid' , self .plugins .valid )
490+
491+ if mode == 'ignore' :
492+ # ignore
493+ return
494+
495+ if mode == 'debug' :
496+ # debug
497+ jm_debug ('plugin.validation' ,
498+ f'插件 [{ e .plugin .plugin_key } ] 参数校验异常:{ e .msg } '
499+ )
500+ return
501+
502+ if mode == 'raise' :
503+ # raise
473504 raise e
474505
506+ # 其他的mode可以通过继承+方法重写来扩展
507+
508+ # noinspection PyMethodMayBeStatic,PyUnusedLocal
509+ def handle_plugin_unexpected_error (self , e , pinfo : dict , kwargs : dict , plugin ):
510+ msg = str (e )
511+ jm_debug ('plugin.error' , f'插件 [{ plugin .plugin_key } ],运行遇到未捕获异常,异常信息: { msg } ' )
512+ raise e
513+
514+ # noinspection PyMethodMayBeStatic,PyUnusedLocal
515+ def handle_plugin_exception (self , e , pinfo : dict , kwargs : dict , plugin ):
516+ msg = str (e )
517+ jm_debug ('plugin.exception' , f'插件 [{ plugin .plugin_key } ],调用失败,异常信息: { msg } ' )
518+ raise e
519+
475520 # noinspection PyMethodMayBeStatic
476521 def fix_kwargs (self , kwargs ) -> Dict [str , Any ]:
477522 """
0 commit comments