Permalink
Browse files

[fix] Output a human readable error when we have dependencies missing…

…. Fixes #3
  • Loading branch information...
1 parent 99d6fdf commit 46a85922b0e2031c295e9f101d58b95de9fb88d0 @3rd-Eden 3rd-Eden committed Jul 5, 2013
Showing with 121 additions and 4 deletions.
  1. +75 −4 index.js
  2. +6 −0 parsers.json
  3. +18 −0 test/primus.test.js
  4. +22 −0 transformers.json
View
@@ -62,6 +62,50 @@ Object.defineProperty(Primus.prototype, 'Socket', {
//
Primus.prototype.version = require('./package.json').version;
+//
+// A list of supported transformers and the required Node.js modules.
+//
+Primus.transformers = require('./transformers.json');
+Primus.parsers = require('./parsers.json');
+
+/**
+ * Simple function to output common errors.
+ *
+ * @param {String} what What is missing.
+ * @param {Object} where Either Primus.parsers or Primus.transformers.
+ * @returns {Object}
+ * @api private
+ */
+Primus.prototype.is = function is(what, where) {
+ var missing = Primus.parsers !== where
+ ? 'transformer'
+ : 'parser'
+ , dependency = where[what];
+
+ return {
+ missing: function write() {
+ console.error('Primus:');
+ console.error('Primus: Missing required npm dependency for '+ what);
+ console.error('Primus: Please run the following command and try again:');
+ console.error('Primus:');
+ console.error('Primus: npm install --save %s', dependency.server);
+ console.error('Primus:');
+
+ return 'Missing dependencies for '+ missing +': "'+ what + '"';
+ },
+ unknown: function write() {
+ console.error('Primus:');
+ console.error('Primus: Unsupported %s: "%s"', missing, what);
+ console.error('Primus: We only support the following %ss:', what);
+ console.error('Primus:');
+ console.error('Primus: %s', Object.keys(where).join(', '));
+ console.error('Primus:');
+
+ return 'Unsupported '+ missing +': "'+ what +'"';
+ }
+ };
+};
+
/**
* Initialise the real-time transport that was chosen.
*
@@ -70,16 +114,31 @@ Primus.prototype.version = require('./package.json').version;
*/
Primus.prototype.initialise = function initialise(Transformer) {
Transformer = Transformer || 'websockets';
+ var transformer;
if ('string' === typeof Transformer) {
- Transformer = require('./transformers/'+ Transformer.toLowerCase());
+ Transformer = transformer = Transformer.toLowerCase();
+
+ //
+ // This is a unknown transporter, it could be people made a typo.
+ //
+ if (!(Transformer in Primus.transformers)) {
+ throw new Error(this.is(Transformer, Primus.transformers).unknown());
+ }
+
+ try {
+ Transformer = require('./transformers/'+ transformer);
+ this.transformer = new Transformer(this);
+ } catch (e) {
+ throw new Error(this.is(transformer, Primus.transformers).missing());
+ }
}
if ('function' !== typeof Transformer) {
throw new Error('The given transformer is not a constructor');
}
- this.transformer = new Transformer(this);
+ this.transformer = this.transformer || new Transformer(this);
this.on('connection', function connection(stream) {
this.connected++;
@@ -116,11 +175,23 @@ Primus.prototype.parsers = function parsers(parser) {
parser = parser || 'json';
if ('string' === typeof parser) {
- parser = require('./parsers/'+ parser.toLowerCase());
+ parser = parser.toLowerCase();
+
+ //
+ // This is a unknown parser, it could be people made a typo.
+ //
+ if (!(parser in Primus.parsers)) {
+ throw new Error(this.is(parser, Primus.parsers).unknown());
+ }
+
+ try { parser = require('./parsers/'+ parser); }
+ catch (e) {
+ throw new Error(this.is(parser, Primus.parsers).missing());
+ }
}
if ('object' !== typeof parser) {
- throw new Error('The given parser is not an Object');
+ throw new Error('The given parser is not an Object.');
}
this.encoder = parser.encoder;
View
@@ -0,0 +1,6 @@
+{
+ "json": {},
+ "jsonh": {
+ "server": "jsonh"
+ }
+}
View
@@ -135,6 +135,24 @@ describe('Primus', function () {
});
});
+ it('throws a human readable error for an unsupported transformer', function () {
+ try {
+ new Primus(server, { transformer: 'cowsack' });
+ } catch (e) {
+ expect(e).to.be.instanceOf(Error);
+ expect(e.message).to.include('cowsack');
+ }
+ });
+
+ it('throws a human readable error for an unsupported parser', function () {
+ try {
+ new Primus(server, { parser: 'cowsack' });
+ } catch (e) {
+ expect(e).to.be.instanceOf(Error);
+ expect(e.message).to.include('cowsack');
+ }
+ });
+
describe('#forEach', function () {
it('iterates over all active connections', function (done) {
var spark = new primus.Spark()
View
@@ -0,0 +1,22 @@
+{
+ "websockets": {
+ "server": "ws",
+ "client": "ws"
+ },
+ "engine.io": {
+ "server": "engine.io",
+ "client": "engine.io-client"
+ },
+ "socket.io": {
+ "server": "socket.io",
+ "client": "socket.io-client"
+ },
+ "browserchannel": {
+ "server": "browserchannel",
+ "client": "browserchannel"
+ },
+ "sockjs": {
+ "server": "sockjs",
+ "client": "sockjs-client-node"
+ }
+}

0 comments on commit 46a8592

Please sign in to comment.