Skip to content
Snippets Groups Projects
Commit ffe8681c authored by Jason Mulligan's avatar Jason Mulligan
Browse files

Adding default error handling for throws within inline scripts, fixes #4

parent cd1de2fa
No related branches found
No related tags found
No related merge requests found
......@@ -29,8 +29,18 @@ var Worker = (function () {
});
this.child.on("message", function (msg) {
if (_this.onmessage) {
_this.onmessage.call(_this, JSON.parse(msg));
var message = JSON.parse(msg);
var error = undefined;
if (!message.error && _this.onmessage) {
_this.onmessage.call(_this, message);
}
if (message.error && _this.onerror) {
error = new Error(message.error);
error.stack = message.stack;
_this.onerror.call(_this, error);
}
});
......
......@@ -33,7 +33,9 @@ process.once("message", function (obj) {
process.send(JSON.stringify({ data: msg }));
},
onmessage: void 0,
onerror: void 0,
onerror: function onerror(err) {
process.send(JSON.stringify({ error: err.message, stack: err.stack }));
},
addEventListener: function addEventListener(event, fn) {
if (events.test(event)) {
global["on" + event] = global.self["on" + event] = fn;
......@@ -66,7 +68,11 @@ process.once("message", function (obj) {
});
process.on("message", function (msg) {
(global.onmessage || global.self.onmessage || noop)(JSON.parse(msg));
try {
(global.onmessage || global.self.onmessage || noop)(JSON.parse(msg));
} catch (err) {
(global.onerror || global.self.onerror || noop)(err);
}
});
process.on("error", function (err) {
......
{
"name": "tiny-worker",
"version": "1.1.6",
"version": "1.1.7",
"description": "Tiny WebWorker for Server",
"main": "lib/index.js",
"scripts": {
......
......@@ -19,8 +19,18 @@ class Worker {
});
this.child.on("message", msg => {
if (this.onmessage) {
this.onmessage.call(this, JSON.parse(msg));
const message = JSON.parse(msg);
let error;
if (!message.error && this.onmessage) {
this.onmessage.call(this, message);
}
if (message.error && this.onerror) {
error = new Error(message.error);
error.stack = message.stack;
this.onerror.call(this, error);
}
});
......
......@@ -21,7 +21,7 @@ function toFunction (arg) {
// Bootstraps the Worker
process.once("message", obj => {
let exp = obj.isfn ? toFunction(obj.input) : fs.readFileSync(obj.input, "utf8");
const exp = obj.isfn ? toFunction(obj.input) : fs.readFileSync(obj.input, "utf8");
global.self = {
close: () => {
......@@ -31,7 +31,9 @@ process.once("message", obj => {
process.send(JSON.stringify({data: msg}));
},
onmessage: void 0,
onerror: void 0,
onerror: err => {
process.send(JSON.stringify({error: err.message, stack: err.stack}));
},
addEventListener: (event, fn) => {
if (events.test(event)) {
global["on" + event] = global.self["on" + event] = fn;
......@@ -60,7 +62,11 @@ process.once("message", obj => {
});
process.on("message", msg => {
(global.onmessage || global.self.onmessage || noop)(JSON.parse(msg));
try {
(global.onmessage || global.self.onmessage || noop)(JSON.parse(msg));
} catch (err) {
(global.onerror || global.self.onerror || noop)(err);
}
});
process.on("error", err => {
......
......@@ -53,6 +53,36 @@ exports["inline script"] = {
}
};
exports["inline script - error"] = {
setUp: function (done) {
this.worker = new Worker(function () {
self.onmessage = function (ev) {
throw new Error(ev.data);
};
});
this.msg = "Something went wrong";
this.response = "";
done();
},
test: function (test) {
var self = this;
test.expect(3);
test.notEqual(this.msg, this.response, "Should not match");
this.worker.onerror = function (err) {
self.response = err.message;
self.worker.terminate();
test.equal(self.msg, self.response, "Should be a match");
test.notEqual(err.stack, undefined, "Should not be a match");
test.done();
};
this.worker.postMessage(this.msg);
}
};
exports["inline script - require"] = {
setUp: function (done) {
this.worker = new Worker(function () {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment