Add failure analyzer for incorrect interface name

This commit is contained in:
Sergey Shkurov
2023-04-25 11:28:28 +02:00
parent eef33308a5
commit 0d756ec39c
5 changed files with 38 additions and 6 deletions

View File

@@ -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;
}

View File

@@ -14,7 +14,6 @@ public class PcapFileNotFoundFailureAnalyzer extends AbstractFailureAnalyzer<Pca
String description = "The file " + cause.getFile().getAbsolutePath() + " was not found"; String description = "The file " + cause.getFile().getAbsolutePath() + " was not found";
String existingFilesMessage; String existingFilesMessage;
File[] existingFiles = cause.getDirectory().listFiles(); File[] existingFiles = cause.getDirectory().listFiles();
if (existingFiles == null) { if (existingFiles == null) {
@@ -26,7 +25,6 @@ public class PcapFileNotFoundFailureAnalyzer extends AbstractFailureAnalyzer<Pca
); );
} }
if (existingFiles.length == 0) { if (existingFiles.length == 0) {
existingFilesMessage = "The pcaps directory is currently empty"; existingFilesMessage = "The pcaps directory is currently empty";
} else { } else {

View File

@@ -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
);
}
}

View File

@@ -6,10 +6,12 @@ import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.pcap4j.core.PcapNativeException; import org.pcap4j.core.PcapNativeException;
import org.pcap4j.core.PcapNetworkInterface; import org.pcap4j.core.PcapNetworkInterface;
import org.pcap4j.core.Pcaps; import org.pcap4j.core.Pcaps;
import ru.serega6531.packmate.exception.PcapInterfaceNotFoundException;
import ru.serega6531.packmate.service.ServicesService; import ru.serega6531.packmate.service.ServicesService;
import ru.serega6531.packmate.service.StreamService; import ru.serega6531.packmate.service.StreamService;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -26,9 +28,9 @@ public class LivePcapWorker extends AbstractPcapWorker {
super(servicesService, streamService, localIpString); super(servicesService, streamService, localIpString);
device = Pcaps.getDevByName(interfaceName); device = Pcaps.getDevByName(interfaceName);
if(device == null) { if (device == null) {
log.info("Existing devices: {}", Pcaps.findAllDevs().stream().map(PcapNetworkInterface::getName).toList()); List<String> existingInterfaces = Pcaps.findAllDevs().stream().map(PcapNetworkInterface::getName).toList();
throw new IllegalArgumentException("Device " + interfaceName + " does not exist"); throw new PcapInterfaceNotFoundException(interfaceName, existingInterfaces);
} }
BasicThreadFactory factory = new BasicThreadFactory.Builder() BasicThreadFactory factory = new BasicThreadFactory.Builder()

View File

@@ -1,2 +1,3 @@
org.springframework.boot.diagnostics.FailureAnalyzer=\ org.springframework.boot.diagnostics.FailureAnalyzer=\
ru.serega6531.packmate.exception.analyzer.PcapFileNotFoundFailureAnalyzer ru.serega6531.packmate.exception.analyzer.PcapFileNotFoundFailureAnalyzer,\
ru.serega6531.packmate.exception.analyzer.PcapInterfaceNotFoundFailureAnalyzer