19
19
20
20
using System . Collections . Generic ;
21
21
22
+ #nullable enable
23
+
22
24
namespace OpenQA . Selenium
23
25
{
24
26
/// <summary>
25
27
/// Provides a way to store errors from a response
26
28
/// </summary>
27
29
public class ErrorResponse
28
30
{
29
- private StackTraceElement [ ] stackTrace ;
30
- private string message = string . Empty ;
31
- private string className = string . Empty ;
32
- private string screenshot = string . Empty ;
33
-
34
31
/// <summary>
35
32
/// Initializes a new instance of the <see cref="ErrorResponse"/> class.
36
33
/// </summary>
@@ -43,58 +40,45 @@ public ErrorResponse()
43
40
/// </summary>
44
41
/// <param name="responseValue">A <see cref="Dictionary{K, V}"/> containing names and values of
45
42
/// the properties of this <see cref="ErrorResponse"/>.</param>
46
- public ErrorResponse ( Dictionary < string , object > responseValue )
43
+ public ErrorResponse ( Dictionary < string , object ? > ? responseValue )
47
44
{
48
45
if ( responseValue != null )
49
46
{
50
- if ( responseValue . ContainsKey ( "message" ) )
47
+ if ( responseValue . TryGetValue ( "message" , out object ? messageObj )
48
+ && messageObj ? . ToString ( ) is string message )
51
49
{
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 ;
60
51
}
61
-
62
- if ( responseValue . ContainsKey ( "screen" ) && responseValue [ "screen" ] != null )
52
+ else
63
53
{
64
- this . screenshot = responseValue [ "screen" ] . ToString ( ) ;
54
+ this . Message = "The error did not contain a message." ;
65
55
}
66
56
67
- if ( responseValue . ContainsKey ( "class" ) && responseValue [ "class" ] != null )
57
+ if ( responseValue . TryGetValue ( "screen" , out object ? screenObj ) )
68
58
{
69
- this . className = responseValue [ "class" ] . ToString ( ) ;
59
+ this . Screenshot = screenObj ? . ToString ( ) ;
70
60
}
71
61
72
- if ( responseValue . ContainsKey ( "stackTrace" ) || responseValue . ContainsKey ( "stacktrace" ) )
62
+ if ( responseValue . TryGetValue ( "class" , out object ? classObj ) )
73
63
{
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
+ }
84
66
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 )
86
71
{
87
72
List < StackTraceElement > stackTraceList = new List < StackTraceElement > ( ) ;
88
- foreach ( object rawStackTraceElement in stackTraceArray )
73
+ foreach ( object ? rawStackTraceElement in stackTraceArray )
89
74
{
90
- Dictionary < string , object > elementAsDictionary = rawStackTraceElement as Dictionary < string , object > ;
91
- if ( elementAsDictionary != null )
75
+ if ( rawStackTraceElement is Dictionary < string , object ? > elementAsDictionary )
92
76
{
93
77
stackTraceList . Add ( new StackTraceElement ( elementAsDictionary ) ) ;
94
78
}
95
79
}
96
80
97
- this . stackTrace = stackTraceList . ToArray ( ) ;
81
+ this . StackTrace = stackTraceList . ToArray ( ) ;
98
82
}
99
83
}
100
84
}
@@ -103,38 +87,22 @@ public ErrorResponse(Dictionary<string, object> responseValue)
103
87
/// <summary>
104
88
/// Gets or sets the message from the response
105
89
/// </summary>
106
- public string Message
107
- {
108
- get { return this . message ; }
109
- set { this . message = value ; }
110
- }
90
+ public string Message { get ; } = string . Empty ;
111
91
112
92
/// <summary>
113
93
/// Gets or sets the class name that threw the error
114
94
/// </summary>
115
- public string ClassName
116
- {
117
- get { return this . className ; }
118
- set { this . className = value ; }
119
- }
95
+ public string ? ClassName { get ; }
120
96
97
+ // TODO: (JimEvans) Change this to return an Image.
121
98
/// <summary>
122
99
/// Gets or sets the screenshot of the error
123
100
/// </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 ; }
130
102
131
103
/// <summary>
132
104
/// Gets or sets the stack trace of the error
133
105
/// </summary>
134
- public StackTraceElement [ ] StackTrace
135
- {
136
- get { return this . stackTrace ; }
137
- set { this . stackTrace = value ; }
138
- }
106
+ public StackTraceElement [ ] ? StackTrace { get ; }
139
107
}
140
108
}
0 commit comments