Skip to content

Commit 80f1ea9

Browse files
committed
Improve error message when autoloading invalid view engine
fixes #3403
1 parent c3fb7e5 commit 80f1ea9

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

History.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
unreleased
2+
==========
3+
4+
* Improve error message when autoloading invalid view engine
5+
16
4.15.5 / 2017-09-24
27
===================
38

lib/view.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,15 @@ function View(name, options) {
7676
// load engine
7777
var mod = this.ext.substr(1)
7878
debug('require "%s"', mod)
79-
opts.engines[this.ext] = require(mod).__express
79+
80+
// default engine export
81+
var fn = require(mod).__express
82+
83+
if (typeof fn !== 'function') {
84+
throw new Error('Module "' + mod + '" does not provide a view engine.')
85+
}
86+
87+
opts.engines[this.ext] = fn
8088
}
8189

8290
// store loaded engine

test/fixtures/broken.send

Whitespace-only changes.

test/res.render.js

+14
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ describe('res', function(){
3535
.expect('<p>tobi</p>', done);
3636
})
3737

38+
it('should error without "view engine" set and file extension to a non-engine module', function (done) {
39+
var app = createApp()
40+
41+
app.locals.user = { name: 'tobi' }
42+
43+
app.use(function (req, res) {
44+
res.render(path.join(__dirname, 'fixtures', 'broken.send'))
45+
})
46+
47+
request(app)
48+
.get('/')
49+
.expect(500, /does not provide a view engine/, done)
50+
})
51+
3852
it('should error without "view engine" set and no file extension', function (done) {
3953
var app = createApp();
4054

0 commit comments

Comments
 (0)