From e723d8b6418f306b4cbd347a44a11d52837280fc Mon Sep 17 00:00:00 2001 From: David Cheung Date: Thu, 5 May 2016 18:20:36 -0400 Subject: [PATCH] Throw error upon extending unknown model Create Model now uses findModel to retrieve base instead of getModel and throws error upon base model not found --- lib/registry.js | 9 ++++----- test/registries.test.js | 10 ++++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/registry.js b/lib/registry.js index a50a6e8a..9225fad1 100644 --- a/lib/registry.js +++ b/lib/registry.js @@ -109,11 +109,10 @@ Registry.prototype.createModel = function(name, properties, options) { if (typeof BaseModel === 'string') { var baseName = BaseModel; - BaseModel = this.getModel(BaseModel); - - if (BaseModel === undefined) { - console.warn('Model `%s` is extending an unknown model `%s`. ' + - 'Using `PersistedModel` as the base.', name, baseName); + BaseModel = this.findModel(BaseModel); + if (!BaseModel) { + throw new Error('Model not found: model `' + name + '` is extending an unknown model `' + + baseName + '`.'); } } diff --git a/test/registries.test.js b/test/registries.test.js index 17b525fc..a4b01d56 100644 --- a/test/registries.test.js +++ b/test/registries.test.js @@ -4,6 +4,16 @@ // License text available at https://opensource.org/licenses/MIT describe('Registry', function() { + describe('createModel', function() { + it('should throw error upon extending non-exist base model', function() { + var app = loopback(); + var props = {}; + var opts = { base: 'nonexistent' }; + expect(function() { app.registry.createModel('aModel', props, opts); }) + .to.throw(/model\s`aModel`(.*)unknown\smodel\s`nonexistent`/); + }); + }); + describe('one per app', function() { it('should allow two apps to reuse the same model name', function(done) { var appFoo = loopback();