1
|
var inherits = require('inherits');
|
2
|
|
3
|
function Reporter(options) {
|
4
|
this._reporterState = {
|
5
|
obj: null,
|
6
|
path: [],
|
7
|
options: options || {},
|
8
|
errors: []
|
9
|
};
|
10
|
}
|
11
|
exports.Reporter = Reporter;
|
12
|
|
13
|
Reporter.prototype.isError = function isError(obj) {
|
14
|
return obj instanceof ReporterError;
|
15
|
};
|
16
|
|
17
|
Reporter.prototype.save = function save() {
|
18
|
var state = this._reporterState;
|
19
|
|
20
|
return { obj: state.obj, pathLen: state.path.length };
|
21
|
};
|
22
|
|
23
|
Reporter.prototype.restore = function restore(data) {
|
24
|
var state = this._reporterState;
|
25
|
|
26
|
state.obj = data.obj;
|
27
|
state.path = state.path.slice(0, data.pathLen);
|
28
|
};
|
29
|
|
30
|
Reporter.prototype.enterKey = function enterKey(key) {
|
31
|
return this._reporterState.path.push(key);
|
32
|
};
|
33
|
|
34
|
Reporter.prototype.exitKey = function exitKey(index) {
|
35
|
var state = this._reporterState;
|
36
|
|
37
|
state.path = state.path.slice(0, index - 1);
|
38
|
};
|
39
|
|
40
|
Reporter.prototype.leaveKey = function leaveKey(index, key, value) {
|
41
|
var state = this._reporterState;
|
42
|
|
43
|
this.exitKey(index);
|
44
|
if (state.obj !== null)
|
45
|
state.obj[key] = value;
|
46
|
};
|
47
|
|
48
|
Reporter.prototype.path = function path() {
|
49
|
return this._reporterState.path.join('/');
|
50
|
};
|
51
|
|
52
|
Reporter.prototype.enterObject = function enterObject() {
|
53
|
var state = this._reporterState;
|
54
|
|
55
|
var prev = state.obj;
|
56
|
state.obj = {};
|
57
|
return prev;
|
58
|
};
|
59
|
|
60
|
Reporter.prototype.leaveObject = function leaveObject(prev) {
|
61
|
var state = this._reporterState;
|
62
|
|
63
|
var now = state.obj;
|
64
|
state.obj = prev;
|
65
|
return now;
|
66
|
};
|
67
|
|
68
|
Reporter.prototype.error = function error(msg) {
|
69
|
var err;
|
70
|
var state = this._reporterState;
|
71
|
|
72
|
var inherited = msg instanceof ReporterError;
|
73
|
if (inherited) {
|
74
|
err = msg;
|
75
|
} else {
|
76
|
err = new ReporterError(state.path.map(function(elem) {
|
77
|
return '[' + JSON.stringify(elem) + ']';
|
78
|
}).join(''), msg.message || msg, msg.stack);
|
79
|
}
|
80
|
|
81
|
if (!state.options.partial)
|
82
|
throw err;
|
83
|
|
84
|
if (!inherited)
|
85
|
state.errors.push(err);
|
86
|
|
87
|
return err;
|
88
|
};
|
89
|
|
90
|
Reporter.prototype.wrapResult = function wrapResult(result) {
|
91
|
var state = this._reporterState;
|
92
|
if (!state.options.partial)
|
93
|
return result;
|
94
|
|
95
|
return {
|
96
|
result: this.isError(result) ? null : result,
|
97
|
errors: state.errors
|
98
|
};
|
99
|
};
|
100
|
|
101
|
function ReporterError(path, msg) {
|
102
|
this.path = path;
|
103
|
this.rethrow(msg);
|
104
|
};
|
105
|
inherits(ReporterError, Error);
|
106
|
|
107
|
ReporterError.prototype.rethrow = function rethrow(msg) {
|
108
|
this.message = msg + ' at: ' + (this.path || '(shallow)');
|
109
|
if (Error.captureStackTrace)
|
110
|
Error.captureStackTrace(this, ReporterError);
|
111
|
|
112
|
if (!this.stack) {
|
113
|
try {
|
114
|
// IE only adds stack when thrown
|
115
|
throw new Error(this.message);
|
116
|
} catch (e) {
|
117
|
this.stack = e.stack;
|
118
|
}
|
119
|
}
|
120
|
return this;
|
121
|
};
|