-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathScriptEngineException.cs
More file actions
172 lines (141 loc) · 6.64 KB
/
ScriptEngineException.cs
File metadata and controls
172 lines (141 loc) · 6.64 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
// 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 an error occurs during script execution or script object access.
/// </summary>
[Serializable]
public class ScriptEngineException : InvalidOperationException, 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 = "An error occurred during script execution";
#region constructors
/// <summary>
/// Initializes a new <c><see cref="ScriptEngineException"/></c> instance.
/// </summary>
public ScriptEngineException()
: base(defaultMessage)
{
errorDetails = base.Message;
}
/// <summary>
/// Initializes a new <c><see cref="ScriptEngineException"/></c> with the specified error message.
/// </summary>
/// <param name="message">The error message.</param>
public ScriptEngineException(string message)
: base(message.ToNonBlank(defaultMessage))
{
errorDetails = base.Message;
}
/// <summary>
/// Initializes a new <c><see cref="ScriptEngineException"/></c> 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 ScriptEngineException(string message, Exception innerException)
: base(message.ToNonBlank(defaultMessage), innerException)
{
errorDetails = base.Message;
}
/// <summary>
/// Initializes a new <c><see cref="ScriptEngineException"/></c> 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 ScriptEngineException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
engineName = info.GetString(engineNameItemName);
errorDetails = info.GetString(errorDetailsItemName);
isFatal = info.GetBoolean(isFatalItemName);
executionStarted = info.GetBoolean(executionStartedItemName);
}
internal ScriptEngineException(string engineName, string message, string errorDetails, int errorCode, bool isFatal, bool executionStarted, object scriptException, Exception innerException)
: base(message.ToNonBlank(defaultMessage), innerException)
{
this.engineName = engineName;
this.errorDetails = errorDetails.ToNonBlank(base.Message);
this.isFatal = isFatal;
this.executionStarted = executionStarted;
this.scriptException = scriptException;
if (errorCode != 0)
{
HResult = errorCode;
}
}
#endregion
#region IScriptEngineException implementation
/// <summary>
/// Gets an <c><see href="http://en.wikipedia.org/wiki/HRESULT">HRESULT</see></c> error code if one is available, zero otherwise.
/// </summary>
int IScriptEngineException.HResult => HResult;
/// <summary>
/// Gets the name associated with the script engine instance.
/// </summary>
public string EngineName => engineName;
/// <summary>
/// Gets a detailed error message if one is available, <c>null</c> otherwise.
/// </summary>
public string ErrorDetails => errorDetails;
/// <summary>
/// Gets a value that indicates whether the exception represents a fatal error.
/// </summary>
public bool IsFatal => isFatal;
/// <summary>
/// Gets a value that indicates whether script code execution had started before the current exception was thrown.
/// </summary>
public bool ExecutionStarted => 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 => scriptException;
/// <summary>
/// Gets the script exception that caused the current exception to be thrown, or <c>null</c> if one was not specified, without engaging the dynamic infrastructure.
/// </summary>
public object ScriptExceptionAsObject => 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 InvalidOperationException overrides
/// <summary>
/// Populates a <c><see cref="SerializationInfo"/></c> with the data needed to serialize the target object.
/// </summary>
/// <param name="info">The <c><see cref="SerializationInfo"/></c> to populate with data.</param>
/// <param name="context">The destination (see <c><see cref="StreamingContext"/></c>) 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
}
}