|
1 |
| -public final class JSError { |
2 |
| - private let ref: JSObject |
| 1 | +/** A wrapper around the [JavaScript Error |
| 2 | +class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) that |
| 3 | +exposes its properties in a type-safe way. |
| 4 | +*/ |
| 5 | +public final class JSError: Error { |
| 6 | + /// The underlying JavaScript `Error` object. |
| 7 | + public let jsObject: JSObject |
| 8 | + |
| 9 | + /// The constructor function used to create new `Error` objects. |
3 | 10 | private static let constructor = JSObject.global.Error.function!
|
4 | 11 |
|
| 12 | + /// Creates a new instance of the JavaScript `Error` class with a given message. |
5 | 13 | public init(message: String) {
|
6 |
| - ref = Self.constructor.new([message]) |
| 14 | + jsObject = Self.constructor.new([message]) |
7 | 15 | }
|
8 | 16 |
|
| 17 | + /// The error message of the underlying `Error` object. |
9 | 18 | public var message: String {
|
10 |
| - ref.message.string! |
| 19 | + jsObject.message.string! |
11 | 20 | }
|
12 | 21 |
|
| 22 | + /// The name (usually corresponds to the name of the underlying class) of a given error. |
13 | 23 | public var name: String {
|
14 |
| - ref.name.string! |
| 24 | + jsObject.name.string! |
| 25 | + } |
| 26 | + |
| 27 | + /// The JavaScript call trace that led to the creation of this error object. |
| 28 | + public var stack: String? { |
| 29 | + jsObject.stack.string |
15 | 30 | }
|
16 | 31 | }
|
17 | 32 |
|
18 | 33 | extension JSError: CustomStringConvertible {
|
19 |
| - public var description: String { ref.description } |
| 34 | + /// The textual representation of this error. |
| 35 | + public var description: String { jsObject.description } |
20 | 36 | }
|
0 commit comments