Skip to content

Commit 913dd3a

Browse files
committed
Merge pull request #2105 from strongloop/fix/err-msg-on-connector-error
Improve error message on connector init error
2 parents 05f3573 + 93d487f commit 913dd3a

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

lib/application.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,19 @@ app.models = function() {
219219
* @param {Object} config The data source config
220220
*/
221221
app.dataSource = function(name, config) {
222-
var ds = dataSourcesFromConfig(name, config, this.connectors, this.registry);
223-
this.dataSources[name] =
224-
this.dataSources[classify(name)] =
225-
this.dataSources[camelize(name)] = ds;
226-
return ds;
222+
try {
223+
var ds = dataSourcesFromConfig(name, config, this.connectors, this.registry);
224+
this.dataSources[name] =
225+
this.dataSources[classify(name)] =
226+
this.dataSources[camelize(name)] = ds;
227+
return ds;
228+
} catch (err) {
229+
if (err.message) {
230+
err.message = 'Cannot create data source ' + JSON.stringify(name) +
231+
': ' + err.message;
232+
}
233+
throw err;
234+
}
227235
};
228236

229237
/**
@@ -410,6 +418,8 @@ function dataSourcesFromConfig(name, config, connectorRegistry, registry) {
410418
config.connector = require(connectorPath);
411419
}
412420
}
421+
if (!config.connector.name)
422+
config.connector.name = name;
413423
}
414424

415425
return registry.createDataSource(config);

test/app.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,16 @@ describe('app', function() {
732732
app.dataSource('custom', { connector: 'custom' });
733733
expect(app.dataSources.custom.name).to.equal(loopback.Memory.name);
734734
});
735+
736+
it('adds data source name to error messages', function() {
737+
app.connector('throwing', {
738+
initialize: function() { throw new Error('expected test error'); },
739+
});
740+
741+
expect(function() {
742+
app.dataSource('bad-ds', { connector: 'throwing' });
743+
}).to.throw(/bad-ds.*throwing/);
744+
});
735745
});
736746

737747
describe.onServer('listen()', function() {

0 commit comments

Comments
 (0)