This is imported from https://github.com/mozilla-services/screenshots/. It has been reviewed as patches landed, but also reviewed by Mossop and kmag. This also includes the patch from bug 1356394 MozReview-Commit-ID: FXIVw7WjxlN
84 lines
1.7 KiB
JavaScript
84 lines
1.7 KiB
JavaScript
/* globals log */
|
|
|
|
"use strict";
|
|
|
|
var global = this;
|
|
|
|
this.catcher = (function () {
|
|
let exports = {};
|
|
|
|
let handler;
|
|
|
|
let queue = [];
|
|
|
|
exports.unhandled = function (error, info) {
|
|
log.error("Unhandled error:", error, info);
|
|
let e = makeError(error, info);
|
|
if (! handler) {
|
|
queue.push(e);
|
|
} else {
|
|
handler(e);
|
|
}
|
|
};
|
|
|
|
/** Turn an exception into an error object */
|
|
function makeError(exc, info) {
|
|
let result;
|
|
if (exc.fromMakeError) {
|
|
result = exc;
|
|
} else {
|
|
result = {
|
|
fromMakeError: true,
|
|
name: exc.name || "ERROR",
|
|
message: String(exc),
|
|
stack: exc.stack
|
|
};
|
|
for (let attr in exc) {
|
|
result[attr] = exc[attr];
|
|
}
|
|
}
|
|
if (info) {
|
|
for (let attr of Object.keys(info)) {
|
|
result[attr] = info[attr];
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/** Wrap the function, and if it raises any exceptions then call unhandled() */
|
|
exports.watchFunction = function watchFunction(func) {
|
|
return function () {
|
|
try {
|
|
return func.apply(this, arguments);
|
|
} catch (e) {
|
|
exports.unhandled(e);
|
|
throw e;
|
|
}
|
|
};
|
|
};
|
|
|
|
exports.watchPromise = function watchPromise(promise) {
|
|
return promise.catch((e) => {
|
|
log.error("------Error in promise:", e);
|
|
log.error(e.stack);
|
|
exports.unhandled(makeError(e));
|
|
throw e;
|
|
});
|
|
};
|
|
|
|
exports.registerHandler = function (h) {
|
|
if (handler) {
|
|
log.error("registerHandler called after handler was already registered");
|
|
return;
|
|
}
|
|
handler = h;
|
|
for (let error of queue) {
|
|
handler(error);
|
|
}
|
|
queue = [];
|
|
};
|
|
|
|
return exports;
|
|
})();
|
|
null;
|