From 851a5c4f53a2ddf341e71aa9ed2ea90e9dfafef1 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Mon, 14 Sep 2020 09:45:18 +0100 Subject: [PATCH 1/3] Add JSError.stack, add Error conformance --- Sources/JavaScriptKit/BasicObjects/JSError.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sources/JavaScriptKit/BasicObjects/JSError.swift b/Sources/JavaScriptKit/BasicObjects/JSError.swift index 1ed8c0ffd..9e321a3cb 100644 --- a/Sources/JavaScriptKit/BasicObjects/JSError.swift +++ b/Sources/JavaScriptKit/BasicObjects/JSError.swift @@ -1,4 +1,4 @@ -public final class JSError { +public final class JSError: Error { private let ref: JSObject private static let constructor = JSObject.global.Error.function! @@ -13,6 +13,10 @@ public final class JSError { public var name: String { ref.name.string! } + + public var stack: String { + ref.stack.string! + } } extension JSError: CustomStringConvertible { From 85ab3934e972c7294cfb3453d63b09935a348f7c Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Mon, 14 Sep 2020 15:26:13 +0100 Subject: [PATCH 2/3] Check that stack is non-empty in JSError test --- IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift b/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift index f62133c71..ed1464595 100644 --- a/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift +++ b/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift @@ -401,4 +401,5 @@ try test("Error") { try expectEqual(error.name, "Error") try expectEqual(error.message, message) try expectEqual(error.description, "Error: test error") + try expectEqual(error.stack.isEmpty, false) } From 2c99d8264f9237d2a8215f76bb3cbde1b26a4931 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Tue, 15 Sep 2020 09:38:53 +0100 Subject: [PATCH 3/3] Make `JSError.stack` optional, add doc comments --- .../Sources/PrimaryTests/main.swift | 2 +- .../JavaScriptKit/BasicObjects/JSError.swift | 26 ++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift b/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift index ed1464595..e9055307d 100644 --- a/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift +++ b/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift @@ -401,5 +401,5 @@ try test("Error") { try expectEqual(error.name, "Error") try expectEqual(error.message, message) try expectEqual(error.description, "Error: test error") - try expectEqual(error.stack.isEmpty, false) + try expectEqual(error.stack?.isEmpty, false) } diff --git a/Sources/JavaScriptKit/BasicObjects/JSError.swift b/Sources/JavaScriptKit/BasicObjects/JSError.swift index 9e321a3cb..be1760772 100644 --- a/Sources/JavaScriptKit/BasicObjects/JSError.swift +++ b/Sources/JavaScriptKit/BasicObjects/JSError.swift @@ -1,24 +1,36 @@ +/** A wrapper around the [JavaScript Error +class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) that +exposes its properties in a type-safe way. +*/ public final class JSError: Error { - private let ref: JSObject + /// The underlying JavaScript `Error` object. + public let jsObject: JSObject + + /// The constructor function used to create new `Error` objects. private static let constructor = JSObject.global.Error.function! + /// Creates a new instance of the JavaScript `Error` class with a given message. public init(message: String) { - ref = Self.constructor.new([message]) + jsObject = Self.constructor.new([message]) } + /// The error message of the underlying `Error` object. public var message: String { - ref.message.string! + jsObject.message.string! } + /// The name (usually corresponds to the name of the underlying class) of a given error. public var name: String { - ref.name.string! + jsObject.name.string! } - public var stack: String { - ref.stack.string! + /// The JavaScript call trace that led to the creation of this error object. + public var stack: String? { + jsObject.stack.string } } extension JSError: CustomStringConvertible { - public var description: String { ref.description } + /// The textual representation of this error. + public var description: String { jsObject.description } }