forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptInterruptedException.cs
More file actions
189 lines (159 loc) · 6.9 KB
/
ScriptInterruptedException.cs
File metadata and controls
189 lines (159 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Runtime.Serialization;
using Microsoft.ClearScript.Util;
namespace Microsoft.ClearScript
{
/// <summary>
/// The exception that is thrown when script execution is interrupted by the host.
/// </summary>
[Serializable]
public class ScriptInterruptedException : OperationCanceledException, IScriptEngineException
{
private readonly string engineName;
private const string engineNameItemName = "ScriptEngineName";
private readonly string errorDetails;
private const string errorDetailsItemName = "ScriptErrorDetails";
private readonly bool isFatal;
private const string isFatalItemName = "IsFatal";
private readonly bool executionStarted;
private const string executionStartedItemName = "ExecutionStarted";
private readonly object scriptException;
private const string defaultMessage = "Script execution was interrupted";
#region constructors
/// <summary>
/// Initializes a new <see cref="ScriptInterruptedException"/> instance.
/// </summary>
public ScriptInterruptedException()
: base(defaultMessage)
{
// ReSharper disable once RedundantBaseQualifier
errorDetails = base.Message;
}
/// <summary>
/// Initializes a new <see cref="ScriptInterruptedException"/> with the specified error message.
/// </summary>
/// <param name="message">The error message.</param>
public ScriptInterruptedException(string message)
: base(MiscHelpers.EnsureNonBlank(message, defaultMessage))
{
// ReSharper disable once RedundantBaseQualifier
errorDetails = base.Message;
}
/// <summary>
/// Initializes a new <see cref="ScriptInterruptedException"/> with the specified error message and nested exception.
/// </summary>
/// <param name="message">The error message.</param>
/// <param name="innerException">The exception that caused the current exception to be thrown.</param>
public ScriptInterruptedException(string message, Exception innerException)
: base(MiscHelpers.EnsureNonBlank(message, defaultMessage), innerException)
{
// ReSharper disable once RedundantBaseQualifier
errorDetails = base.Message;
}
/// <summary>
/// Initializes a new <see cref="ScriptInterruptedException"/> with serialized data.
/// </summary>
/// <param name="info">The object that holds the serialized data.</param>
/// <param name="context">The contextual information about the source or destination.</param>
protected ScriptInterruptedException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
engineName = info.GetString(engineNameItemName);
errorDetails = info.GetString(errorDetailsItemName);
isFatal = info.GetBoolean(isFatalItemName);
executionStarted = info.GetBoolean(executionStartedItemName);
}
internal ScriptInterruptedException(string engineName, string message, string errorDetails, int errorCode, bool isFatal, bool executionStarted, object scriptException, Exception innerException)
: base(MiscHelpers.EnsureNonBlank(message, defaultMessage), innerException)
{
this.engineName = engineName;
// ReSharper disable once RedundantBaseQualifier
this.errorDetails = MiscHelpers.EnsureNonBlank(errorDetails, base.Message);
this.isFatal = isFatal;
this.executionStarted = executionStarted;
this.scriptException = scriptException;
if (errorCode != 0)
{
HResult = errorCode;
}
}
#endregion
#region IScriptEngineException implementation
/// <summary>
/// Gets an <see href="http://en.wikipedia.org/wiki/HRESULT">HRESULT</see> error code if one is available, zero otherwise.
/// </summary>
int IScriptEngineException.HResult
{
get { return HResult; }
}
/// <summary>
/// Gets the name associated with the script engine instance.
/// </summary>
public string EngineName
{
get { return engineName; }
}
/// <summary>
/// Gets a detailed error message if one is available, <c>null</c> otherwise.
/// </summary>
public string ErrorDetails
{
get { return errorDetails; }
}
/// <summary>
/// Gets a value that indicates whether the exception represents a fatal error.
/// </summary>
public bool IsFatal
{
get { return isFatal; }
}
/// <summary>
/// Gets a value that indicates whether script code execution had started before the current exception was thrown.
/// </summary>
public bool ExecutionStarted
{
get { return executionStarted; }
}
/// <summary>
/// Gets the script exception that caused the current exception to be thrown, or <c>null</c> if one was not specified.
/// </summary>
public dynamic ScriptException
{
get { return scriptException; }
}
#endregion
#region Object overrides
/// <summary>
/// Returns a string that represents the current exception.
/// </summary>
/// <returns>A string that represents the current exception.</returns>
public override string ToString()
{
var result = base.ToString();
if (!string.IsNullOrEmpty(errorDetails) && (errorDetails != Message))
{
var details = " " + errorDetails.Replace("\n", "\n ");
result += "\n --- Script error details follow ---\n" + details;
}
return result;
}
#endregion
#region OperationCanceledException overrides
/// <summary>
/// Populates a <see cref="SerializationInfo"/> with the data needed to serialize the target object.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo"/> to populate with data.</param>
/// <param name="context">The destination (see <see cref="StreamingContext"/>) for this serialization.</param>
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue(engineNameItemName, engineName);
info.AddValue(errorDetailsItemName, errorDetails);
info.AddValue(isFatalItemName, isFatal);
info.AddValue(executionStartedItemName, executionStarted);
}
#endregion
}
}