Skip to content

Commit e7274a3

Browse files
authored
Merge pull request #1246 from strongloop/backport/fix-connector-loader
Fix datasource to report connector-loading errors
2 parents cbc8c8e + af1d959 commit e7274a3

File tree

5 files changed

+44
-1
lines changed

5 files changed

+44
-1
lines changed

lib/datasource.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,16 @@ function tryModules(names, loader) {
224224
try {
225225
mod = loader(names[m]);
226226
} catch (e) {
227-
/* ignore */
227+
var notFound = e.code === 'MODULE_NOT_FOUND' &&
228+
e.message && e.message.indexOf(names[m]) > 0;
229+
230+
if (notFound) {
231+
debug('Module %s not found, will try another candidate.', names[m]);
232+
continue;
233+
}
234+
235+
debug('Cannot load connector %s: %s', names[m], e.stack || e);
236+
throw e;
228237
}
229238
if (mod) {
230239
break;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"bluebird": "^2.9.9",
3737
"eslint": "^2.13.1",
3838
"eslint-config-loopback": "^4.0.0",
39+
"loopback-connector-throwing": "file:./test/fixtures/loopback-connector-throwing",
3940
"mocha": "^2.1.0",
4041
"should": "^8.0.2"
4142
},

test/datasource.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,24 @@ describe('DataSource', function() {
2424
});
2525
}).should.throw(/loopback-connector-throwing/);
2626
});
27+
28+
it('reports helpful error when connector init via short name throws', function() {
29+
(function() {
30+
// this is what LoopBack does
31+
return new DataSource({
32+
name: 'dsname',
33+
connector: 'throwing',
34+
});
35+
}).should.throw(/expected test error/);
36+
});
37+
38+
it('reports helpful error when connector init via long name throws', function() {
39+
(function() {
40+
// this is what LoopBack does
41+
return new DataSource({
42+
name: 'dsname',
43+
connector: 'loopback-connector-throwing',
44+
});
45+
}).should.throw(/expected test error/);
46+
});
2747
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
2+
// Node module: loopback-datasource-juggler
3+
// This file is licensed under the MIT License.
4+
// License text available at https://opensource.org/licenses/MIT
5+
6+
'use strict';
7+
8+
throw new Error('expected test error');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "loopback-connector-throwing",
3+
"version": "1.0.0",
4+
"description": "A dummy connector that throws at initialization time."
5+
}

0 commit comments

Comments
 (0)