name:
src/Errors.ts
-rw-r--r--
2878
1let NULL_ERROR = "#NULL!";
2let DIV_ZERO_ERROR = "#DIV/0!";
3let VALUE_ERROR = "#VALUE!";
4let REF_ERROR = "#REF!";
5let NAME_ERROR = "#NAME!";
6let NUM_ERROR = "#NUM!";
7let NA_ERROR = "#N/A";
8let PARSE_ERROR = "#ERROR";
9
10/**
11 * Execution or parsing produced a null value, or intersection of ranges produced zero cells.
12 */
13class NullError extends Error {
14 constructor(message: string) {
15 super(message);
16 this.name = NULL_ERROR;
17 }
18}
19
20/**
21 * Attempt to divide by zero, including division by an empty cell.
22 */
23class DivZeroError extends Error {
24 constructor(message: string) {
25 super(message);
26 this.name = DIV_ZERO_ERROR;
27 }
28}
29
30/**
31 * Parameter is wrong type, or value is incompatible, or cannot be parsed, converted, or manipulated.
32 */
33class ValueError extends Error {
34 constructor(message: string) {
35 super(message);
36 this.name = VALUE_ERROR;
37 }
38}
39
40/**
41 * Reference to invalid cell, range, or empty range.
42 */
43class RefError extends Error {
44 constructor(message: string) {
45 super(message);
46 this.name = REF_ERROR;
47 }
48}
49
50/**
51 * Unrecognized/deleted name.
52 */
53class NameError extends Error {
54 constructor(message: string) {
55 super(message);
56 this.name = NAME_ERROR;
57 }
58}
59
60/**
61 * Failed to meet domain constraints (e.g., input number too high or too low).
62 */
63class NumError extends Error {
64 constructor(message: string) {
65 super(message);
66 this.name = NUM_ERROR;
67 }
68}
69
70/**
71 * Lookup functions which failed and NA() return this value.
72 */
73class NAError extends Error {
74 constructor(message: string) {
75 super(message);
76 this.name = NA_ERROR;
77 }
78}
79
80/**
81 * Input could not be parsed.
82 */
83class ParseError extends Error {
84 constructor(message: string) {
85 super(message);
86 this.name = PARSE_ERROR;
87 }
88}
89
90/**
91 * Constructs an error by error name.
92 * @param {string} name - Name of error. If not one of DIV_ZERO_ERROR, NULL_ERROR, VALUE_ERROR, REF_ERROR, NAME_ERROR,
93 * NUM_ERROR,NA_ERROR, or PARSE_ERROR, will default to ParseError.
94 * @param {string} msg - Message for error, will default to empty string.
95 * @returns {Error}
96 */
97function constructErrorByName(name : string, msg? : string) : Error {
98 msg = msg || "";
99 switch (name) {
100 case DIV_ZERO_ERROR:
101 return new DivZeroError(msg);
102 case NULL_ERROR:
103 return new NullError(msg);
104 case VALUE_ERROR:
105 return new ValueError(msg);
106 case REF_ERROR:
107 return new RefError(msg);
108 case NAME_ERROR:
109 return new NameError(msg);
110 case NA_ERROR:
111 return new NAError(msg);
112 case NUM_ERROR:
113 return new NumError(msg);
114 default:
115 return new ParseError(msg);
116 }
117}
118
119export {
120 DIV_ZERO_ERROR,
121 NULL_ERROR,
122 VALUE_ERROR,
123 REF_ERROR,
124 NAME_ERROR,
125 NUM_ERROR,
126 NA_ERROR,
127 PARSE_ERROR,
128 DivZeroError,
129 NullError,
130 ValueError,
131 RefError,
132 NameError,
133 NumError,
134 NAError,
135 ParseError,
136 constructErrorByName
137}