Skip to content

Commit

Permalink
register plugins individually to show better errors (electrode-io#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip authored Jun 20, 2018
1 parent 7d89c8f commit 12e15f9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 22 deletions.
48 changes: 27 additions & 21 deletions lib/electrode-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,32 +126,38 @@ function startElectrodeServer(context) {
};

const registerPlugins = plugins => {
const failMsg = (plg, msg) => {
let withInfo;
if (plg.module) {
withInfo = `with module '${plg.module}`;
} else {
withInfo = `with register function`;
}
return `electrode-server register plugin '${plg.__name}' ${withInfo} ${msg}`;
};
// making the registerPluginsTimeout configurable by apps
const timeout = Math.max(3000, config.electrode.registerPluginsTimeout);
const noteTimeout = showRegisterPluginsNote(timeout);
return new Promise((resolve, reject) => {
server.register(plugins, err => {
if (err) {
logger.error("Register plugins failed", err);
reject(err instanceof Error ? err : new Error(err));
} else {
resolve();
}
});
})
.timeout(timeout)
.catch(Promise.TimeoutError, () => {
return new Promise((resolve, reject) => {
server.stop(() => {
reject(
new Error(
"Electrode Server register plugins timeout. Did you forget next() in a plugin?"
)
);
});
return Promise.each(plugins, plg => {
return new Promise((resolve, reject) => {
server.register(plg, err => {
if (err) {
logger.error(failMsg(plg, "failed"), err);
reject(err instanceof Error ? err : new Error(err));
} else {
resolve();
}
});
})
.finally(() => clearTimeout(noteTimeout));
.timeout(timeout)
.catch(Promise.TimeoutError, () => {
return new Promise((resolve, reject) => {
server.stop(() => {
reject(new Error(failMsg(plg, "timeout - did you forget next()?")));
});
});
});
}).finally(() => clearTimeout(noteTimeout));
};

const startServer = () =>
Expand Down
11 changes: 11 additions & 0 deletions test/plugins/fail-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

function register(server, options, next) {
next("fail-plugin");
}

register.attributes = {
name: "fail-plugin"
};

module.exports = register;
23 changes: 22 additions & 1 deletion test/spec/electrode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ describe("electrode-server", function() {
if (
!_.includes(
error.message,
"Electrode Server register plugins timeout. Did you forget next"
"electrode-server register plugin 'test' with register function timeout - did you forget next"
)
) {
throw error;
Expand Down Expand Up @@ -248,6 +248,27 @@ describe("electrode-server", function() {
});
});

it("should fail if plugin with module register returned error", () => {
let error;
return electrodeServer({
plugins: {
test: {
module: path.join(__dirname, "../plugins/fail-plugin")
}
},
electrode: {
logLevel
}
})
.catch(e => (error = e))
.then(() => {
expect(error).to.exist;
if (!_.includes(error.message, "fail-plugin")) {
throw error;
}
});
});

it("should fail if plugin register failed", () => {
const register = () => {
throw new Error("test plugin failure");
Expand Down

0 comments on commit 12e15f9

Please sign in to comment.