diff options
author | bptato <nincsnevem662@gmail.com> | 2023-06-19 18:13:10 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-06-19 18:15:09 +0200 |
commit | 17097052794aef56bbc55327d3e6c84ae1c67378 (patch) | |
tree | 13b81e1105c07c69d7a8d1e7367a698f41663a01 /src/js/exception.nim | |
parent | e372bdaa0344b23c91aefa4da44c578fbf8f49e2 (diff) | |
download | chawan-17097052794aef56bbc55327d3e6c84ae1c67378.tar.gz |
Rework JS exception system
Now we use Result for passing exceptions to JS. As a result, we can finally get rid of the .jserr pragma.
Diffstat (limited to 'src/js/exception.nim')
-rw-r--r-- | src/js/exception.nim | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/js/exception.nim b/src/js/exception.nim new file mode 100644 index 00000000..1550d431 --- /dev/null +++ b/src/js/exception.nim @@ -0,0 +1,96 @@ +import tables + +import bindings/quickjs +import js/javascript + +const NamesTable = { + "IndexSizeError": 1u16, + "HierarchyRequestError": 3u16, + "WrongDocumentError": 4u16, + "InvalidCharacterError": 5u16, + "NoModificationAllowedError": 7u16, + "NotFoundError": 8u16, + "NotSupportedError": 9u16, + "InUseAttributeError": 10u16, + "InvalidStateError": 11u16, + "SyntaxError": 12u16, + "InvalidModificationError": 13u16, + "NamespaceError": 14u16, + "InvalidAccessError": 15u16, + "TypeMismatchError": 17u16, + "SecurityError": 18u16, + "NetworkError": 19u16, + "AbortError": 20u16, + "URLMismatchError": 21u16, + "QuotaExceededError": 22u16, + "TimeoutError": 23u16, + "InvalidNodeTypeError": 24u16, + "DataCloneError": 25u16 +}.toTable() + +type DOMException* = ref object of JSError + name* {.jsget.}: string + +proc newDOMException*(message = "", name = "Error"): DOMException {.jsctor.} = + return DOMException( + e: JS_DOM_EXCEPTION, + name: name, + message: message + ) + +func message0(this: DOMException): string {.jsfget: "message".} = + return this.message + +func code(this: DOMException): uint16 {.jsfget.} = + return NamesTable.getOrDefault(this.name, 0u16) + +proc newEvalError*(message: string): JSError = + return JSError( + e: JS_EVAL_ERROR0, + message: message + ) + +proc newRangeError*(message: string): JSError = + return JSError( + e: JS_RANGE_ERROR0, + message: message + ) + +proc newReferenceError*(message: string): JSError = + return JSError( + e: JS_REFERENCE_ERROR0, + message: message + ) + +proc newSyntaxError*(message: string): JSError = + return JSError( + e: JS_SYNTAX_ERROR0, + message: message + ) + +proc newTypeError*(message: string): JSError = + return JSError( + e: JS_TYPE_ERROR0, + message: message + ) + +proc newURIError*(message: string): JSError = + return JSError( + e: JS_URI_ERROR0, + message: message + ) + +proc newInternalError*(message: string): JSError = + return JSError( + e: JS_INTERNAL_ERROR0, + message: message + ) + +proc newAggregateError*(message: string): JSError = + return JSError( + e: JS_AGGREGATE_ERROR0, + message: message + ) + +proc addDOMExceptionModule*(ctx: JSContext) = + ctx.registerType(DOMException, JS_CLASS_ERROR, errid = opt(JS_DOM_EXCEPTION)) |