Skip to content

Add public init(message:) to JSException #385

New issue

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

By clicking “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? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 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
8 changes: 8 additions & 0 deletions Sources/JavaScriptKit/JSException.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ public struct JSException: Error, Equatable, CustomStringConvertible {
self.stack = nil
}
}

/// Initializes a new JavaScript `Error` instance with a message and prepare it to be thrown.
///
/// - Parameters:
/// - message: The message to throw.
public init(message: String) {
self.init(JSError(message: message).jsValue)
}
}
64 changes: 64 additions & 0 deletions Tests/JavaScriptKitTests/JSExceptionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import XCTest
import JavaScriptKit

class JSExceptionTests: XCTestCase {
private func eval(_ code: String) -> JSValue {
return JSObject.global.eval!(code)
}

func testThrowingMethodCalls() {
let context = eval(
"""
(() => ({
func1: () => { throw new Error(); },
func2: () => { throw 'String Error'; },
func3: () => { throw 3.0; },
}))()
"""
).object!

// MARK: Throwing method calls
XCTAssertThrowsError(try context.throwing.func1!()) { error in
XCTAssertTrue(error is JSException)
let errorObject = JSError(from: (error as! JSException).thrownValue)
XCTAssertNotNil(errorObject)
}

XCTAssertThrowsError(try context.throwing.func2!()) { error in
XCTAssertTrue(error is JSException)
let thrownValue = (error as! JSException).thrownValue
XCTAssertEqual(thrownValue.string, "String Error")
}

XCTAssertThrowsError(try context.throwing.func3!()) { error in
XCTAssertTrue(error is JSException)
let thrownValue = (error as! JSException).thrownValue
XCTAssertEqual(thrownValue.number, 3.0)
}
}

func testThrowingUnboundFunctionCalls() {
let jsThrowError = eval("() => { throw new Error(); }")
XCTAssertThrowsError(try jsThrowError.function!.throws()) { error in
XCTAssertTrue(error is JSException)
let errorObject = JSError(from: (error as! JSException).thrownValue)
XCTAssertNotNil(errorObject)
}
}

func testThrowingConstructorCalls() {
let Animal = JSObject.global.Animal.function!
XCTAssertNoThrow(try Animal.throws.new("Tama", 3, true))
XCTAssertThrowsError(try Animal.throws.new("Tama", -3, true)) { error in
XCTAssertTrue(error is JSException)
let errorObject = JSError(from: (error as! JSException).thrownValue)
XCTAssertNotNil(errorObject)
}
}

func testInitWithMessage() {
let message = "THIS IS AN ERROR MESSAGE"
let exception = JSException(message: message)
XCTAssertTrue(exception.description.contains(message))
}
}
58 changes: 0 additions & 58 deletions Tests/JavaScriptKitTests/JavaScriptKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -423,64 +423,6 @@ class JavaScriptKitTests: XCTestCase {
globalObject1.prop_1.nested_prop = originalProp1
}

func testException() {
// ```js
// global.globalObject1 = {
// ...
// prop_9: {
// func1: function () {
// throw new Error();
// },
// func2: function () {
// throw "String Error";
// },
// func3: function () {
// throw 3.0
// },
// },
// ...
// }
// ```
//
let globalObject1 = JSObject.global.globalObject1
let prop_9: JSValue = globalObject1.prop_9

// MARK: Throwing method calls
XCTAssertThrowsError(try prop_9.object!.throwing.func1!()) { error in
XCTAssertTrue(error is JSException)
let errorObject = JSError(from: (error as! JSException).thrownValue)
XCTAssertNotNil(errorObject)
}

XCTAssertThrowsError(try prop_9.object!.throwing.func2!()) { error in
XCTAssertTrue(error is JSException)
let thrownValue = (error as! JSException).thrownValue
XCTAssertEqual(thrownValue.string, "String Error")
}

XCTAssertThrowsError(try prop_9.object!.throwing.func3!()) { error in
XCTAssertTrue(error is JSException)
let thrownValue = (error as! JSException).thrownValue
XCTAssertEqual(thrownValue.number, 3.0)
}

// MARK: Simple function calls
XCTAssertThrowsError(try prop_9.func1.function!.throws()) { error in
XCTAssertTrue(error is JSException)
let errorObject = JSError(from: (error as! JSException).thrownValue)
XCTAssertNotNil(errorObject)
}

// MARK: Throwing constructor call
let Animal = JSObject.global.Animal.function!
XCTAssertNoThrow(try Animal.throws.new("Tama", 3, true))
XCTAssertThrowsError(try Animal.throws.new("Tama", -3, true)) { error in
XCTAssertTrue(error is JSException)
let errorObject = JSError(from: (error as! JSException).thrownValue)
XCTAssertNotNil(errorObject)
}
}

func testSymbols() {
let symbol1 = JSSymbol("abc")
let symbol2 = JSSymbol("abc")
Expand Down
11 changes: 0 additions & 11 deletions Tests/prelude.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,6 @@ function setupTestGlobals(global) {
},
prop_7: 3.14,
prop_8: [0, , 2, 3, , , 6],
prop_9: {
func1: function () {
throw new Error();
},
func2: function () {
throw "String Error";
},
func3: function () {
throw 3.0;
},
},
eval_closure: function (fn) {
return fn(arguments[1]);
},
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy