-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.js
130 lines (111 loc) · 2.67 KB
/
error.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var error = function () {
return "error! ";
}
var unbound_symbol = function () {
return " is an unbound symbol";
}
var number_error = function () {
return " is not a number";
}
var num_args_error = function () {
return "incorrect number of arguments";
}
var unknown_error = function () {
return "input error";
}
var divide_zero = function () {
return "Divide by zero";
}
var list_argument = function () {
return "Argument should be a list. ";
}
var has_message = function (err) {
return (err.message !== "");
}
var handle_errors = function (err) {
var error_message = "";
error_message += error();
if (has_message(err)) {
error_message += err.message;
}
switch (err.name) {
case 'InputError':
error_message += unknown_error();
break;
case 'NumArgsError':
error_message += num_args_error();
break;
case 'DivideZeroError':
error_message += divide_zero();
break;
case 'ListArgError':
error_message += list_argument();
break;
case 'NumberError':
error_message += number_error();
break;
case 'UnboundSymbolError':
error_message += unbound_symbol();
break;
}
return error_message;
}
class NumberError extends Error {
constructor(message = "") {
super(message);
this.name = 'NumberError';
this.message = message;
}
}
class InputError extends Error {
constructor(message = "") {
super(message);
this.name = 'InputError';
this.message = message;
}
}
class NumArgsError extends Error {
constructor(message = "") {
super(message);
this.name = 'NumArgsError';
this.message = message;
}
}
class DivideZeroError extends Error {
constructor(message = "") {
super(message);
this.name = 'DivideZeroError';
this.message = message;
}
}
class ListArgError extends Error {
constructor(message = "") {
super(message);
this.name = 'ListArgError';
this.message = message;
}
}
class UnboundSymbolError extends Error {
constructor(message = "") {
super(message);
this.name = 'UnboundSymbolError';
this.message = message;
}
}
class UnboundFunctionError extends Error {
constructor(message = "") {
super(message);
this.name = 'UnboundFunctionError';
this.message = message;
}
}
module.exports = {
NumberError,
InputError,
NumArgsError,
DivideZeroError,
ListArgError,
UnboundSymbolError,
UnboundFunctionError,
handle_errors
}