55import io .kimmking .rpcfx .api .RpcfxRequest ;
66import io .kimmking .rpcfx .api .RpcfxResolver ;
77import io .kimmking .rpcfx .api .RpcfxResponse ;
8+ import io .kimmking .rpcfx .common .RpcfxException ;
9+ import lombok .extern .slf4j .Slf4j ;
810
911import java .lang .reflect .InvocationTargetException ;
1012import java .lang .reflect .Method ;
1113import java .util .Arrays ;
1214
15+ @ Slf4j
1316public class RpcfxInvoker {
1417
1518 private RpcfxResolver resolver ;
1619
17- public RpcfxInvoker (RpcfxResolver resolver ){
20+ public RpcfxInvoker (RpcfxResolver resolver ) {
1821 this .resolver = resolver ;
1922 }
2023
@@ -23,6 +26,7 @@ public RpcfxResponse invoke(RpcfxRequest request) {
2326 String serviceClass = request .getServiceClass ();
2427
2528 // 作业1:改成泛型和反射
29+ // TODO 处理beanException
2630 Object service = resolver .resolve (serviceClass );//this.applicationContext.getBean(serviceClass);
2731
2832 try {
@@ -32,14 +36,21 @@ public RpcfxResponse invoke(RpcfxRequest request) {
3236 response .setResult (JSON .toJSONString (result , SerializerFeature .WriteClassName ));
3337 response .setStatus (true );
3438 return response ;
35- } catch ( IllegalAccessException | InvocationTargetException e ) {
39+ } catch (IllegalAccessException | InvocationTargetException e ) {
3640
3741 // 3.Xstream
3842
3943 // 2.封装一个统一的RpcfxException
4044 // 客户端也需要判断异常
41- e .printStackTrace ();
42- response .setException (e );
45+ String msg = "invoke error: " + request .getServiceClass () + "." + request .getMethod ();
46+ log .error (msg , e );
47+ RpcfxException rpcfxException ;
48+ if (e instanceof InvocationTargetException ) {
49+ rpcfxException = new RpcfxException (((InvocationTargetException )e ).getTargetException (), msg );
50+ } else {
51+ rpcfxException = new RpcfxException (e , msg );
52+ }
53+ response .setException (rpcfxException );
4354 response .setStatus (false );
4455 return response ;
4556 }
0 commit comments