Skip to content

Commit fea0eb3

Browse files
committed
Fixed a error #22 “Make Exception serializable”
1 parent 2d26f69 commit fea0eb3

File tree

18 files changed

+404
-120
lines changed

18 files changed

+404
-120
lines changed

src/JavaScriptEngineSwitcher.ChakraCore/ChakraCoreJsEngine.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,7 @@ private static JsRuntimeException ConvertJsExceptionToJsRuntimeException(
903903
Category = category,
904904
LineNumber = lineNumber,
905905
ColumnNumber = columnNumber,
906-
SourceFragment = sourceFragment,
907-
HelpLink = jsException.HelpLink
906+
SourceFragment = sourceFragment
908907
};
909908

910909
return jsEngineException;
Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,45 @@
1+
#if !NETSTANDARD1_3
2+
using System;
3+
using System.Runtime.Serialization;
4+
#endif
5+
16
namespace JavaScriptEngineSwitcher.ChakraCore.JsRt
27
{
38
/// <summary>
49
/// The exception that occurred in the workings of the JavaScript engine itself
510
/// </summary>
11+
#if !NETSTANDARD1_3
12+
[Serializable]
13+
#endif
614
internal sealed class JsEngineException : JsException
715
{
816
/// <summary>
917
/// Initializes a new instance of the <see cref="JsEngineException"/> class
1018
/// </summary>
11-
/// <param name="code">The error code returned</param>
12-
public JsEngineException(JsErrorCode code)
13-
: this(code, "A fatal exception has occurred in a JavaScript runtime")
19+
/// <param name="errorCode">The error code returned</param>
20+
public JsEngineException(JsErrorCode errorCode)
21+
: this(errorCode, "A fatal exception has occurred in a JavaScript runtime")
1422
{ }
1523

1624
/// <summary>
1725
/// Initializes a new instance of the <see cref="JsEngineException"/> class
26+
/// with a specified error message
1827
/// </summary>
19-
/// <param name="code">The error code returned</param>
28+
/// <param name="errorCode">The error code returned</param>
2029
/// <param name="message">The error message</param>
21-
public JsEngineException(JsErrorCode code, string message)
22-
: base(code, message)
30+
public JsEngineException(JsErrorCode errorCode, string message)
31+
: base(errorCode, message)
32+
{ }
33+
#if !NETSTANDARD1_3
34+
35+
/// <summary>
36+
/// Initializes a new instance of the <see cref="JsEngineException"/> class with serialized data
37+
/// </summary>
38+
/// <param name="info">The object that holds the serialized data</param>
39+
/// <param name="context">The contextual information about the source or destination</param>
40+
private JsEngineException(SerializationInfo info, StreamingContext context)
41+
: base(info, context)
2342
{ }
43+
#endif
2444
}
2545
}
Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,89 @@
11
using System;
2+
#if !NETSTANDARD1_3
3+
using System.Runtime.Serialization;
4+
using System.Security.Permissions;
5+
#endif
26

