Commit b98d48b9 authored by Brickner_cp's avatar Brickner_cp

Better error messages in Core

parent 843c5d44
......@@ -23,7 +23,7 @@ ReadOnlyCollection<LivePacketDevice^>^ LivePacketDevice::AllLocalMachine::get()
&alldevs, errorBuffer) == -1)
{
String^ errorString = gcnew String(errorBuffer);
throw gcnew InvalidOperationException(String::Format(CultureInfo::InvariantCulture, "Failed getting devices. Error: {0}", errorString));
throw gcnew InvalidOperationException(String::Format(CultureInfo::InvariantCulture, "Failed getting devices. WinPcap Error: {0}", errorString));
}
try
......
......@@ -7,6 +7,7 @@
#include "OfflinePacketCommunicator.h"
using namespace System;
using namespace System::Globalization;
using namespace System::Collections::Generic;
using namespace System::Collections::ObjectModel;
using namespace PcapDotNet::Core;
......@@ -51,7 +52,7 @@ PacketCommunicator^ OfflinePacketDevice::Open(int snapshotLength, PacketDeviceOp
errorBuffer // error buffer
) != 0)
{
throw gcnew InvalidOperationException("Error creating a source string from filename " + _fileName + " Error: " + gcnew String(errorBuffer));
throw gcnew InvalidOperationException(String::Format(CultureInfo::InvariantCulture, "Error creating a source string from filename {0}. Error: ", _fileName, gcnew String(errorBuffer)));
}
return gcnew OfflinePacketCommunicator(source, snapshotLength, attributes, readTimeout, NULL);
......
......@@ -294,7 +294,7 @@ PacketCommunicator::PacketCommunicator(const char* source, int snapshotLength, P
errorBuffer); // error buffer
if (pcapDescriptor == NULL)
throw gcnew InvalidOperationException("Unable to open the adapter. Adapter name: " + gcnew String(source) + ". WinPcap Error: " + gcnew String(errorBuffer));
throw gcnew InvalidOperationException(String::Format(CultureInfo::InvariantCulture, "Unable to open the adapter. Adapter name: {0}. WinPcap Error: {1}", gcnew String(source), gcnew String(errorBuffer)));
_pcapDescriptor = pcapDescriptor;
_ipV4Netmask = dynamic_cast<IpV4SocketAddress^>(netmask);
......
......@@ -8,7 +8,10 @@ using namespace PcapDotNet::Core;
// static
String^ PcapError::GetErrorMessage(pcap_t* pcapDescriptor)
{
return gcnew String(pcap_geterr(pcapDescriptor));
char* unmanagedPcapError = pcap_geterr(pcapDescriptor);
if (unmanagedPcapError == NULL)
return nullptr;
return gcnew String(unmanagedPcapError);
}
// static
......@@ -17,12 +20,12 @@ InvalidOperationException^ PcapError::BuildInvalidOperation(String^ errorMessage
StringBuilder^ fullError = gcnew StringBuilder(errorMessage);
if (pcapDescriptor != NULL)
{
String^ pcapError = gcnew String(pcap_geterr(pcapDescriptor));
if (!String::IsNullOrEmpty(pcapError))
{
fullError->Append(". ");
fullError->Append(pcapError);
}
String^ pcapError = GetErrorMessage(pcapDescriptor);
if (!String::IsNullOrEmpty(pcapError))
{
fullError->Append(". WinPcap Error: ");
fullError->Append(pcapError);
}
}
return gcnew InvalidOperationException(fullError->ToString());
}
\ No newline at end of file
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment