Skip to content

Commit 290b165

Browse files
committed
Throw an original error if err.code is unexpected
1 parent b0895cd commit 290b165

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,15 @@ module.exports = function (source) {
7070
vueCompiler = require('vue-template-compiler')
7171
compileVueTemplate = require('@vue/component-compiler-utils').compileTemplate
7272
} catch (err) {
73-
throw new Error(
74-
"Failed to import vue-template-compiler or/and @vue/component-compiler-utils: \n" +
75-
"If you intend to use 'vue-component', `vue-render-functions` mode, install both to your project: \n" +
76-
"https://hmsk.github.io/frontmatter-markdown-loader/vue.html"
77-
);
73+
if (err.code === "MODULE_NOT_FOUND") {
74+
throw new Error(
75+
"Failed to import vue-template-compiler or/and @vue/component-compiler-utils: \n" +
76+
"If you intend to use 'vue-component', `vue-render-functions` mode, install both to your project: \n" +
77+
"https://hmsk.github.io/frontmatter-markdown-loader/vue.html"
78+
);
79+
} else {
80+
throw err;
81+
}
7882
}
7983

8084
const vueRootClass = options.vue && options.vue.root ? options.vue.root : 'frontmatter-markdown';

test/frontmatter-markdown-loader.test.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,20 +168,33 @@ describe("frontmatter-markdown-loader", () => {
168168

169169
it("throw if vue-template|compiler is not installed in the project", () => {
170170
jest.mock('vue-template-compiler', () => {
171-
throw new Error();
171+
const error = new Error()
172+
error.code = 'MODULE_NOT_FOUND'
173+
throw error
172174
});
173175
expect(() => {
174176
load(markdownWithFrontmatter, contextEnablingVueRenderFunctions());
175-
}).toThrow();
177+
}).toThrow(/Failed to import/);
176178
});
177179

178180
it("throw if @vue/component-compiler-utils is not installed in the project", () => {
179181
jest.mock("@vue/component-compiler-utils", () => {
180-
throw new Error();
182+
const error = new Error()
183+
error.code = 'MODULE_NOT_FOUND'
184+
throw error
181185
});
182186
expect(() => {
183187
load(markdownWithFrontmatter, contextEnablingVueRenderFunctions());
184-
}).toThrow();
188+
}).toThrow(/Failed to import/);
189+
});
190+
191+
it("throw if unintentional exception by importing @vue/component-compiler-utils", () => {
192+
jest.mock("@vue/component-compiler-utils", () => {
193+
throw new Error('unintentional problem')
194+
});
195+
expect(() => {
196+
load(markdownWithFrontmatter, contextEnablingVueRenderFunctions());
197+
}).toThrow('unintentional problem');
185198
});
186199
});
187200

@@ -243,20 +256,33 @@ describe("frontmatter-markdown-loader", () => {
243256

244257
it("throw if vue-template|compiler is not installed in the project", () => {
245258
jest.mock('vue-template-compiler', () => {
246-
throw new Error();
259+
const error = new Error()
260+
error.code = 'MODULE_NOT_FOUND'
261+
throw error
247262
});
248263
expect(() => {
249264
load(markdownWithFrontmatterIncludingChildComponent, contextEnablingVueComponent());
250-
}).toThrow();
265+
}).toThrow(/Failed to import/);
251266
});
252267

253268
it("throw if @vue/component-compiler-utils is not installed in the project", () => {
254269
jest.mock("@vue/component-compiler-utils", () => {
255-
throw new Error();
270+
const error = new Error()
271+
error.code = 'MODULE_NOT_FOUND'
272+
throw error
273+
});
274+
expect(() => {
275+
load(markdownWithFrontmatterIncludingChildComponent, contextEnablingVueComponent());
276+
}).toThrow(/Failed to import/);
277+
});
278+
279+
it("throw if unintentional exception by importing @vue/component-compiler-utils", () => {
280+
jest.mock("@vue/component-compiler-utils", () => {
281+
throw new Error('unintentional problem')
256282
});
257283
expect(() => {
258284
load(markdownWithFrontmatterIncludingChildComponent, contextEnablingVueComponent());
259-
}).toThrow();
285+
}).toThrow('unintentional problem');
260286
});
261287
});
262288

0 commit comments

Comments
 (0)