Commit 62c08e7c authored by David Sveningsson's avatar David Sveningsson

fix(config): throw ConfigError when elements cannot be loaded

parent 6554f72f
Pipeline #98094390 passed with stages
in 10 minutes and 41 seconds
......@@ -4,6 +4,7 @@ import { Source } from "../context";
import { UserError } from "../error/user-error";
import { Transformer, TRANSFORMER_API } from "../transform";
import { Config } from "./config";
import { ConfigError } from "./error";
import { Severity } from "./severity";
let mockElements: any;
......@@ -316,6 +317,16 @@ describe("config", () => {
const metatable = config.getMetaTable();
expect(Object.keys(metatable.elements)).not.toHaveLength(0);
});
it("should throw ConfigError when module doesn't exist", () => {
const config = Config.fromObject({
elements: ["missing-module"],
});
expect(() => config.getMetaTable()).toThrow(ConfigError);
expect(() => config.getMetaTable()).toThrow(
/Failed to load elements from "missing-module": .*/
);
});
});
describe("transformSource()", () => {
......
......@@ -231,8 +231,15 @@ export class Config {
}
/* assume it is loadable with require() */
// eslint-disable-next-line security/detect-non-literal-require
metaTable.loadFromObject(require(entry));
try {
// eslint-disable-next-line security/detect-non-literal-require
metaTable.loadFromObject(require(entry));
} catch (err) {
throw new ConfigError(
`Failed to load elements from "${entry}": ${err.message}`,
err
);
}
}
metaTable.init();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment