简体   繁体   English

在多个地方使用大块异常处理

[英]Use large block of exception handling in multiple places

I am trying to use a large block of exception handling in multiple functions, in separate classes.我正在尝试在单独的类中的多个函数中使用大量异常处理。 for example -例如 -

public class A {
  public void function funcA(input){
    try {
        randomService.randomFunction(input);
        log.info("Finished processing randomFunction");
    } catch (CustomException ex) {
        log.error("Custom exception occurred");
    } catch (MongoException ex) {
        log.error("Mongo exception occurred");
        throw ex;
    } catch (ResourceNotFoundException ex) {
        log.error("ResourceNotFound exception occurred");
    } catch (Exception ex) {
        log.error("Something totally weird happened");
        throw ex;
    }
  }
}

This is the second place.这是第二个地方。 The idea here is that the code snippet inside the try block is different, but the exception handling(ie all of the catch blocks) code is the same.这里的想法是 try 块内的代码片段不同,但异常处理(即所有的 catch 块)代码是相同的。

public class B {
  public void function funcB(input){
    try {
        randomOtherService.randomFunction(input);
        log.info("Finished processing randomFunction");
    } catch (CustomException ex) {
        log.error("Custom exception occurred");
    } catch (MongoException ex) {
        log.error("Mongo exception occurred");
        throw ex;
    } catch (ResourceNotFoundException ex) {
        log.error("ResourceNotFound exception occurred");
    } catch (Exception ex) {
        log.error("Something totally weird happened");
        throw ex;
    }
  }
}

Now one thing I should mention is that in my case this is not an API response.现在我应该提到的一件事是,就我而言,这不是 API 响应。 So I cannot get away with using @ControllerAdvice.所以我无法摆脱使用@ControllerAdvice。 If it helps my use case is in Kafka consumer.如果它对我的用例有帮助,那就是 Kafka 消费者。

So my query is there any way I can have this exception handling in same place instead of duplicating the code?所以我的查询有什么办法可以在同一个地方处理这个异常而不是复制代码? Like how @ControllerAdvice can be used to handle exceptions from @RestController is there something I can use with Kafka @StreamListener?就像如何使用 @ControllerAdvice 来处理来自 @RestController 的异常一样,我可以在 Kafka @StreamListener 中使用什么东西吗? Or can I somehow modify the lombok @Sneakythrow to and make a custom exception handler/interceptor to do all these?或者我可以以某种方式修改 lombok @Sneakythrow 并制作一个自定义异常处理程序/拦截器来完成所有这些工作吗? Or is there some better design pattern that I can follow to avoid the duplication?还是有一些更好的设计模式可以遵循以避免重复?

Thanks in advance !提前致谢 !

Here's a way to handle it – create a separate function which takes an Exception as input, and then does the common handling that you want in the various callers ( funcA() and funcB() ).这是处理它的一种方法 - 创建一个单独的 function ,它将Exception作为输入,然后在各种调用者( funcA()funcB() )中执行您想要的常见处理。

public static void handleException(Exception e) throws Exception {
    if (e instanceof CustomException) {
        log.error("Custom exception occurred");
    } else if (e instanceof MongoException) {
        log.error("Mongo exception occurred");
        throw e;
    } else if (e instanceof ResourceNotFoundException) {
        log.error("ResourceNotFound exception occurred");
    } else {
        log.error("Something totally weird happened");
        throw e;
    }
}

The callers both become pretty simple in the catch block, both just take whatever Exception was caught and hand it off to the handleException() method.调用者在catch块中都变得非常简单,两者都只是获取捕获的任何Exception并将其交给handleException()方法。 funcA() below shows how this could look:下面funcA()显示了它的外观:

public void funcA(Object input) throws Exception {
    try {
        randomService.randomFunction(input);
        log.info("Finished processing randomFunction");
    } catch (Exception e) {
        handleException(e);
    }
}

funcB() would follow the same changes: funcB()将遵循相同的更改:

public void funcB(Object input) throws Exception {
    try {
        randomOtherService.randomFunction(input);
        log.info("Finished processing randomFunction");
    } catch (Exception e) {
        handleException(e);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM