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() ...@@ -23,7 +23,7 @@ ReadOnlyCollection<LivePacketDevice^>^ LivePacketDevice::AllLocalMachine::get()
&alldevs, errorBuffer) == -1) &alldevs, errorBuffer) == -1)
{ {
String^ errorString = gcnew String(errorBuffer); 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 try
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "OfflinePacketCommunicator.h" #include "OfflinePacketCommunicator.h"
using namespace System; using namespace System;
using namespace System::Globalization;
using namespace System::Collections::Generic; using namespace System::Collections::Generic;
using namespace System::Collections::ObjectModel; using namespace System::Collections::ObjectModel;
using namespace PcapDotNet::Core; using namespace PcapDotNet::Core;
...@@ -51,7 +52,7 @@ PacketCommunicator^ OfflinePacketDevice::Open(int snapshotLength, PacketDeviceOp ...@@ -51,7 +52,7 @@ PacketCommunicator^ OfflinePacketDevice::Open(int snapshotLength, PacketDeviceOp
errorBuffer // error buffer errorBuffer // error buffer
) != 0) ) != 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); return gcnew OfflinePacketCommunicator(source, snapshotLength, attributes, readTimeout, NULL);
......
...@@ -294,7 +294,7 @@ PacketCommunicator::PacketCommunicator(const char* source, int snapshotLength, P ...@@ -294,7 +294,7 @@ PacketCommunicator::PacketCommunicator(const char* source, int snapshotLength, P
errorBuffer); // error buffer errorBuffer); // error buffer
if (pcapDescriptor == NULL) 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; _pcapDescriptor = pcapDescriptor;
_ipV4Netmask = dynamic_cast<IpV4SocketAddress^>(netmask); _ipV4Netmask = dynamic_cast<IpV4SocketAddress^>(netmask);
......
...@@ -8,7 +8,10 @@ using namespace PcapDotNet::Core; ...@@ -8,7 +8,10 @@ using namespace PcapDotNet::Core;
// static // static
String^ PcapError::GetErrorMessage(pcap_t* pcapDescriptor) 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 // static
...@@ -17,12 +20,12 @@ InvalidOperationException^ PcapError::BuildInvalidOperation(String^ errorMessage ...@@ -17,12 +20,12 @@ InvalidOperationException^ PcapError::BuildInvalidOperation(String^ errorMessage
StringBuilder^ fullError = gcnew StringBuilder(errorMessage); StringBuilder^ fullError = gcnew StringBuilder(errorMessage);
if (pcapDescriptor != NULL) if (pcapDescriptor != NULL)
{ {
String^ pcapError = gcnew String(pcap_geterr(pcapDescriptor)); String^ pcapError = GetErrorMessage(pcapDescriptor);
if (!String::IsNullOrEmpty(pcapError)) if (!String::IsNullOrEmpty(pcapError))
{ {
fullError->Append(". "); fullError->Append(". WinPcap Error: ");
fullError->Append(pcapError); fullError->Append(pcapError);
} }
} }
return gcnew InvalidOperationException(fullError->ToString()); 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