12
12
/// let jsErrorValue = error.thrownValue
13
13
/// }
14
14
/// ```
15
- public struct JSException : Error , Equatable {
15
+ public struct JSException : Error , Equatable , CustomStringConvertible {
16
16
/// The value thrown from JavaScript.
17
17
/// This can be any JavaScript value (error object, string, number, etc.).
18
18
public var thrownValue : JSValue {
@@ -25,10 +25,25 @@ public struct JSException: Error, Equatable {
25
25
/// from `Error` protocol.
26
26
private nonisolated ( unsafe) let _thrownValue : JSValue
27
27
28
+ /// A description of the exception.
29
+ public let description : String
30
+
31
+ /// The stack trace of the exception.
32
+ public let stack : String ?
33
+
28
34
/// Initializes a new JSException instance with a value thrown from JavaScript.
29
35
///
30
- /// Only available within the package.
36
+ /// Only available within the package. This must be called on the thread where the exception object created.
31
37
package init ( _ thrownValue: JSValue ) {
32
38
self . _thrownValue = thrownValue
39
+ // Capture the stringified representation on the object owner thread
40
+ // to bring useful info to the catching thread even if they are different threads.
41
+ if let errorObject = thrownValue. object, let stack = errorObject. stack. string {
42
+ self . description = " JSException( \( stack) ) "
43
+ self . stack = stack
44
+ } else {
45
+ self . description = " JSException( \( thrownValue) ) "
46
+ self . stack = nil
47
+ }
33
48
}
34
49
}
0 commit comments