forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsErrorHelpers.cs
More file actions
280 lines (219 loc) · 8.36 KB
/
JsErrorHelpers.cs
File metadata and controls
280 lines (219 loc) · 8.36 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
namespace JavaScriptEngineSwitcher.ChakraCore.JsRt
{
/// <summary>
/// Error helpers
/// </summary>
internal static class JsErrorHelpers
{
/// <summary>
/// Throws if a native method returns an error code
/// </summary>
/// <param name="error">The error</param>
public static void ThrowIfError(JsErrorCode error)
{
if (error != JsErrorCode.NoError)
{
switch (error)
{
#region Usage
case JsErrorCode.InvalidArgument:
throw new JsUsageException(error, "Invalid argument.");
case JsErrorCode.NullArgument:
throw new JsUsageException(error, "Null argument.");
case JsErrorCode.NoCurrentContext:
throw new JsUsageException(error, "No current context.");
case JsErrorCode.InExceptionState:
throw new JsUsageException(error, "Runtime is in exception state.");
case JsErrorCode.NotImplemented:
throw new JsUsageException(error, "Method is not implemented.");
case JsErrorCode.WrongThread:
throw new JsUsageException(error, "Runtime is active on another thread.");
case JsErrorCode.RuntimeInUse:
throw new JsUsageException(error, "Runtime is in use.");
case JsErrorCode.BadSerializedScript:
throw new JsUsageException(error, "Bad serialized script.");
case JsErrorCode.InDisabledState:
throw new JsUsageException(error, "Runtime is disabled.");
case JsErrorCode.CannotDisableExecution:
throw new JsUsageException(error, "Cannot disable execution.");
case JsErrorCode.HeapEnumInProgress:
throw new JsUsageException(error, "Heap enumeration is in progress.");
case JsErrorCode.ArgumentNotObject:
throw new JsUsageException(error, "Argument is not an object.");
case JsErrorCode.InProfileCallback:
throw new JsUsageException(error, "In a profile callback.");
case JsErrorCode.InThreadServiceCallback:
throw new JsUsageException(error, "In a thread service callback.");
case JsErrorCode.CannotSerializeDebugScript:
throw new JsUsageException(error, "Cannot serialize a debug script.");
case JsErrorCode.AlreadyDebuggingContext:
throw new JsUsageException(error, "Context is already in debug mode.");
case JsErrorCode.AlreadyProfilingContext:
throw new JsUsageException(error, "Already profiling this context.");
case JsErrorCode.IdleNotEnabled:
throw new JsUsageException(error, "Idle is not enabled.");
case JsErrorCode.CannotSetProjectionEnqueueCallback:
throw new JsUsageException(error, "Cannot set projection enqueue callback.");
case JsErrorCode.CannotStartProjection:
throw new JsUsageException(error, "Cannot start projection.");
case JsErrorCode.InObjectBeforeCollectCallback:
throw new JsUsageException(error, "In object before collect callback.");
case JsErrorCode.ObjectNotInspectable:
throw new JsUsageException(error, "Object not inspectable.");
case JsErrorCode.PropertyNotSymbol:
throw new JsUsageException(error, "Property not symbol.");
case JsErrorCode.PropertyNotString:
throw new JsUsageException(error, "Property not string.");
case JsErrorCode.InvalidContext:
throw new JsUsageException(error, "Invalid context.");
case JsErrorCode.InvalidModuleHostInfoKind:
throw new JsUsageException(error, "Invalid module host info kind.");
case JsErrorCode.ModuleParsed:
throw new JsUsageException(error, "Module parsed.");
case JsErrorCode.ModuleEvaluated:
throw new JsUsageException(error, "Module evaluated.");
#endregion
#region Engine
case JsErrorCode.OutOfMemory:
throw new JsEngineException(error, "Out of memory.");
case JsErrorCode.BadFPUState:
throw new JsEngineException(error, "Bad the Floating Point Unit state.");
#endregion
#region Script
case JsErrorCode.ScriptException:
{
JsValue errorMetadata;
JsErrorCode innerError = NativeMethods.JsGetAndClearExceptionWithMetadata(out errorMetadata);
if (innerError != JsErrorCode.NoError)
{
throw new JsFatalException(innerError);
}
throw new JsScriptException(error, errorMetadata, "Script threw an exception.");
}
case JsErrorCode.ScriptCompile:
{
JsValue errorMetadata;
JsErrorCode innerError = NativeMethods.JsGetAndClearExceptionWithMetadata(out errorMetadata);
if (innerError != JsErrorCode.NoError)
{
throw new JsFatalException(innerError);
}
throw new JsScriptException(error, errorMetadata, "Compile error.");
}
case JsErrorCode.ScriptTerminated:
throw new JsScriptException(error, JsValue.Invalid, "Script was terminated.");
case JsErrorCode.ScriptEvalDisabled:
throw new JsScriptException(error, JsValue.Invalid, "Eval of strings is disabled in this runtime.");
#endregion
#region Fatal
case JsErrorCode.Fatal:
throw new JsFatalException(error, "Fatal error.");
case JsErrorCode.WrongRuntime:
throw new JsFatalException(error, "Wrong runtime.");
#endregion
default:
throw new JsFatalException(error);
}
}
}
/// <summary>
/// Creates a new JavaScript error object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="message">The message that describes the error</param>
/// <returns>The new error object</returns>
public static JsValue CreateError(string message)
{
JsValue messageValue = JsValue.FromString(message);
JsValue errorValue = JsValue.CreateError(messageValue);
return errorValue;
}
/// <summary>
/// Creates a new JavaScript RangeError error object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="message">The message that describes the error</param>
/// <returns>The new error object</returns>
public static JsValue CreateRangeError(string message)
{
JsValue messageValue = JsValue.FromString(message);
JsValue errorValue = JsValue.CreateRangeError(messageValue);
return errorValue;
}
/// <summary>
/// Creates a new JavaScript ReferenceError error object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="message">The message that describes the error</param>
/// <returns>The new error object</returns>
public static JsValue CreateReferenceError(string message)
{
JsValue messageValue = JsValue.FromString(message);
JsValue errorValue = JsValue.CreateReferenceError(messageValue);
return errorValue;
}
/// <summary>
/// Creates a new JavaScript SyntaxError error object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="message">The message that describes the error</param>
/// <returns>The new error object</returns>
public static JsValue CreateSyntaxError(string message)
{
JsValue messageValue = JsValue.FromString(message);
JsValue errorValue = JsValue.CreateSyntaxError(messageValue);
return errorValue;
}
/// <summary>
/// Creates a new JavaScript TypeError error object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="message">The message that describes the error</param>
/// <returns>The new error object</returns>
public static JsValue CreateTypeError(string message)
{
JsValue messageValue = JsValue.FromString(message);
JsValue errorValue = JsValue.CreateTypeError(messageValue);
return errorValue;
}
/// <summary>
/// Creates a new JavaScript URIError error object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="message">The message that describes the error</param>
/// <returns>The new error object</returns>
public static JsValue CreateUriError(string message)
{
JsValue messageValue = JsValue.FromString(message);
JsValue errorValue = JsValue.CreateUriError(messageValue);
return errorValue;
}
/// <summary>
/// Sets a exception
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="exception">The error object</param>
public static void SetException(JsValue exception)
{
JsErrorCode innerError = NativeMethods.JsSetException(exception);
if (innerError != JsErrorCode.NoError)
{
throw new JsFatalException(innerError);
}
}
}
}