diff --git a/src/main/java/ru/serega6531/packmate/exception/PcapFileNotFoundException.java b/src/main/java/ru/serega6531/packmate/exception/PcapFileNotFoundException.java new file mode 100644 index 0000000..567afb5 --- /dev/null +++ b/src/main/java/ru/serega6531/packmate/exception/PcapFileNotFoundException.java @@ -0,0 +1,15 @@ +package ru.serega6531.packmate.exception; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.io.File; + +@EqualsAndHashCode(callSuper = true) +@Data +public class PcapFileNotFoundException extends RuntimeException { + + private final File file; + private final File directory; + +} 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 new file mode 100644 index 0000000..5b4be11 --- /dev/null +++ b/src/main/java/ru/serega6531/packmate/exception/analyzer/PcapFileNotFoundFailureAnalyzer.java @@ -0,0 +1,42 @@ +package ru.serega6531.packmate.exception.analyzer; + +import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; +import org.springframework.boot.diagnostics.FailureAnalysis; +import ru.serega6531.packmate.exception.PcapFileNotFoundException; + +import java.io.File; +import java.util.Arrays; +import java.util.List; + +public class PcapFileNotFoundFailureAnalyzer extends AbstractFailureAnalyzer { + @Override + protected FailureAnalysis analyze(Throwable rootFailure, PcapFileNotFoundException cause) { + String description = "The file " + cause.getFile().getAbsolutePath() + " was not found"; + String existingFilesMessage; + + File[] existingFiles = cause.getDirectory().listFiles(); + + if (existingFiles == null) { + return new FailureAnalysis( + description, + "Make sure you've put the pcap file to the ./pcaps directory, not the root directory. " + + "The directory currently does not exist", + cause + ); + } + + if (existingFiles.length == 0) { + existingFilesMessage = "The pcaps directory is currently empty"; + } else { + List existingFilesNames = Arrays.stream(existingFiles).map(File::getName).toList(); + existingFilesMessage = "The files present in " + cause.getDirectory().getAbsolutePath() + " are: " + existingFilesNames; + } + + return new FailureAnalysis( + description, + "Please verify the file name. Make sure you've put the pcap file to the ./pcaps directory, not the root directory.\n" + + existingFilesMessage, + cause + ); + } +} diff --git a/src/main/java/ru/serega6531/packmate/exception/analyzer/PcapInterfaceNotFoundFailureAnalyzer.java b/src/main/java/ru/serega6531/packmate/exception/analyzer/PcapInterfaceNotFoundFailureAnalyzer.java new file mode 100644 index 0000000..540baf9 --- /dev/null +++ b/src/main/java/ru/serega6531/packmate/exception/analyzer/PcapInterfaceNotFoundFailureAnalyzer.java @@ -0,0 +1,16 @@ +package ru.serega6531.packmate.exception.analyzer; + +import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; +import org.springframework.boot.diagnostics.FailureAnalysis; +import ru.serega6531.packmate.exception.PcapInterfaceNotFoundException; + +public class PcapInterfaceNotFoundFailureAnalyzer 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/FilePcapWorker.java b/src/main/java/ru/serega6531/packmate/pcap/FilePcapWorker.java index 6eb2b2a..c37fe5c 100644 --- a/src/main/java/ru/serega6531/packmate/pcap/FilePcapWorker.java +++ b/src/main/java/ru/serega6531/packmate/pcap/FilePcapWorker.java @@ -6,6 +6,7 @@ import org.apache.tomcat.util.threads.InlineExecutorService; import org.pcap4j.core.PcapNativeException; import org.pcap4j.core.Pcaps; import org.pcap4j.packet.Packet; +import ru.serega6531.packmate.exception.PcapFileNotFoundException; import ru.serega6531.packmate.model.enums.Protocol; import ru.serega6531.packmate.model.enums.SubscriptionMessageType; import ru.serega6531.packmate.model.pojo.SubscriptionMessage; @@ -16,7 +17,6 @@ import ru.serega6531.packmate.service.SubscriptionService; import java.io.EOFException; import java.io.File; import java.net.UnknownHostException; -import java.util.Arrays; @Slf4j public class FilePcapWorker extends AbstractPcapWorker { @@ -32,10 +32,10 @@ public class FilePcapWorker extends AbstractPcapWorker { super(servicesService, streamService, localIpString); this.subscriptionService = subscriptionService; - file = new File("pcaps", filename); + File directory = new File("pcaps"); + file = new File(directory, filename); if (!file.exists()) { - log.info("Existing files: " + Arrays.toString(new File("pcaps").listFiles())); - throw new IllegalArgumentException("File " + file.getAbsolutePath() + " does not exist"); + throw new PcapFileNotFoundException(file, directory); } processorExecutorService = new InlineExecutorService(); 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 new file mode 100644 index 0000000..64547f6 --- /dev/null +++ b/src/main/resources/META-INF/spring.factories @@ -0,0 +1,3 @@ +org.springframework.boot.diagnostics.FailureAnalyzer=\ + ru.serega6531.packmate.exception.analyzer.PcapFileNotFoundFailureAnalyzer,\ + ru.serega6531.packmate.exception.analyzer.PcapInterfaceNotFoundFailureAnalyzer \ No newline at end of file