diff --git a/index.js b/index.js index cbdc7642..84bca23c 100644 --- a/index.js +++ b/index.js @@ -179,6 +179,10 @@ function checkRootPathForErrors (rootPath) { try { pathStat = statSync(rootPath) } catch (e) { + if (e.code === 'ENOENT') { + return new Error(`"root" path "${rootPath}" must exist`) + } + return e } diff --git a/test/static.test.js b/test/static.test.js index 4ebd9074..63ecabf1 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -4,7 +4,6 @@ const path = require('path') const fs = require('fs') const url = require('url') const http = require('http') - const t = require('tap') const simple = require('simple-get') const Fastify = require('fastify') @@ -593,6 +592,17 @@ t.test('prefix default', t => { t.doesNotThrow(() => fastify.register(fastifyStatic, pluginOptions)) }) +t.test('root not found error', t => { + t.plan(1) + const rootPath = path.join(__dirname, 'does-not-exist') + const pluginOptions = { root: rootPath } + const fastify = Fastify({ logger: false }) + fastify.register(fastifyStatic, pluginOptions) + fastify.listen(0, err => { + t.equal(err.message, `"root" path "${rootPath}" must exist`) + }) +}) + t.test('send options', t => { t.plan(11) const pluginOptions = {