about summary refs log tree commit diff stats
path: root/src/js/exception.nim
blob: 44b178bae0d892834d2bc18789356a2dcd98de9e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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

jsDestructor(DOMException)

type
  JSResult*[T] = Result[T, JSError]

  DOMResult*[T] = Result[T, DOMException]

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))