Skip to content
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 IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,11 @@ try test("TypedArray_Mutation") {
}
try expectEqual(toString(array.jsValue().object!), jsStringify(Array(0..<100)))
}

try test("Error") {
let message = "test error"
let error = JSError(message: message)
try expectEqual(error.name, "Error")
try expectEqual(error.message, message)
try expectEqual(error.description, "Error: test error")
}
20 changes: 20 additions & 0 deletions Sources/JavaScriptKit/BasicObjects/JSError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public final class JSError {
private let ref: JSObject
private static let constructor = JSObject.global.Error.function!

public init(message: String) {
ref = Self.constructor.new([message])
}

public var message: String {
ref.message.string!
}

public var name: String {
ref.name.string!
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it’s not standard, all engines that I know of implement error.stack to provide a stack trace as a string. It might be useful to access it.


extension JSError: CustomStringConvertible {
public var description: String { ref.description }
}
3 changes: 3 additions & 0 deletions Sources/JavaScriptKit/FundamentalObjects/JSObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ public class JSObject: Equatable {
}
}

extension JSObject: CustomStringConvertible {
public var description: String { self.toString!().string! }
}