Skip to content

Commit 9054e89

Browse files
[dotnet] Enable NRT on exceptional types (#14672)
1 parent e4ab299 commit 9054e89

34 files changed

+367
-172
lines changed

dotnet/src/webdriver/DefaultFileDetector.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using System.Diagnostics.CodeAnalysis;
21+
22+
#nullable enable
23+
2024
namespace OpenQA.Selenium
2125
{
2226
/// <summary>
@@ -31,7 +35,7 @@ public class DefaultFileDetector : IFileDetector
3135
/// </summary>
3236
/// <param name="keySequence">The sequence to test for file existence.</param>
3337
/// <returns>This method always returns <see langword="false"/> in this implementation.</returns>
34-
public bool IsFile(string keySequence)
38+
public bool IsFile([NotNullWhen(true)] string? keySequence)
3539
{
3640
return false;
3741
}

dotnet/src/webdriver/DetachedShadowRootException.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
// </copyright>
1919

2020
using System;
21-
using System.Runtime.Serialization;
21+
22+
#nullable enable
2223

2324
namespace OpenQA.Selenium
2425
{
@@ -41,7 +42,7 @@ public DetachedShadowRootException()
4142
/// a specified error message.
4243
/// </summary>
4344
/// <param name="message">The message that describes the error.</param>
44-
public DetachedShadowRootException(string message)
45+
public DetachedShadowRootException(string? message)
4546
: base(message)
4647
{
4748
}
@@ -54,7 +55,7 @@ public DetachedShadowRootException(string message)
5455
/// <param name="message">The error message that explains the reason for the exception.</param>
5556
/// <param name="innerException">The exception that is the cause of the current exception,
5657
/// or <see langword="null"/> if no inner exception is specified.</param>
57-
public DetachedShadowRootException(string message, Exception innerException)
58+
public DetachedShadowRootException(string? message, Exception? innerException)
5859
: base(message, innerException)
5960
{
6061
}

dotnet/src/webdriver/DevTools/CommandResponseException.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
using System;
2121

22+
#nullable enable
23+
2224
namespace OpenQA.Selenium.DevTools
2325
{
2426
/// <summary>
@@ -39,7 +41,7 @@ public CommandResponseException()
3941
/// Initializes a new instance of the <see cref="CommandResponseException"/> class with the specified message.
4042
/// </summary>
4143
/// <param name="message">The message of the exception.</param>
42-
public CommandResponseException(string message)
44+
public CommandResponseException(string? message)
4345
: base(message)
4446
{
4547
}
@@ -49,7 +51,7 @@ public CommandResponseException(string message)
4951
/// </summary>
5052
/// <param name="message">The message of the exception.</param>
5153
/// <param name="innerException">The inner exception for this exception.</param>
52-
public CommandResponseException(string message, Exception innerException)
54+
public CommandResponseException(string? message, Exception? innerException)
5355
: base(message, innerException)
5456
{
5557
}

dotnet/src/webdriver/DriverServiceNotFoundException.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
// </copyright>
1919

2020
using System;
21-
using System.Runtime.Serialization;
21+
22+
#nullable enable
2223

2324
namespace OpenQA.Selenium
2425
{
2526
/// <summary>
26-
/// The exception that is thrown when an element is not visible.
27+
/// The exception that is thrown when the driver service is not available.
2728
/// </summary>
2829
[Serializable]
2930
public class DriverServiceNotFoundException : WebDriverException
@@ -41,7 +42,7 @@ public DriverServiceNotFoundException()
4142
/// a specified error message.
4243
/// </summary>
4344
/// <param name="message">The message that describes the error.</param>
44-
public DriverServiceNotFoundException(string message)
45+
public DriverServiceNotFoundException(string? message)
4546
: base(message)
4647
{
4748
}
@@ -54,7 +55,7 @@ public DriverServiceNotFoundException(string message)
5455
/// <param name="message">The error message that explains the reason for the exception.</param>
5556
/// <param name="innerException">The exception that is the cause of the current exception,
5657
/// or <see langword="null"/> if no inner exception is specified.</param>
57-
public DriverServiceNotFoundException(string message, Exception innerException)
58+
public DriverServiceNotFoundException(string? message, Exception? innerException)
5859
: base(message, innerException)
5960
{
6061
}

dotnet/src/webdriver/ElementClickInterceptedException.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
// </copyright>
1919

2020
using System;
21-
using System.Runtime.Serialization;
21+
22+
#nullable enable
2223

2324
namespace OpenQA.Selenium
2425
{
@@ -41,7 +42,7 @@ public ElementClickInterceptedException()
4142
/// a specified error message.
4243
/// </summary>
4344
/// <param name="message">The message that describes the error.</param>
44-
public ElementClickInterceptedException(string message)
45+
public ElementClickInterceptedException(string? message)
4546
: base(message)
4647
{
4748
}
@@ -54,7 +55,7 @@ public ElementClickInterceptedException(string message)
5455
/// <param name="message">The error message that explains the reason for the exception.</param>
5556
/// <param name="innerException">The exception that is the cause of the current exception,
5657
/// or <see langword="null"/> if no inner exception is specified.</param>
57-
public ElementClickInterceptedException(string message, Exception innerException)
58+
public ElementClickInterceptedException(string? message, Exception? innerException)
5859
: base(message, innerException)
5960
{
6061
}

dotnet/src/webdriver/ElementNotInteractableException.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
// </copyright>
1919

2020
using System;
21-
using System.Runtime.Serialization;
21+
22+
#nullable enable
2223

2324
namespace OpenQA.Selenium
2425
{
2526
/// <summary>
26-
/// The exception that is thrown when an element is not visible.
27+
/// The exception that is thrown when an element is not interactable.
2728
/// </summary>
2829
[Serializable]
2930
public class ElementNotInteractableException : InvalidElementStateException
@@ -41,7 +42,7 @@ public ElementNotInteractableException()
4142
/// a specified error message.
4243
/// </summary>
4344
/// <param name="message">The message that describes the error.</param>
44-
public ElementNotInteractableException(string message)
45+
public ElementNotInteractableException(string? message)
4546
: base(message)
4647
{
4748
}
@@ -54,7 +55,7 @@ public ElementNotInteractableException(string message)
5455
/// <param name="message">The error message that explains the reason for the exception.</param>
5556
/// <param name="innerException">The exception that is the cause of the current exception,
5657
/// or <see langword="null"/> if no inner exception is specified.</param>
57-
public ElementNotInteractableException(string message, Exception innerException)
58+
public ElementNotInteractableException(string? message, Exception? innerException)
5859
: base(message, innerException)
5960
{
6061
}

dotnet/src/webdriver/ElementNotSelectableException.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
// </copyright>
1919

2020
using System;
21-
using System.Runtime.Serialization;
21+
22+
#nullable enable
2223

2324
namespace OpenQA.Selenium
2425
{
2526
/// <summary>
26-
/// The exception that is thrown when an element is not visible.
27+
/// The exception that is thrown when an element is not selectable.
2728
/// </summary>
2829
[Serializable]
2930
public class ElementNotSelectableException : InvalidElementStateException
@@ -41,7 +42,7 @@ public ElementNotSelectableException()
4142
/// a specified error message.
4243
/// </summary>
4344
/// <param name="message">The message that describes the error.</param>
44-
public ElementNotSelectableException(string message)
45+
public ElementNotSelectableException(string? message)
4546
: base(message)
4647
{
4748
}
@@ -54,7 +55,7 @@ public ElementNotSelectableException(string message)
5455
/// <param name="message">The error message that explains the reason for the exception.</param>
5556
/// <param name="innerException">The exception that is the cause of the current exception,
5657
/// or <see langword="null"/> if no inner exception is specified.</param>
57-
public ElementNotSelectableException(string message, Exception innerException)
58+
public ElementNotSelectableException(string? message, Exception? innerException)
5859
: base(message, innerException)
5960
{
6061
}

dotnet/src/webdriver/ElementNotVisibleException.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
// </copyright>
1919

2020
using System;
21-
using System.Runtime.Serialization;
21+
22+
#nullable enable
2223

2324
namespace OpenQA.Selenium
2425
{
@@ -41,7 +42,7 @@ public ElementNotVisibleException()
4142
/// a specified error message.
4243
/// </summary>
4344
/// <param name="message">The message that describes the error.</param>
44-
public ElementNotVisibleException(string message)
45+
public ElementNotVisibleException(string? message)
4546
: base(message)
4647
{
4748
}
@@ -54,7 +55,7 @@ public ElementNotVisibleException(string message)
5455
/// <param name="message">The error message that explains the reason for the exception.</param>
5556
/// <param name="innerException">The exception that is the cause of the current exception,
5657
/// or <see langword="null"/> if no inner exception is specified.</param>
57-
public ElementNotVisibleException(string message, Exception innerException)
58+
public ElementNotVisibleException(string? message, Exception? innerException)
5859
: base(message, innerException)
5960
{
6061
}

dotnet/src/webdriver/ErrorResponse.cs

+25-57
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,15 @@
1919

2020
using System.Collections.Generic;
2121

22+
#nullable enable
23+
2224
namespace OpenQA.Selenium
2325
{
2426
/// <summary>
2527
/// Provides a way to store errors from a response
2628
/// </summary>
2729
public class ErrorResponse
2830
{
29-
private StackTraceElement[] stackTrace;
30-
private string message = string.Empty;
31-
private string className = string.Empty;
32-
private string screenshot = string.Empty;
33-
3431
/// <summary>
3532
/// Initializes a new instance of the <see cref="ErrorResponse"/> class.
3633
/// </summary>
@@ -43,58 +40,45 @@ public ErrorResponse()
4340
/// </summary>
4441
/// <param name="responseValue">A <see cref="Dictionary{K, V}"/> containing names and values of
4542
/// the properties of this <see cref="ErrorResponse"/>.</param>
46-
public ErrorResponse(Dictionary<string, object> responseValue)
43+
public ErrorResponse(Dictionary<string, object?>? responseValue)
4744
{
4845
if (responseValue != null)
4946
{
50-
if (responseValue.ContainsKey("message"))
47+
if (responseValue.TryGetValue("message", out object? messageObj)
48+
&& messageObj?.ToString() is string message)
5149
{
52-
if (responseValue["message"] != null)
53-
{
54-
this.message = responseValue["message"].ToString();
55-
}
56-
else
57-
{
58-
this.message = "The error did not contain a message.";
59-
}
50+
this.Message = message;
6051
}
61-
62-
if (responseValue.ContainsKey("screen") && responseValue["screen"] != null)
52+
else
6353
{
64-
this.screenshot = responseValue["screen"].ToString();
54+
this.Message = "The error did not contain a message.";
6555
}
6656

67-
if (responseValue.ContainsKey("class") && responseValue["class"] != null)
57+
if (responseValue.TryGetValue("screen", out object? screenObj))
6858
{
69-
this.className = responseValue["class"].ToString();
59+
this.Screenshot = screenObj?.ToString();
7060
}
7161

72-
if (responseValue.ContainsKey("stackTrace") || responseValue.ContainsKey("stacktrace"))
62+
if (responseValue.TryGetValue("class", out object? classObj))
7363
{
74-
object[] stackTraceArray = null;
75-
76-
if (responseValue.ContainsKey("stackTrace"))
77-
{
78-
stackTraceArray = responseValue["stackTrace"] as object[];
79-
}
80-
else if (responseValue.ContainsKey("stacktrace"))
81-
{
82-
stackTraceArray = responseValue["stacktrace"] as object[];
83-
}
64+
this.ClassName = classObj?.ToString();
65+
}
8466

85-
if (stackTraceArray != null)
67+
if (responseValue.TryGetValue("stackTrace", out object? stackTraceObj)
68+
|| responseValue.TryGetValue("stacktrace", out stackTraceObj))
69+
{
70+
if (stackTraceObj is object?[] stackTraceArray)
8671
{
8772
List<StackTraceElement> stackTraceList = new List<StackTraceElement>();
88-
foreach (object rawStackTraceElement in stackTraceArray)
73+
foreach (object? rawStackTraceElement in stackTraceArray)
8974
{
90-
Dictionary<string, object> elementAsDictionary = rawStackTraceElement as Dictionary<string, object>;
91-
if (elementAsDictionary != null)
75+
if (rawStackTraceElement is Dictionary<string, object?> elementAsDictionary)
9276
{
9377
stackTraceList.Add(new StackTraceElement(elementAsDictionary));
9478
}
9579
}
9680

97-
this.stackTrace = stackTraceList.ToArray();
81+
this.StackTrace = stackTraceList.ToArray();
9882
}
9983
}
10084
}
@@ -103,38 +87,22 @@ public ErrorResponse(Dictionary<string, object> responseValue)
10387
/// <summary>
10488
/// Gets or sets the message from the response
10589
/// </summary>
106-
public string Message
107-
{
108-
get { return this.message; }
109-
set { this.message = value; }
110-
}
90+
public string Message { get; } = string.Empty;
11191

11292
/// <summary>
11393
/// Gets or sets the class name that threw the error
11494
/// </summary>
115-
public string ClassName
116-
{
117-
get { return this.className; }
118-
set { this.className = value; }
119-
}
95+
public string? ClassName { get; }
12096

97+
// TODO: (JimEvans) Change this to return an Image.
12198
/// <summary>
12299
/// Gets or sets the screenshot of the error
123100
/// </summary>
124-
public string Screenshot
125-
{
126-
// TODO: (JimEvans) Change this to return an Image.
127-
get { return this.screenshot; }
128-
set { this.screenshot = value; }
129-
}
101+
public string? Screenshot { get; }
130102

131103
/// <summary>
132104
/// Gets or sets the stack trace of the error
133105
/// </summary>
134-
public StackTraceElement[] StackTrace
135-
{
136-
get { return this.stackTrace; }
137-
set { this.stackTrace = value; }
138-
}
106+
public StackTraceElement[]? StackTrace { get; }
139107
}
140108
}

0 commit comments

Comments
 (0)