Differentiate between missing port data source file and import error

This commit is contained in:
Johannes Maas 2022-03-20 15:22:00 +01:00
parent 10cf930226
commit d9ecd328d8

View File

@ -54,26 +54,32 @@ function lookupOrPerform(portsFile, mode, rawRequest, hasFsAccess) {
resolve(responsePath);
} else {
let portDataSource = {};
let portDataSourceFound = false;
let portDataSourceImportError = null;
try {
if (portsFile === undefined) {
throw "missing"
}
const portDataSourcePath = path.join(process.cwd(), portsFile);
// On Windows, we need cannot use paths directly and instead must use a file:// URL.
portDataSource = await import(url.pathToFileURL(portDataSourcePath).href);
portDataSourceFound = true;
} catch (e) {}
} catch (e) {
portDataSourceImportError = e;
}
if (request.url === "elm-pages-internal://port") {
try {
const { input, portName } = rawRequest.body.args[0];
if (!portDataSource[portName]) {
if (portDataSourceFound) {
throw `DataSource.Port.send "${portName}" is not defined. Be sure to export a function with that name from port-data-source.js`;
if (portDataSourceImportError === null) {
throw `DataSource.Port.send "${portName}" was called, but I couldn't find a function with that name in the port definitions file. Is it exported correctly?`;
} else if (portDataSourceImportError === "missing") {
throw `DataSource.Port.send "${portName}" was called, but I couldn't find the port definitions file. Be sure to create a 'port-data-source.ts' or 'port-data-source.js' file and maybe restart the dev server.`;
} else {
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.`;
throw `DataSource.Port.send "${portName}" was called, but I couldn't import the port definitions file, because of this exception: \`${portDataSourceImportError}\` Are there syntax errors or expections thrown during import?`;
}
} 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`;
throw `DataSource.Port.send "${portName}" was called, but it is not a function. Be sure to export a function with that name from port-data-source.js`;
}
await fs.promises.writeFile(
responsePath,