2
2
3
3
type Queryable = Document | DocumentFragment | Element
4
4
5
+ class QueryError extends Error {
6
+ framesToPop : number
7
+
8
+ constructor ( message ) {
9
+ super ( message )
10
+ this . name = 'QueryError'
11
+ this . framesToPop = 1
12
+ }
13
+ }
14
+
5
15
export function closest < T : Element > ( element : Element , selectors : string , type : Class < T > ) : T {
6
16
const klass = type || HTMLElement
7
17
const el = element . closest ( selectors )
8
18
if ( el instanceof klass ) {
9
19
return el
10
20
}
11
- throw new Error ( `Element not found: <${ klass . name } > ${ selectors } ` )
21
+ throw new QueryError ( `Element not found: <${ klass . name } > ${ selectors } ` )
12
22
}
13
23
14
24
export function query < T : Element > ( context : Queryable , selectors : string , type : Class < T > ) : T {
@@ -17,7 +27,7 @@ export function query<T: Element>(context: Queryable, selectors: string, type: C
17
27
if ( el instanceof klass ) {
18
28
return el
19
29
}
20
- throw new Error ( `Element not found: <${ klass . name } > ${ selectors } ` )
30
+ throw new QueryError ( `Element not found: <${ klass . name } > ${ selectors } ` )
21
31
}
22
32
23
33
export function querySelectorAll < T : Element > ( context : Queryable , selectors : string , type : Class < T > ) : Array < T > {
@@ -37,13 +47,13 @@ export function namedItem<T: HTMLElement>(form: HTMLFormElement, itemName: strin
37
47
if ( el instanceof klass ) {
38
48
return el
39
49
}
40
- throw new Error ( `Element not found by name: <${ klass . name } > ${ itemName } ` )
50
+ throw new QueryError ( `Element not found by name: <${ klass . name } > ${ itemName } ` )
41
51
}
42
52
43
53
export function getAttribute ( element : Element , attributeName : string ) : string {
44
54
const attribute = element . getAttribute ( attributeName )
45
55
if ( attribute != null ) {
46
56
return attribute
47
57
}
48
- throw new Error ( `Attribute not found on element: ${ attributeName } ` )
58
+ throw new QueryError ( `Attribute not found on element: ${ attributeName } ` )
49
59
}
0 commit comments