[component-docs] Fix requests for non-existent .html component files

Such requests shouldn't crash the component docs server but rather
result in a 404.

[email protected]

Bug: None
Change-Id: I345126da8f2208d2eff248d157cfb3cfd72a0279
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/5272592
Commit-Queue: Alex Rudenko <[email protected]>
Auto-Submit: Simon Zünd <[email protected]>
Reviewed-by: Alex Rudenko <[email protected]>
Commit-Queue: Simon Zünd <[email protected]>
diff --git a/scripts/component_server/server.js b/scripts/component_server/server.js
index 207b1b5..8b0a190 100644
--- a/scripts/component_server/server.js
+++ b/scripts/component_server/server.js
@@ -302,7 +302,12 @@
      */
     const baseUrlForSharedResource =
         componentDocsBaseArg && componentDocsBaseArg.endsWith(sharedResourcesBase) ? '/' : `/${sharedResourcesBase}`;
-    const fileContents = await fs.promises.readFile(path.join(componentDocsBaseFolder, filePath), {encoding: 'utf8'});
+    const fullPath = path.join(componentDocsBaseFolder, filePath);
+    if (!(await checkFileExists(fullPath))) {
+      send404(response, '404, File not found');
+      return;
+    }
+    const fileContents = await fs.promises.readFile(fullPath, {encoding: 'utf8'});
 
     const linksToStyleSheets =
         styleSheetPaths