forked from dexie/Dexie.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.js
50 lines (44 loc) · 1.88 KB
/
debug.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
// By default, debug will be true only if platform is a web platform and its page is served from localhost.
// When debug = true, error's stacks will contain asyncronic long stacks.
export var debug = typeof location !== 'undefined' &&
// By default, use debug mode if served from localhost.
/^(http|https):\/\/(localhost|127\.0\.0\.1)/.test(location.href);
export function setDebug(value, filter) {
debug = value;
libraryFilter = filter;
}
export var libraryFilter = () => true;
export const NEEDS_THROW_FOR_STACK = !new Error("").stack;
export function getErrorWithStack() {
"use strict";
if (NEEDS_THROW_FOR_STACK) try {
// Doing something naughty in strict mode here to trigger a specific error
// that can be explicitely ignored in debugger's exception settings.
// If we'd just throw new Error() here, IE's debugger's exception settings
// will just consider it as "exception thrown by javascript code" which is
// something you wouldn't want it to ignore.
getErrorWithStack.arguments;
throw new Error(); // Fallback if above line don't throw.
} catch(e) {
return e;
}
return new Error();
}
export function prettyStack(exception, numIgnoredFrames) {
var stack = exception.stack;
if (!stack) return "";
numIgnoredFrames = (numIgnoredFrames || 0);
if (stack.indexOf(exception.name) === 0)
numIgnoredFrames += (exception.name + exception.message).split('\n').length;
return stack.split('\n')
.slice(numIgnoredFrames)
.filter(libraryFilter)
.map(frame => "\n" + frame)
.join('');
}
export function deprecated (what, fn) {
return function () {
console.warn(`${what} is deprecated. See https://github.com/dfahlander/Dexie.js/wiki/Deprecations. ${prettyStack(getErrorWithStack(), 1)}`);
return fn.apply(this, arguments);
}
}