/
errors.js
174 lines (157 loc) · 5.39 KB
/
errors.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { derive, setProp } from '../functions/utils';
import { getErrorWithStack, prettyStack } from '../helpers/debug';
var dexieErrorNames = [
'Modify',
'Bulk',
'OpenFailed',
'VersionChange',
'Schema',
'Upgrade',
'InvalidTable',
'MissingAPI',
'NoSuchDatabase',
'InvalidArgument',
'SubTransaction',
'Unsupported',
'Internal',
'DatabaseClosed',
'PrematureCommit',
'ForeignAwait'
];
var idbDomErrorNames = [
'Unknown',
'Constraint',
'Data',
'TransactionInactive',
'ReadOnly',
'Version',
'NotFound',
'InvalidState',
'InvalidAccess',
'Abort',
'Timeout',
'QuotaExceeded',
'Syntax',
'DataClone'
];
var errorList = dexieErrorNames.concat(idbDomErrorNames);
var defaultTexts = {
VersionChanged: "Database version changed by other database connection",
DatabaseClosed: "Database has been closed",
Abort: "Transaction aborted",
TransactionInactive: "Transaction has already completed or failed",
MissingAPI: "IndexedDB API missing. Please visit https://tinyurl.com/y2uuvskb"
};
//
// DexieError - base class of all out exceptions.
//
export function DexieError (name, msg) {
// Reason we don't use ES6 classes is because:
// 1. It bloats transpiled code and increases size of minified code.
// 2. It doesn't give us much in this case.
// 3. It would require sub classes to call super(), which
// is not needed when deriving from Error.
this._e = getErrorWithStack();
this.name = name;
this.message = msg;
}
derive(DexieError).from(Error).extend({
stack: {
get: function() {
return this._stack ||
(this._stack = this.name + ": " + this.message + prettyStack(this._e, 2));
}
},
toString: function(){ return this.name + ": " + this.message; }
});
function getMultiErrorMessage (msg, failures) {
return msg + ". Errors: " + Object.keys(failures)
.map(key=>failures[key].toString())
.filter((v,i,s)=>s.indexOf(v) === i) // Only unique error strings
.join('\n');
}
//
// ModifyError - thrown in Collection.modify()
// Specific constructor because it contains members failures and failedKeys.
//
export function ModifyError (msg, failures, successCount, failedKeys) {
this._e = getErrorWithStack();
this.failures = failures;
this.failedKeys = failedKeys;
this.successCount = successCount;
this.message = getMultiErrorMessage(msg, failures);
}
derive(ModifyError).from(DexieError);
export function BulkError (msg, failures) {
this._e = getErrorWithStack();
this.name = "BulkError";
this.failures = Object.keys(failures).map(pos => failures[pos]);
this.failuresByPos = failures;
this.message = getMultiErrorMessage(msg, failures);
}
derive(BulkError).from(DexieError);
//
//
// Dynamically generate error names and exception classes based
// on the names in errorList.
//
//
// Map of {ErrorName -> ErrorName + "Error"}
export var errnames = errorList.reduce((obj,name)=>(obj[name]=name+"Error",obj),{});
// Need an alias for DexieError because we're gonna create subclasses with the same name.
const BaseException = DexieError;
// Map of {ErrorName -> exception constructor}
export var exceptions = errorList.reduce((obj,name)=>{
// Let the name be "DexieError" because this name may
// be shown in call stack and when debugging. DexieError is
// the most true name because it derives from DexieError,
// and we cannot change Function.name programatically without
// dynamically create a Function object, which would be considered
// 'eval-evil'.
var fullName = name + "Error";
function DexieError (msgOrInner, inner){
this._e = getErrorWithStack();
this.name = fullName;
if (!msgOrInner) {
this.message = defaultTexts[name] || fullName;
this.inner = null;
} else if (typeof msgOrInner === 'string') {
this.message = `${msgOrInner}${!inner ? '' : '\n ' + inner}`;
this.inner = inner || null;
} else if (typeof msgOrInner === 'object') {
this.message = `${msgOrInner.name} ${msgOrInner.message}`;
this.inner = msgOrInner;
}
}
derive(DexieError).from(BaseException);
obj[name]=DexieError;
return obj;
},{});
// Use ECMASCRIPT standard exceptions where applicable:
exceptions.Syntax = SyntaxError;
exceptions.Type = TypeError;
exceptions.Range = RangeError;
export var exceptionMap = idbDomErrorNames.reduce((obj, name)=>{
obj[name + "Error"] = exceptions[name];
return obj;
}, {});
export function mapError (domError, message) {
if (!domError || domError instanceof DexieError || domError instanceof TypeError || domError instanceof SyntaxError || !domError.name || !exceptionMap[domError.name])
return domError;
var rv = new exceptionMap[domError.name](message || domError.message, domError);
if ("stack" in domError) {
// Derive stack from inner exception if it has a stack
setProp(rv, "stack", {get: function(){
return this.inner.stack;
}});
}
return rv;
}
export var fullNameExceptions = errorList.reduce((obj, name)=>{
if (["Syntax","Type","Range"].indexOf(name) === -1)
obj[name + "Error"] = exceptions[name];
return obj;
}, {});
fullNameExceptions.ModifyError = ModifyError;
fullNameExceptions.DexieError = DexieError;
fullNameExceptions.BulkError = BulkError;