briandilley/jsonrpc4j

How to stop the server printing the exceptions from aop?

Closed this issue · 5 comments

I use spring with aop, and I want to throw some exceptions around my service methods.
But I do not want to print the exception in my server console, I just want to handle it in my client.
So, what should I do to prevent the exception printStackTrace?
Thanks a lot!

Where are you catching those exceptions? Can you post some example code? If they're uncaught exceptions then they'll be logged by JsonRpcBasicServer if shouldLogInvocationErrors is set to true. If those exceptions are caught elsewhere then they should not be logged in your console.

Thanks for your reply, my code as follow:

//import the packages

@Aspect
@Component
public class CheckAuthorities {

    @Autowired
    HttpSession session;

    @Pointcut("execution(* com.something.service.impl.*.*(..))") 
    private void anyMethod(){}

    @Around("anyMethod()")
    public Object isAccessMethod(ProceedingJoinPoint joinPoint) throws Throwable {

        //get the method info
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature)signature;
        Method method = methodSignature.getMethod();
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String serviceName = className.substring(0, className.indexOf("Impl"));
        String signatureStr = serviceName + "." + ReflectUtil.getSignatur(method);

        //if the method has not @PublicAPI, we need check the user's auth.
        if (method.getAnnotation(PublicAPI.class) == null){
            User user = (User) session.getAttribute("user");
            if(user == null){ 
                throw new NeedLogInException();
            }
            else if(user.getRoles().contains(new Role("superAdmin"))){
                return joinPoint.proceed(); 
            }
            else{
                for(Role role : user.getRoles()){
                    if(role.getMethods().contains(new ServiceMethod(signatureStr))){
                        return joinPoint.proceed();
                    }
                }
                throw new NeedAuthException();
            }
        }
        else return joinPoint.proceed(); 
    }
}

So there will be a lot of exceptions, joinPoint.proceed() maybe throw a Throwable, and sometimes it will throw a custom exception.
Of cause I can catch them with try-catch block in this method, but if I do this, the server's json-rpc response will not include my error infomation.
By the way, how to set the shouldLogInvocationErrors to false?

If I understand your comment correctly you don't want to catch the exceptions. Instead, you want to return them in the response but not log them. If this is the case you can set shouldLogInvocationErrors to false by defining a bean like this:

@Bean
    public AutoJsonRpcServiceImplExporter autoJsonRpcServiceImplExporter() {
        AutoJsonRpcServiceImplExporter exp = new AutoJsonRpcServiceImplExporter();
        exp.setShouldLogInvocationErrors(false);
        exp.setRegisterTraceInterceptor(false);
        return exp;
    }

@mslipper Yes, it's works. Thanks a lot!

Any time 😄