Merge branch 'failure-analyzer' into 'master'
Add failure analyzers Closes #30 See merge request packmate/Packmate!16
This commit is contained in:
@@ -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;
|
||||
|
||||
}
|
||||
@@ -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<String> existingInterfaces;
|
||||
|
||||
}
|
||||
@@ -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<PcapFileNotFoundException> {
|
||||
@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<String> 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
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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<PcapInterfaceNotFoundException> {
|
||||
@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
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
@@ -27,8 +29,8 @@ public class LivePcapWorker extends AbstractPcapWorker {
|
||||
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");
|
||||
List<String> existingInterfaces = Pcaps.findAllDevs().stream().map(PcapNetworkInterface::getName).toList();
|
||||
throw new PcapInterfaceNotFoundException(interfaceName, existingInterfaces);
|
||||
}
|
||||
|
||||
BasicThreadFactory factory = new BasicThreadFactory.Builder()
|
||||
|
||||
3
src/main/resources/META-INF/spring.factories
Normal file
3
src/main/resources/META-INF/spring.factories
Normal file
@@ -0,0 +1,3 @@
|
||||
org.springframework.boot.diagnostics.FailureAnalyzer=\
|
||||
ru.serega6531.packmate.exception.analyzer.PcapFileNotFoundFailureAnalyzer,\
|
||||
ru.serega6531.packmate.exception.analyzer.PcapInterfaceNotFoundFailureAnalyzer
|
||||
Reference in New Issue
Block a user