37
namespace JavaScriptEngineSwitcher.ChakraCore.JsRt
48
{
59
/// <summary>
610
/// The exception returned from the Chakra engine
711
/// </summary>
12+
#if !NETSTANDARD1_3
13+
[Serializable]
14+
#endif
815
internal class JsException : Exception
916
{
1017
/// <summary>
1118
/// The error code
1219
/// </summary>
13-
private readonly JsErrorCode _code;
20+
private readonly JsErrorCode _errorCode;
1421

1522
/// <summary>
1623
/// Gets a error code
1724
/// </summary>
1825
public JsErrorCode ErrorCode
1926
{
20-
get { return _code; }
27+
get { return _errorCode; }
2128
}
2229

2330

2431
/// <summary>
2532
/// Initializes a new instance of the <see cref="JsException"/> class
2633
/// </summary>
27-
/// <param name="code">The error code returned</param>
28-
public JsException(JsErrorCode code)
29-
: this(code, "A fatal exception has occurred in a JavaScript runtime")
34+
/// <param name="errorCode">The error code returned</param>
35+
public JsException(JsErrorCode errorCode)
36+
: this(errorCode, "A fatal exception has occurred in a JavaScript runtime")
3037
{ }
3138

3239
/// <summary>
3340
/// Initializes a new instance of the <see cref="JsException"/> class
41+
/// with a specified error message
3442
/// </summary>
35-
/// <param name="code">The error code returned</param>
43+
/// <param name="errorCode">The error code returned</param>
3644
/// <param name="message">The error message</param>
37-
public JsException(JsErrorCode code, string message)
45+
public JsException(JsErrorCode errorCode, string message)
3846
: base(message)
3947
{
40-
_code = code;
48+
_errorCode = errorCode;
4149
}
50+
#if !NETSTANDARD1_3
4251

4352
/// <summary>
44-
/// Initializes a new instance of the <see cref="JsException"/> class
53+
/// Initializes a new instance of the <see cref="JsException"/> class with serialized data
4554
/// </summary>
46-
/// <param name="message">The error message</param>
47-
/// <param name="innerException">The exception that is the cause of the current exception</param>
48-
protected JsException(string message, Exception innerException)
49-
: base(message, innerException)
55+
/// <param name="info">The object that holds the serialized data</param>
56+
/// <param name="context">The contextual information about the source or destination</param>
57+
protected JsException(SerializationInfo info, StreamingContext context)
58+
: base(info, context)
5059
{
51-
if (message != null)
60+
if (info != null)
5261
{
53-
_code = (JsErrorCode) HResult;
62+
_errorCode = (JsErrorCode)info.GetUInt32("ErrorCode");
5463
}
5564
}
65+
66+
67+
#region Exception overrides
68+
69+
/// <summary>
70+
/// Populates a <see cref="SerializationInfo"/> with the data needed to serialize the target object
71+
/// </summary>
72+
/// <param name="info">The <see cref="SerializationInfo"/> to populate with data</param>
73+
/// <param name="context">The destination (see <see cref="StreamingContext"/>) for this serialization</param>
74+
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
75+
public override void GetObjectData(SerializationInfo info, StreamingContext context)
76+
{
77+
if (info == null)
78+
{
79+
throw new ArgumentNullException("info");
80+
}
81+
82+
base.GetObjectData(info, context);
83+
info.AddValue("ErrorCode", (uint)_errorCode);
84+
}
85+
86+
#endregion
87+
#endif
5688
}
5789
}
Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,45 @@
1-
namespace JavaScriptEngineSwitcher.ChakraCore.JsRt
1+
#if !NETSTANDARD1_3
2+
using System;
3+
using System.Runtime.Serialization;
4+
#endif
5+
6+
namespace JavaScriptEngineSwitcher.ChakraCore.JsRt
27
{
38
/// <summary>
49
/// The fatal exception occurred
510
/// </summary>
11+
#if !NETSTANDARD1_3
12+
[Serializable]
13+
#endif
614
internal sealed class JsFatalException : JsException
715
{
816
/// <summary>
917
/// Initializes a new instance of the <see cref="JsFatalException"/> class
1018
/// </summary>
11-
/// <param name="code">The error code returned</param>
12-
public JsFatalException(JsErrorCode code)
13-
: this(code, "A fatal exception has occurred in a JavaScript runtime")
19+
/// <param name="errorCode">The error code returned</param>
20+
public JsFatalException(JsErrorCode errorCode)
21+
: this(errorCode, "A fatal exception has occurred in a JavaScript runtime")
1422
{ }
1523

1624
/// <summary>
1725
/// Initializes a new instance of the <see cref="JsFatalException"/> class
26+
/// with a specified error message
1827
/// </summary>
19-
/// <param name="code">The error code returned</param>
28+
/// <param name="errorCode">The error code returned</param>
2029
/// <param name="message">The error message</param>
21-
public JsFatalException(JsErrorCode code, string message)
22-
: base(code, message)
30+
public JsFatalException(JsErrorCode errorCode, string message)
31+
: base(errorCode, message)
32+
{ }
33+
#if !NETSTANDARD1_3
34+
35+
/// <summary>
36+
/// Initializes a new instance of the <see cref="JsFatalException"/> class with serialized data
37+
/// </summary>
38+
/// <param name="info">The object that holds the serialized data</param>
39+
/// <param name="context">The contextual information about the source or destination</param>
40+
private JsFatalException(SerializationInfo info, StreamingContext context)
41+
: base(info, context)
2342
{ }
43+
#endif
2444
}
2545
}
Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,66 @@
1+
#if !NETSTANDARD1_3
12
using System;
3+
using System.Runtime.Serialization;
4+
#endif
25

