Skip to content

Commit

Permalink
[dotnet] Move initialization of error codes to static ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
nvborisenko committed Feb 14, 2024
1 parent 3338c08 commit 8d6bcca
Showing 1 changed file with 17 additions and 26 deletions.
43 changes: 17 additions & 26 deletions dotnet/src/webdriver/WebDriverError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,33 +183,9 @@ internal static class WebDriverError
/// </summary>
public const string UnsupportedOperation = "unsupported operation";

private static Dictionary<string, WebDriverResult> resultMap;
private static object lockObject = new object();
private static readonly Dictionary<string, WebDriverResult> resultMap;

/// <summary>
/// Converts a string error to a <see cref="WebDriverResult"/> value.
/// </summary>
/// <param name="error">The error string to convert.</param>
/// <returns>The converted <see cref="WebDriverResult"/> value.</returns>
public static WebDriverResult ResultFromError(string error)
{
lock (lockObject)
{
if (resultMap == null)
{
InitializeResultMap();
}
}

if (!resultMap.ContainsKey(error))
{
error = UnsupportedOperation;
}

return resultMap[error];
}

private static void InitializeResultMap()
static WebDriverError()
{
resultMap = new Dictionary<string, WebDriverResult>();
resultMap[ElementClickIntercepted] = WebDriverResult.ElementClickIntercepted;
Expand Down Expand Up @@ -244,5 +220,20 @@ private static void InitializeResultMap()
resultMap[UnknownMethod] = WebDriverResult.UnknownCommand;
resultMap[UnsupportedOperation] = WebDriverResult.UnhandledError;
}

/// <summary>
/// Converts a string error to a <see cref="WebDriverResult"/> value.
/// </summary>
/// <param name="error">The error string to convert.</param>
/// <returns>The converted <see cref="WebDriverResult"/> value.</returns>
public static WebDriverResult ResultFromError(string error)
{
if (!resultMap.ContainsKey(error))
{
error = UnsupportedOperation;
}

return resultMap[error];
}
}
}

0 comments on commit 8d6bcca

Please sign in to comment.