-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
err.ts
56 lines (49 loc) · 1.3 KB
/
err.ts
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
function setPrototypeOf(obj: any, proto: any) {
// eslint-disable-line @typescript-eslint/no-explicit-any
if (Object.setPrototypeOf) {
Object.setPrototypeOf(obj, proto);
} else {
obj.__proto__ = proto;
}
}
// This is pretty much the only way to get nice, extended Errors
// without using ES6
/**
* This returns a new Error with a custom prototype. Note that it's _not_ a constructor
*
* @param message Error message
*
* **Example**
*
* ```js
* throw EtaErr("template not found")
* ```
*/
export default function EtaErr(message: string): Error {
const err = new Error(message);
setPrototypeOf(err, EtaErr.prototype);
return err as Error;
}
EtaErr.prototype = Object.create(Error.prototype, {
name: { value: "Eta Error", enumerable: false },
});
/**
* Throws an EtaErr with a nicely formatted error and message showing where in the template the error occurred.
*/
export function ParseErr(message: string, str: string, indx: number): void {
const whitespace = str.slice(0, indx).split(/\n/);
const lineNo = whitespace.length;
const colNo = whitespace[lineNo - 1].length + 1;
message += " at line " +
lineNo +
" col " +
colNo +
":\n\n" +
" " +
str.split(/\n/)[lineNo - 1] +
"\n" +
" " +
Array(colNo).join(" ") +
"^";
throw EtaErr(message);
}