From 0d756ec39c7643cdf1bf4dfc7de3383c00bfd59d Mon Sep 17 00:00:00 2001 From: Sergey Shkurov Date: Tue, 25 Apr 2023 11:28:28 +0200 Subject: [PATCH] Add failure analyzer for incorrect interface name --- .../PcapInterfaceNotFoundException.java | 15 +++++++++++++++ .../PcapFileNotFoundFailureAnalyzer.java | 2 -- .../PcapInterfaceNotFoundFailureAnalyzer.java | 16 ++++++++++++++++ .../serega6531/packmate/pcap/LivePcapWorker.java | 8 +++++--- src/main/resources/META-INF/spring.factories | 3 ++- 5 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 src/main/java/ru/serega6531/packmate/exception/PcapInterfaceNotFoundException.java create mode 100644 src/main/java/ru/serega6531/packmate/exception/analyzer/PcapInterfaceNotFoundFailureAnalyzer.java diff --git a/src/main/java/ru/serega6531/packmate/exception/PcapInterfaceNotFoundException.java b/src/main/java/ru/serega6531/packmate/exception/PcapInterfaceNotFoundException.java new file mode 100644 index 0000000..2b68750 --- /dev/null +++ b/src/main/java/ru/serega6531/packmate/exception/PcapInterfaceNotFoundException.java @@ -0,0 +1,15 @@ +package ru.serega6531.packmate.exception; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.util.List; + +@EqualsAndHashCode(callSuper = true) +@Data +public class PcapInterfaceNotFoundException extends RuntimeException { + + private final String requestedInterface; + private final List existingInterfaces; + +} diff --git a/src/main/java/ru/serega6531/packmate/exception/analyzer/PcapFileNotFoundFailureAnalyzer.java b/src/main/java/ru/serega6531/packmate/exception/analyzer/PcapFileNotFoundFailureAnalyzer.java index a7679ef..5b4be11 100644 --- a/src/main/java/ru/serega6531/packmate/exception/analyzer/PcapFileNotFoundFailureAnalyzer.java +++ b/src/main/java/ru/serega6531/packmate/exception/analyzer/PcapFileNotFoundFailureAnalyzer.java @@ -14,7 +14,6 @@ public class PcapFileNotFoundFailureAnalyzer extends AbstractFailureAnalyzer { + @Override + protected FailureAnalysis analyze(Throwable rootFailure, PcapInterfaceNotFoundException cause) { + return new FailureAnalysis( + "The interface \"" + cause.getRequestedInterface() + "\" was not found", + "Check the interface name in the config. Existing interfaces are: " + cause.getExistingInterfaces(), + cause + ); + } +} diff --git a/src/main/java/ru/serega6531/packmate/pcap/LivePcapWorker.java b/src/main/java/ru/serega6531/packmate/pcap/LivePcapWorker.java index e9cc6f2..719deec 100644 --- a/src/main/java/ru/serega6531/packmate/pcap/LivePcapWorker.java +++ b/src/main/java/ru/serega6531/packmate/pcap/LivePcapWorker.java @@ -6,10 +6,12 @@ import org.apache.commons.lang3.concurrent.BasicThreadFactory; import org.pcap4j.core.PcapNativeException; import org.pcap4j.core.PcapNetworkInterface; import org.pcap4j.core.Pcaps; +import ru.serega6531.packmate.exception.PcapInterfaceNotFoundException; import ru.serega6531.packmate.service.ServicesService; import ru.serega6531.packmate.service.StreamService; import java.net.UnknownHostException; +import java.util.List; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -26,9 +28,9 @@ public class LivePcapWorker extends AbstractPcapWorker { super(servicesService, streamService, localIpString); device = Pcaps.getDevByName(interfaceName); - if(device == null) { - log.info("Existing devices: {}", Pcaps.findAllDevs().stream().map(PcapNetworkInterface::getName).toList()); - throw new IllegalArgumentException("Device " + interfaceName + " does not exist"); + if (device == null) { + List existingInterfaces = Pcaps.findAllDevs().stream().map(PcapNetworkInterface::getName).toList(); + throw new PcapInterfaceNotFoundException(interfaceName, existingInterfaces); } BasicThreadFactory factory = new BasicThreadFactory.Builder() diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories index a451ddf..64547f6 100644 --- a/src/main/resources/META-INF/spring.factories +++ b/src/main/resources/META-INF/spring.factories @@ -1,2 +1,3 @@ org.springframework.boot.diagnostics.FailureAnalyzer=\ - ru.serega6531.packmate.exception.analyzer.PcapFileNotFoundFailureAnalyzer \ No newline at end of file + ru.serega6531.packmate.exception.analyzer.PcapFileNotFoundFailureAnalyzer,\ + ru.serega6531.packmate.exception.analyzer.PcapInterfaceNotFoundFailureAnalyzer \ No newline at end of file