Skip to content

Capture error message at JSException construction #349

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? No Sign in to your account

Merged
merged 3 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions Sources/JavaScriptKit/JSException.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/// let jsErrorValue = error.thrownValue
/// }
/// ```
public struct JSException: Error, Equatable {
public struct JSException: Error, Equatable, CustomStringConvertible {
/// The value thrown from JavaScript.
/// This can be any JavaScript value (error object, string, number, etc.).
public var thrownValue: JSValue {
Expand All @@ -25,10 +25,25 @@ public struct JSException: Error, Equatable {
/// from `Error` protocol.
private nonisolated(unsafe) let _thrownValue: JSValue

/// A description of the exception.
public let description: String

/// The stack trace of the exception.
public let stack: String?

/// Initializes a new JSException instance with a value thrown from JavaScript.
///
/// Only available within the package.
/// Only available within the package. This must be called on the thread where the exception object created.
package init(_ thrownValue: JSValue) {
self._thrownValue = thrownValue
// Capture the stringified representation on the object owner thread
// to bring useful info to the catching thread even if they are different threads.
if let errorObject = thrownValue.object, let stack = errorObject.stack.string {
self.description = "JSException(\(stack))"
self.stack = stack
} else {
self.description = "JSException(\(thrownValue))"
self.stack = nil
}
}
}
14 changes: 14 additions & 0 deletions Tests/JavaScriptEventLoopTests/WebWorkerTaskExecutorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,20 @@ final class WebWorkerTaskExecutorTests: XCTestCase {
XCTAssertEqual(object["test"].string!, "Hello, World!")
}

func testThrowJSExceptionAcrossThreads() async throws {
let executor = try await WebWorkerTaskExecutor(numberOfThreads: 1)
let task = Task(executorPreference: executor) {
_ = try JSObject.global.eval.function!.throws("throw new Error()")
}
do {
try await task.value
XCTFail()
} catch let error as JSException {
// Stringify JSException coming from worker should be allowed
_ = String(describing: error)
}
}

// func testDeinitJSObjectOnDifferentThread() async throws {
// let executor = try await WebWorkerTaskExecutor(numberOfThreads: 1)
//
Expand Down