Skip to content
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

Add JSError with tests, add JSObject.description #47

Merged
merged 2 commits into from
Sep 14, 2020
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! }
}