-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy patherror-collectors.js
43 lines (36 loc) · 1007 Bytes
/
error-collectors.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
import { Model, ObjectModel } from "objectmodel";
function log(msg, errors) {
console.log(msg);
errors.forEach(error => {
console.log(error);
});
document.write(
[msg, ...errors.map(e => e.message)].join("<br>→ ") + "<br><br>"
);
}
Model.prototype.errorCollector = function(errors) {
log("Global error collector caught these errors:", errors);
};
const Student = ObjectModel({
name: String,
course: ["math", "english", "history"],
grade: Number
}).assert(
student => student.grade >= 60,
"should at least get 60 to validate semester"
);
new Student({ name: "Joanna", course: "sleep", grade: 0 });
Student.errorCollector = function(errors) {
log("Student model error collector caught these errors:", errors);
};
new Student({ name: "Joanna", course: "math", grade: 50 });
Student.test(
{
name: "Joanna",
course: "cheating",
grade: 90
},
function(errors) {
log("This specific error collector caught these errors:", errors);
}
);