Skip to content
This repository was archived by the owner on Oct 21, 2021. It is now read-only.

Commit 7c4a2b3

Browse files
authored
Merge pull request #2 from github/frames-to-pop
Improve error thrown
2 parents 37edb3e + 85dcd07 commit 7c4a2b3

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@
22

33
type Queryable = Document | DocumentFragment | Element
44

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+
515
export function closest<T: Element>(element: Element, selectors: string, type: Class<T>): T {
616
const klass = type || HTMLElement
717
const el = element.closest(selectors)
818
if (el instanceof klass) {
919
return el
1020
}
11-
throw new Error(`Element not found: <${klass.name}> ${selectors}`)
21+
throw new QueryError(`Element not found: <${klass.name}> ${selectors}`)
1222
}
1323

1424
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
1727
if (el instanceof klass) {
1828
return el
1929
}
20-
throw new Error(`Element not found: <${klass.name}> ${selectors}`)
30+
throw new QueryError(`Element not found: <${klass.name}> ${selectors}`)
2131
}
2232

2333
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
3747
if (el instanceof klass) {
3848
return el
3949
}
40-
throw new Error(`Element not found by name: <${klass.name}> ${itemName}`)
50+
throw new QueryError(`Element not found by name: <${klass.name}> ${itemName}`)
4151
}
4252

4353
export function getAttribute(element: Element, attributeName: string): string {
4454
const attribute = element.getAttribute(attributeName)
4555
if (attribute != null) {
4656
return attribute
4757
}
48-
throw new Error(`Attribute not found on element: ${attributeName}`)
58+
throw new QueryError(`Attribute not found on element: ${attributeName}`)
4959
}

0 commit comments

Comments
 (0)