forked from xiyuyizhi/vod-fp.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCusError.js
73 lines (61 loc) · 1.25 KB
/
CusError.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
import Base from './Base';
Error.prototype.is = () => {};
Error.prototype.getOrElse = () => {};
export default class CusError {
constructor(value) {
if (value instanceof Error) {
this._value = {
fatal: true,
type: value.constructor.name,
message: value.message + ',' + value
.stack
.slice(0, 120),
originType: value.constructor.name // 非自定义错误
};
} else {
this._value = value;
}
}
static of(value) {
return new CusError(value);
}
getOrElse(e) {
if (!this._value.originType)
return this;
return this._merge(e);
}
merge(another) {
return this._merge(another);
}
_merge(another) {
return CusError.of({
...this._value,
...another._value
});
}
fatal(flag) {
if (flag) {
this._value.fatal = flag;
} else {
return this._value.fatal;
}
}
value() {
return this._value;
}
join() {
return this.value();
}
detail() {
return this._value.detail;
}
type() {
return this._value.type;
}
is(another) {
return (this._value.type === another.type && this._value.detail === another.detail);
}
isType(another) {
return this._value.type === another.type;
}
}