Skip to content

Commit eae814f

Browse files
committed
pad_utils: New warnWithStack() function
1 parent 1bbe0d9 commit eae814f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/static/js/pad_utils.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,25 @@ const urlRegex = (() => {
8989
})();
9090

9191
const padutils = {
92+
/**
93+
* Prints a warning message followed by a stack trace (to make it easier to figure out what code
94+
* is using the deprecated function).
95+
*
96+
* Most browsers include UI widget to examine the stack at the time of the warning, but this
97+
* includes the stack in the log message for a couple of reasons:
98+
* - This makes it possible to see the stack if the code runs in Node.js.
99+
* - Users are more likely to paste the stack in bug reports they might file.
100+
*
101+
* @param {...*} args - Passed to `console.warn`, with a stack trace appended.
102+
*/
103+
warnWithStack: (...args) => {
104+
const err = new Error();
105+
if (Error.captureStackTrace) Error.captureStackTrace(err, padutils.warnWithStack);
106+
err.name = '';
107+
if (err.stack) args.push(err.stack);
108+
console.warn(...args);
109+
},
110+
92111
escapeHtml: (x) => Security.escapeHTML(String(x)),
93112
uniqueId: () => {
94113
const pad = require('./pad').pad; // Sidestep circular dependency

0 commit comments

Comments
 (0)