Commit c5a4f381 authored by David Sveningsson's avatar David Sveningsson

fix(config): better error message when transformer fails to load

parent 9774978f
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`config transform() should throw sane error when transformer fails 1`] = `"When transforming \\"/path/to/test.foo\\": Failed to frobnicate a baz"`;
exports[`config transform() should throw sane error when transformer fails 1`] = `"When transforming \\"/path/to/test.foo\\": Failed to frobnicate a baz"`;
exports[`config transform() should throw sane error when transformer fails to load 1`] = `"Failed to load transformer \\"missing-transformer\\""`;
......@@ -381,7 +381,7 @@ describe("config", () => {
]);
});
it("should throw sane error when transformer fails ", () => {
it("should throw sane error when transformer fails", () => {
const config = Config.fromObject({
transform: {
"^.*\\.foo$":
......@@ -393,6 +393,16 @@ describe("config", () => {
config.transform("/path/to/test.foo")
).toThrowErrorMatchingSnapshot();
});
it("should throw sane error when transformer fails to load", () => {
const config = Config.fromObject({
transform: {
"^.*\\.foo$":
"missing-transformer" /* mocked transformer, see top of file */,
},
});
expect(() => config.init()).toThrowErrorMatchingSnapshot();
});
});
describe("init()", () => {
......
......@@ -2,13 +2,13 @@ import deepmerge from "deepmerge";
import fs from "fs";
import path from "path";
import { Source } from "../context";
import { NestedError } from "../error/nested-error";
import { UserError } from "../error/user-error";
import { NestedError } from "../error";
import { MetaTable } from "../meta";
import { MetaDataTable } from "../meta/element";
import { Plugin } from "../plugin";
import { ConfigData, TransformMap } from "./config-data";
import defaultConfig from "./default";
import { ConfigError } from "./error";
import { parseSeverity, Severity } from "./severity";
interface Transformer {
......@@ -43,7 +43,10 @@ function loadFromFile(filename: string): ConfigData {
// eslint-disable-next-line security/detect-non-literal-require
json = require(filename);
} catch (err) {
throw new UserError(`Failed to read configuration from "${filename}"`, err);
throw new ConfigError(
`Failed to read configuration from "${filename}"`,
err
);
}
/* expand any relative paths */
......@@ -337,13 +340,17 @@ export class Config {
private precompileTransformers(transform: TransformMap): Transformer[] {
return Object.entries(transform).map(([pattern, module]) => {
return {
// eslint-disable-next-line security/detect-non-literal-regexp
pattern: new RegExp(pattern),
try {
return {
// eslint-disable-next-line security/detect-non-literal-regexp
pattern: new RegExp(pattern),
// eslint-disable-next-line security/detect-non-literal-require
fn: require(module.replace("<rootDir>", this.rootDir)),
};
// eslint-disable-next-line security/detect-non-literal-require
fn: require(module.replace("<rootDir>", this.rootDir)),
};
} catch (err) {
throw new ConfigError(`Failed to load transformer "${module}"`, err);
}
});
}
......
import { UserError } from "../error";
export class ConfigError extends UserError {}
export { UserError } from "./user-error";
export { NestedError } from "./nested-error";
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