Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to throw a custom exception in Dubbo

Shulou Source: shulou.com Published: 2022-06-02 11:08:41 09月25日 Update

How to throw a custom exception in Dubbo? for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

The custom exception on the provider side of the dubbo service becomes RuntimeException on the consumer side, so that the custom exception cannot be caught.

The dubbo source code has a unified exception handling strategy:

Public Result invoke (Invoker invoker, Invocation invocation) throws RpcException {try {Result result = invoker.invoke (invocation); if (result.hasException () & & GenericService.class! = invoker.getInterface ()) {try {Throwable exception = result.getException () / / directly throw if it's checked exception if (! (exception instanceof RuntimeException) & & (exception instanceof Exception)) {return result } / / directly throw if the exception appears in the signature try {Method method = invoker.getInterface (). GetMethod (invocation.getMethodName (), invocation.getParameterTypes ()); Class [] exceptionClassses = method.getExceptionTypes () For (Class exceptionClass: exceptionClassses) {if (exception.getClass (). Equals (exceptionClass)) {return result;}} catch (NoSuchMethodException e) {return result } / / for the exception not found in method's signature, print ERROR message in server's log. Logger.error ("Got unchecked and undeclared exception which called by" + RpcContext.getContext (). GetRemoteHost () + ". Service: "+ invoker.getInterface (). GetName () +", method: "+ invocation.getMethodName () +", exception: "+ exception.getClass (). GetName () +": "+ exception.getMessage (), exception); / / directly throw if exception class and interface class are in the same jar file. String serviceFile = ReflectUtils.getCodeBase (invoker.getInterface ()); String exceptionFile = ReflectUtils.getCodeBase (exception.getClass ()); if (serviceFile = = null | | exceptionFile = = null | | serviceFile.equals (exceptionFile)) {return result } / / directly throw if it's JDK exception String className = exception.getClass () .getName (); if (className.startsWith ("java.") | | className.startsWith ("javax.")) {return result } / / directly throw if it's dubbo exception if (exception instanceof RpcException) {return result;} / / otherwise, wrap with RuntimeException and throw back to the client return new RpcResult (new RuntimeException (StringUtils.toString (exception) } catch (Throwable e) {logger.warn ("Fail to ExceptionFilter when called by" + RpcContext.getContext (). GetRemoteHost () + ". Service: "+ invoker.getInterface (). GetName () +", method: "+ invocation.getMethodName () +", exception: "+ e.getClass (). GetName () +": "+ e.getMessage (), e); return result;}} return result } catch (RuntimeException e) {logger.error ("Got unchecked and undeclared exception which called by" + RpcContext.getContext (). GetRemoteHost () + ". Service: "+ invoker.getInterface (). GetName () +", method: "+ invocation.getMethodName () +", exception: "+ e.getClass (). GetName () +": "+ e.getMessage (), e); throw e;}

As can be seen from the source code, there are several situations in which the exception is returned directly

An Exception is thrown instead of a RuntimeException

There is a declaration on the method signature

Exception class and interface declaration are in the same package

It's an exception of JDK itself.

It's an exception of dubbo itself.

From the above situations, we can see that there are the following solutions

Throws custom exception on method signature

Put exception classes and interface declarations in the same class (recommended for new projects and small project modifications)

Override the exception handling class ExceptionFilter of dubbo

This is the answer to the question about how to throw a custom exception in Dubbo. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Tags: Methods questions situations interfaces more source code processing help solutions easy easy content guys partners new projects solutions knowledge strategies articles industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Microsoft Huawei Apple Xiaomi