36
namespace JavaScriptEngineSwitcher.ChakraCore.JsRt
47
{
58
/// <summary>
69
/// The script exception
710
/// </summary>
11+
#if !NETSTANDARD1_3
12+
[Serializable]
13+
#endif
814
internal sealed class JsScriptException : JsException
915
{
1016
/// <summary>
1117
/// The error
1218
/// </summary>
19+
#if !NETSTANDARD1_3
20+
[NonSerialized]
21+
#endif
1322
private readonly JsValue _error;
1423

1524
/// <summary>
1625
/// Gets a JavaScript object representing the script error
1726
/// </summary>
1827
public JsValue Error
1928
{
20-
get
21-
{
22-
return _error;
23-
}
29+
get { return _error; }
2430
}
2531

2632

2733
/// <summary>
2834
/// Initializes a new instance of the <see cref="JsScriptException"/> class
2935
/// </summary>
30-
/// <param name="code">The error code returned</param>
36+
/// <param name="errorCode">The error code returned</param>
3137
/// <param name="error">The JavaScript error object</param>
32-
public JsScriptException(JsErrorCode code, JsValue error)
33-
: this(code, error, "JavaScript Exception")
38+
public JsScriptException(JsErrorCode errorCode, JsValue error)
39+
: this(errorCode, error, "JavaScript Exception")
3440
{ }
3541

3642
/// <summary>
3743
/// Initializes a new instance of the <see cref="JsScriptException"/> class
44+
/// with a specified error message
3845
/// </summary>
39-
/// <param name="code">The error code returned</param>
46+
/// <param name="errorCode">The error code returned</param>
4047
/// <param name="error">The JavaScript error object</param>
4148
/// <param name="message">The error message</param>
42-
public JsScriptException(JsErrorCode code, JsValue error, string message)
43-
: base(code, message)
49+
public JsScriptException(JsErrorCode errorCode, JsValue error, string message)
50+
: base(errorCode, message)
4451
{
4552
_error = error;
4653
}
54+
#if !NETSTANDARD1_3
4755

4856
/// <summary>
49-
/// Initializes a new instance of the <see cref="JsScriptException"/> class
57+
/// Initializes a new instance of the <see cref="JsScriptException"/> class with serialized data
5058
/// </summary>
51-
/// <param name="message">The error message</param>
52-
/// <param name="innerException">The exception that is the cause of the current exception</param>
53-
private JsScriptException(string message, Exception innerException)
54-
: base(message, innerException)
59+
/// <param name="info">The object that holds the serialized data</param>
60+
/// <param name="context">The contextual information about the source or destination</param>
61+
private JsScriptException(SerializationInfo info, StreamingContext context)
62+
: base(info, context)
5563
{ }
64+
#endif
5665
}
5766
}
Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,45 @@
1-
namespace JavaScriptEngineSwitcher.ChakraCore.JsRt
1+
#if !NETSTANDARD1_3
2+
using System;
3+
using System.Runtime.Serialization;
4+
#endif
5+
6+
namespace JavaScriptEngineSwitcher.ChakraCore.JsRt
27
{
38
/// <summary>
49
/// The API usage exception occurred
510
/// </summary>
11+
#if !NETSTANDARD1_3
12+
[Serializable]
13+
#endif
614
internal sealed class JsUsageException : JsException
715
{
816
/// <summary>
917
/// Initializes a new instance of the <see cref="JsUsageException"/> class
1018
/// </summary>
11-
/// <param name="code">The error code returned</param>
12-
public JsUsageException(JsErrorCode code)
13-
: this(code, "A fatal exception has occurred in a JavaScript runtime")
19+
/// <param name="errorCode">The error code returned</param>
20+
public JsUsageException(JsErrorCode errorCode)
21+
: this(errorCode, "A fatal exception has occurred in a JavaScript runtime")
1422
{ }
1523

1624
/// <summary>
1725
/// Initializes a new instance of the <see cref="JsUsageException"/> class
26+
/// with a specified error message
1827
/// </summary>
19-
/// <param name="code">The error code returned</param>
28+
/// <param name="errorCode">The error code returned</param>
2029
/// <param name="message">The error message</param>
21-
public JsUsageException(JsErrorCode code, string message)
22-
: base(code, message)
30+
public JsUsageException(JsErrorCode errorCode, string message)
31+
: base(errorCode, message)
32+
{ }
33+
#if !NETSTANDARD1_3
34+
35+
/// <summary>
36+
/// Initializes a new instance of the <see cref="JsUsageException"/> class with serialized data
37+
/// </summary>
38+
/// <param name="info">The object that holds the serialized data</param>
39+
/// <param name="context">The contextual information about the source or destination</param>
40+
private JsUsageException(SerializationInfo info, StreamingContext context)
41+
: base(info, context)
2342
{ }
43+
#endif
2444
}
2545
}

src/JavaScriptEngineSwitcher.Core/EmptyValueException.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
using System;
2+
#if !NETSTANDARD1_3
3+
using System.Runtime.Serialization;
4+
#endif
25

36
namespace JavaScriptEngineSwitcher.Core
47
{
58
/// <summary>
69
/// The exception that is thrown when a specified value is null or empty
710
/// </summary>
11+
#if !NETSTANDARD1_3
12+
[Serializable]
13+
#endif
814
public sealed class EmptyValueException : Exception
915
{
1016
/// <summary>
@@ -25,5 +31,16 @@ public EmptyValueException(string message)
2531
public EmptyValueException(string message, Exception innerException)
2632
: base(message, innerException)
2733
{ }
34+
#if !NETSTANDARD1_3
35+
36+
/// <summary>
37+
/// Initializes a new instance of the <see cref="EmptyValueException"/> class with serialized data
38+
/// </summary>
39+
/// <param name="info">The object that holds the serialized data</param>
40+
/// <param name="context">The contextual information about the source or destination</param>
41+
private EmptyValueException(SerializationInfo info, StreamingContext context)
42+
: base(info, context)
43+
{ }
44+
#endif
2845
}
2946
}

0 commit comments

Comments
 (0)