Skip to content

Commit 4fd5fec

Browse files
committed
CSHARP-4098: Include more detail when throwing PlatformNotSupportedException errors
1 parent a13ba0c commit 4fd5fec

File tree

8 files changed

+27
-20
lines changed

8 files changed

+27
-20
lines changed

Docs/reference/content/reference/driver/crud/compression.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ title = "Compression"
1212

1313
The C# driver supports compression of messages to and from MongoDB servers. The driver implements the three algorithms that are supported by MongoDB servers:
1414

15-
* [Snappy](https://google.github.io/snappy/): Snappy compression can be used when connecting to MongoDB servers starting with the 3.4 release.
16-
* [Zlib](https://zlib.net/): Zlib compression can be used when connecting to MongoDB servers starting with the 3.6 release.
17-
* [Zstandard](https://facebook.github.io/zstd/): Zstandard compression can be used when connecting to MongoDB servers starting with the 4.2 release.
15+
* [Snappy](https://google.github.io/snappy/): Snappy compression can be used when connecting to MongoDB servers starting with the 3.4 release. Supported platforms: Windows: x86 and x64, Linux/macOS: x64.
16+
* [Zlib](https://zlib.net/): Zlib compression can be used when connecting to MongoDB servers starting with the 3.6 release. Supported on all platforms and architectures.
17+
* [Zstandard](https://facebook.github.io/zstd/): Zstandard compression can be used when connecting to MongoDB servers starting with the 4.2 release. Supported platforms: Windows/Linux/macOS: x64 only.
1818

1919
The driver will negotiate which, if any, compression algorithm is used based on capabilities advertised by the server in the [hello]({{<docsref "reference/command/hello/">}}) or legacy hello command response.
2020

src/MongoDB.Driver.Core/Core/Compression/Snappy/Snappy32NativeMethods.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,13 @@ private class Delegates32
107107
private class SnappyLocator : RelativeLibraryLocatorBase
108108
{
109109
public override bool IsX32ModeSupported => OperatingSystemHelper.CurrentOperatingSystem == OperatingSystemPlatform.Windows;
110+
public override string LibraryName => "Snappy";
110111

111-
public override string GetLibraryName(OperatingSystemPlatform currentPlatform) =>
112+
public override string GetLibraryFileName(OperatingSystemPlatform currentPlatform) =>
112113
currentPlatform switch
113114
{
114115
OperatingSystemPlatform.Windows => "snappy32.dll", // supported only on windows
115-
_ => throw new InvalidOperationException($"Snappy is not supported on the current platform: {currentPlatform} in 32-bit mode."),
116+
_ => throw new InvalidOperationException($"{LibraryName} is not supported on the current platform: {currentPlatform} in 32-bit mode."),
116117
};
117118
}
118119
}

src/MongoDB.Driver.Core/Core/Compression/Snappy/Snappy64NativeMethods.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,15 @@ private class Delegates64
101101
private class SnappyLocator : RelativeLibraryLocatorBase
102102
{
103103
public override bool IsX32ModeSupported => false;
104+
public override string LibraryName => "Snappy";
104105

105-
public override string GetLibraryName(OperatingSystemPlatform currentPlatform) =>
106+
public override string GetLibraryFileName(OperatingSystemPlatform currentPlatform) =>
106107
currentPlatform switch
107108
{
108109
OperatingSystemPlatform.Windows => "snappy64.dll",
109110
OperatingSystemPlatform.Linux => "libsnappy64.so",
110111
OperatingSystemPlatform.MacOS => "libsnappy64.dylib",
111-
_ => throw new InvalidOperationException($"Snappy is not supported on the current platform: {currentPlatform}."),
112+
_ => throw new InvalidOperationException($"{LibraryName} is not supported on the current platform: {currentPlatform}."),
112113
};
113114
}
114115
}

src/MongoDB.Driver.Core/Core/Compression/Zstandard/Zstandard64NativeMethods.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,15 @@ private class Delegates64
254254

255255
private class ZstandardLocator : RelativeLibraryLocatorBase
256256
{
257-
public override string GetLibraryName(OperatingSystemPlatform currentPlatform) =>
257+
public override string LibraryName => "Zstandard";
258+
259+
public override string GetLibraryFileName(OperatingSystemPlatform currentPlatform) =>
258260
currentPlatform switch
259261
{
260262
OperatingSystemPlatform.Windows => "libzstd.dll",
261263
OperatingSystemPlatform.Linux => "libzstd.so",
262264
OperatingSystemPlatform.MacOS => "libzstd.dylib",
263-
_ => throw new InvalidOperationException($"Zstandard is not supported on the current platform: {currentPlatform}."),
265+
_ => throw new InvalidOperationException($"{LibraryName} is not supported on the current platform: {currentPlatform}."),
264266
};
265267
}
266268
}

src/MongoDB.Driver.Core/Core/NativeLibraryLoader/ILibraryLocator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace MongoDB.Driver.Core.NativeLibraryLoader
2020
internal interface ILibraryLocator
2121
{
2222
bool IsX32ModeSupported { get; }
23+
string LibraryName { get; }
2324
string GetLibraryAbsolutePath(OperatingSystemPlatform currentPlatform);
2425
}
2526
}

src/MongoDB.Driver.Core/Core/NativeLibraryLoader/LibraryLoader.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
* limitations under the License.
1414
*/
1515

16-
using MongoDB.Driver.Core.Misc;
17-
using MongoDB.Shared;
1816
using System;
1917
using System.Runtime.InteropServices;
18+
using MongoDB.Driver.Core.Misc;
2019

2120
namespace MongoDB.Driver.Core.NativeLibraryLoader
2221
{
@@ -29,7 +28,7 @@ public LibraryLoader(ILibraryLocator libraryLocator)
2928
Ensure.IsNotNull(libraryLocator, nameof(libraryLocator));
3029
if (!libraryLocator.IsX32ModeSupported)
3130
{
32-
ThrowIfNot64BitProcess();
31+
ThrowIfNot64BitProcess(libraryLocator);
3332
}
3433
_nativeLoader = CreateNativeLoader(libraryLocator);
3534
}
@@ -51,10 +50,10 @@ private INativeLibraryLoader CreateNativeLoader(ILibraryLocator libraryLocator)
5150
{
5251
var currentPlatform = OperatingSystemHelper.CurrentOperatingSystem;
5352
var absolutePath = libraryLocator.GetLibraryAbsolutePath(currentPlatform);
54-
return CreateNativeLoader(currentPlatform, absolutePath);
53+
return CreateNativeLoader(currentPlatform, absolutePath, libraryLocator.LibraryName);
5554
}
5655

57-
private INativeLibraryLoader CreateNativeLoader(OperatingSystemPlatform currentPlatform, string libraryPath)
56+
private INativeLibraryLoader CreateNativeLoader(OperatingSystemPlatform currentPlatform, string libraryPath, string libraryName)
5857
{
5958
switch (currentPlatform)
6059
{
@@ -65,15 +64,15 @@ private INativeLibraryLoader CreateNativeLoader(OperatingSystemPlatform currentP
6564
case OperatingSystemPlatform.Windows:
6665
return new WindowsLibraryLoader(libraryPath);
6766
default:
68-
throw new PlatformNotSupportedException($"Unexpected platform {currentPlatform}.");
67+
throw new PlatformNotSupportedException($"Error loading library {libraryName}: Unexpected platform {currentPlatform}.");
6968
}
7069
}
7170

72-
private void ThrowIfNot64BitProcess()
71+
private void ThrowIfNot64BitProcess(ILibraryLocator libraryLocator)
7372
{
7473
if (!Environment.Is64BitProcess)
7574
{
76-
throw new PlatformNotSupportedException("Native libraries can be loaded only in a 64-bit process.");
75+
throw new PlatformNotSupportedException($"{libraryLocator.LibraryName} can be loaded only in a 64-bit process.");
7776
}
7877
}
7978
}

src/MongoDB.Driver.Core/Core/NativeLibraryLoader/RelativeLibraryLocatorBase.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ internal abstract class RelativeLibraryLocatorBase : ILibraryLocator
2525
// public properties
2626
public virtual bool IsX32ModeSupported => false;
2727

28+
public abstract string LibraryName { get; }
29+
2830
// public methods
2931
public string GetBaseAssemblyDirectory()
3032
{
@@ -39,7 +41,7 @@ public string GetBaseAssemblyDirectory()
3941
public string GetLibraryAbsolutePath(OperatingSystemPlatform currentPlatform)
4042
{
4143
var relativePath = GetLibraryDirectoryRelativePath(currentPlatform);
42-
var libraryName = GetLibraryName(currentPlatform);
44+
var libraryName = GetLibraryFileName(currentPlatform);
4345
return GetAbsolutePath(relativePath, libraryName);
4446
}
4547

@@ -49,7 +51,7 @@ public virtual string GetLibraryDirectoryRelativePath(OperatingSystemPlatform cu
4951
return Path.Combine("runtimes", rid, "native");
5052
}
5153

52-
public abstract string GetLibraryName(OperatingSystemPlatform currentPlatform);
54+
public abstract string GetLibraryFileName(OperatingSystemPlatform currentPlatform);
5355

5456
public virtual string GetCurrentPlatformRuntimeIdentifier(OperatingSystemPlatform currentPlatform)
5557
=> currentPlatform switch

tests/MongoDB.Driver.Core.Tests/Core/NativeLibraryLoader/NativeLibraryLoaderTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,11 @@ public TestRelativeLibraryLocator(string mockedAssemblyUri)
9494
_mockedAssemblyUri = mockedAssemblyUri; // can be null
9595
}
9696

97+
public override string LibraryName => "TestLibraryLocator";
9798
public override string GetBaseAssemblyUri() => _mockedAssemblyUri ?? base.GetBaseAssemblyUri();
9899

99100
// not required for these tests yet
100-
public override string GetLibraryName(OperatingSystemPlatform currentPlatform) => throw new NotImplementedException();
101+
public override string GetLibraryFileName(OperatingSystemPlatform currentPlatform) => throw new NotImplementedException();
101102
}
102103
}
103104
}

0 commit comments

Comments
 (0)