Improve error handling when there is no port-data-source file present.

This commit is contained in:
Dillon Kearns 2022-03-02 14:45:34 -08:00
parent f572ee7218
commit 8f1e25e605
2 changed files with 13 additions and 5 deletions

View File

@ -121,17 +121,25 @@ async function run(options) {
metafile: true,
bundle: false,
watch: false,
logLevel: "silent",
})
.then((result) => {
global.portsFilePath = Object.keys(result.metafile.outputs)[0];
console.log("Watching port-data-source...");
})
.catch((error) => {
console.error("Failed to start port-data-source watcher", error);
if (
error.errors.length === 1 &&
error.errors[0].text.includes(
`Could not resolve "./port-data-source"`
)
) {
console.warn("No port-data-source file found.");
} else {
console.error("Failed to load port-data-source file", error);
}
});
// TODO run esbuild for ports file
// TODO extract common code for compiling ports file?
// TODO set global.portsFilePath
XMLHttpRequest = {};
const compileCli = compileCliApp(options);

View File

@ -44,7 +44,7 @@ function lookupOrPerform(portsFile, mode, rawRequest, hasFsAccess) {
const { fs } = require("./request-cache-fs.js")(hasFsAccess);
return new Promise(async (resolve, reject) => {
const request = toRequest(rawRequest);
const portsHash = portsFile.match(/-([^-]+)\.mjs$/)[1];
const portsHash = (portsFile && portsFile.match(/-([^-]+)\.mjs$/)[1]) || "";
const responsePath = fullPath(portsHash, request, hasFsAccess);
// TODO check cache expiration time and delete and go to else if expired
@ -67,7 +67,7 @@ function lookupOrPerform(portsFile, mode, rawRequest, hasFsAccess) {
if (portDataSourceFound) {
throw `DataSource.Port.send "${portName}" is not defined. Be sure to export a function with that name from port-data-source.js`;
} else {
throw `DataSource.Port.send "${portName}" was called, but I couldn't find the port definitions file 'port-data-source.js'.`;
throw `DataSource.Port.send "${portName}" was called, but I couldn't find a port definitions file. Create a 'port-data-source.ts' or 'port-data-source.js' file and export a ${portName} function.`;
}
} else if (typeof portDataSource[portName] !== "function") {
throw `DataSource.Port.send "${portName}" is not a function. Be sure to export a function with that name from port-data-source.js`;