diff --git a/lib/middleware/serveThemes.js b/lib/middleware/serveThemes.js index 15dfc131..6cce31a0 100644 --- a/lib/middleware/serveThemes.js +++ b/lib/middleware/serveThemes.js @@ -39,48 +39,48 @@ function createMiddleware({resources}) { fs: fsInterface(resources.all) }); - return function theme(req, res, next) { - const pathname = parseurl(req).pathname; - const filename = basename(pathname); - if (!themeResources.includes(filename)) { - next(); - return; - } + return async function theme(req, res, next) { + try { + const pathname = parseurl(req).pathname; + const filename = basename(pathname); + if (!themeResources.includes(filename)) { + next(); + return; + } - const sourceLessPath = dirname(pathname) + "/library.source.less"; - resources.all.byPath(sourceLessPath).then((sourceLessResource) => { + const sourceLessPath = dirname(pathname) + "/library.source.less"; + const sourceLessResource = await resources.all.byPath(sourceLessPath); if (!sourceLessResource) { // Not found next(); return; } - return builder.build([sourceLessResource]).then(function(createdResources) { - // Pick requested file resource - const resource = createdResources.find((res) => res.getPath().endsWith(filename)); - if (!resource) { - next(new Error(`Theme Build did not return request file "${pathname}"`)); - return; - } - const resourcePath = resource.getPath(); - const {contentType} = getMimeInfo(resourcePath); - res.setHeader("Content-Type", contentType); + const createdResources = await builder.build([sourceLessResource]); + // Pick requested file resource + const resource = createdResources.find((res) => res.getPath().endsWith(filename)); + if (!resource) { + next(new Error(`Theme Build did not return request file "${pathname}"`)); + return; + } + + const resourcePath = resource.getPath(); + const {contentType} = getMimeInfo(resourcePath); + res.setHeader("Content-Type", contentType); - return resource.getBuffer().then((content) => { - res.setHeader("ETag", etag(content)); + const content = await resource.getBuffer(); + res.setHeader("ETag", etag(content)); - if (isFresh(req, res)) { - // client has a fresh copy of the resource - res.statusCode = 304; - res.end(); - return; - } + if (isFresh(req, res)) { + // client has a fresh copy of the resource + res.statusCode = 304; + res.end(); + return; + } - res.end(content.toString()); - }); - }); - }).catch(function(err) { + res.end(content.toString()); + } catch (err) { next(err); - }); + } }; } diff --git a/test/lib/server/middleware/serveThemes.js b/test/lib/server/middleware/serveThemes.js index 37088133..ca0a37e6 100644 --- a/test/lib/server/middleware/serveThemes.js +++ b/test/lib/server/middleware/serveThemes.js @@ -258,3 +258,22 @@ test.serial.cb("Error handling: Request resource that ThemeBuild doesn't return" t.end(); }); }); + +test.serial.cb("Error handling: Unexpected exception within middleware should call next with error", (t) => { + const error = new Error("Unexpected Error"); + + const {middleware, byPath} = createMiddleware(); + byPath.rejects(error); + + const req = { + url: "/resources/sap/ui/test/themes/base/library.css", + headers: {} + }; + + const res = {}; + + middleware(req, res, function(err) { + t.is(err, error); + t.end(); + }); +});