Skip to content

Commit 5dc1942

Browse files
committed
CSHARP-1730: Added IsApplicationNameValid method.
1 parent f9d1fc4 commit 5dc1942

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/MongoDB.Driver.Core/Core/Configuration/ConnectionString.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,14 +493,12 @@ private void ParseOption(string name, string value)
493493
switch (name.ToLower())
494494
{
495495
case "appname":
496-
try
496+
string invalidApplicationNameMessage;
497+
if (!ApplicationNameHelper.IsApplicationNameValid(value, out invalidApplicationNameMessage))
497498
{
498-
_applicationName = ApplicationNameHelper.EnsureApplicationNameIsValid(value, nameof(value));
499-
}
500-
catch (Exception ex)
501-
{
502-
throw new MongoConfigurationException(ex.Message, ex);
499+
throw new MongoConfigurationException(invalidApplicationNameMessage);
503500
}
501+
_applicationName = value;
504502
break;
505503
case "authmechanism":
506504
_authMechanism = value;

src/MongoDB.Shared/ApplicationNameHelper.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,30 @@ namespace MongoDB.Shared
2121
internal static class ApplicationNameHelper
2222
{
2323
public static string EnsureApplicationNameIsValid(string applicationName, string paramName)
24+
{
25+
string message;
26+
if (!IsApplicationNameValid(applicationName, out message))
27+
{
28+
throw new ArgumentException(message, paramName);
29+
}
30+
31+
return applicationName;
32+
}
33+
34+
public static bool IsApplicationNameValid(string applicationName, out string message)
2435
{
2536
if (applicationName != null)
2637
{
2738
var utf8 = Utf8Encodings.Strict.GetBytes(applicationName);
2839
if (utf8.Length > 128)
2940
{
30-
throw new ArgumentException("Application name exceeds 128 bytes after encoding to UTF8.", paramName);
41+
message = "Application name exceeds 128 bytes after encoding to UTF8.";
42+
return false;
3143
}
3244
}
3345

34-
return applicationName;
46+
message = null;
47+
return true;
3548
}
3649
}
3750
}

0 commit comments

Comments
 (0)