1212/// let jsErrorValue = error.thrownValue
1313/// }
1414/// ```
15- public struct JSException : Error , Equatable {
15+ public struct JSException : Error , Equatable , CustomStringConvertible {
1616 /// The value thrown from JavaScript.
1717 /// This can be any JavaScript value (error object, string, number, etc.).
1818 public var thrownValue : JSValue {
@@ -25,10 +25,25 @@ public struct JSException: Error, Equatable {
2525 /// from `Error` protocol.
2626 private nonisolated ( unsafe) let _thrownValue : JSValue
2727
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+
2834 /// Initializes a new JSException instance with a value thrown from JavaScript.
2935 ///
30- /// Only available within the package.
36+ /// Only available within the package. This must be called on the thread where the exception object created.
3137 package init ( _ thrownValue: JSValue ) {
3238 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+ }
3348 }
3449}
0 commit comments