Skip to content

Commit 0f287c4

Browse files
author
Guillaume Chau
committed
Better error handling with messages sent to the client
1 parent 096607c commit 0f287c4

File tree

3 files changed

+85
-31
lines changed

3 files changed

+85
-31
lines changed

packages/vue-component-dev-client/client/dev-client.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,19 @@ Meteor.startup(function () {
199199
_deferReload = 3000
200200
})
201201

202+
// Message
203+
_socket.on('message', function ({ type, message }) {
204+
let func
205+
if (type === 'error') {
206+
func = console.error
207+
} else if (type === 'warn') {
208+
func = console.warn
209+
} else {
210+
func = console.log
211+
}
212+
func(`[HMR] ${message}`)
213+
})
214+
202215
// Reg
203216
const jsImportsReg = /module\.import\((['"])(.+?)\1/g
204217
})

packages/vue-component/plugin/utils.js

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,27 @@ printSourceMap = function(map) {
109109
console.log(map)
110110
}
111111

112-
throwCompileError = function throwCompileError({
113-
inputFile,
114-
path,
115-
action,
116-
message,
117-
line,
118-
column,
119-
tag,
120-
lang,
121-
charIndex,
122-
error,
123-
showError = false,
124-
showStack = false
125-
}) {
112+
throwCompileError = function throwCompileError(options) {
113+
options = Object.assign({}, {
114+
showError: false,
115+
showStack: false,
116+
}, options)
117+
118+
const {
119+
inputFile,
120+
path,
121+
action,
122+
message,
123+
line,
124+
column,
125+
tag,
126+
lang,
127+
charIndex,
128+
error,
129+
showError,
130+
showStack,
131+
} = options
132+
126133
let output = '[vue-component] Error';
127134

128135
// Action
@@ -135,14 +142,18 @@ throwCompileError = function throwCompileError({
135142
output += ' in tag <' + tag + '>';
136143
}
137144

145+
if(column) {
146+
output += ' col:' + column;
147+
}
148+
138149
// Lang
139150
if(lang) {
140151
output += ' using lang ' + lang;
141152
}
142153

143154
// Message
144155
if(message) {
145-
if(action) {
156+
if(!action) {
146157
output += ': ';
147158
} else {
148159
output += ' ';
@@ -154,13 +165,13 @@ throwCompileError = function throwCompileError({
154165
let errMsg = `${output}`;
155166

156167
// Error location
157-
168+
let file
158169
if(path) {
159-
output += ' -> in ' + path;
170+
file = path;
160171
} else if(inputFile) {
161-
output += ' -> in ' + getFullPathInApp(inputFile);
172+
file = getFullPathInApp(inputFile);
162173
} else {
163-
output += ' (unknown source file)';
174+
file = '(unknown source file)';
164175
}
165176

166177
let lineNumber = line;
@@ -172,27 +183,40 @@ throwCompileError = function throwCompileError({
172183
lineNumber = lineResult;
173184
}
174185
}
175-
if(lineNumber) {
176-
output += ' at line ' + lineNumber;
186+
if (!lineNumber) {
187+
lineNumber = 1
177188
}
178189

179-
if(column) {
180-
output += ' col ' + column;
190+
// Native error
191+
if(showError) {
192+
output += ` ${error.message}`
181193
}
182194

183195
// Stack
184196
if(showStack && error && error.stack) {
185197
ouput += '\n' + error.stack;
186198
}
187199

188-
console.error(output);
189-
190-
// Native error
191-
if(showError) {
192-
console.error(error);
200+
if (isDevelopment()) {
201+
global._dev_server.emit('message', {
202+
type: 'error',
203+
message: `${file}:${lineNumber} ${output}`,
204+
})
193205
}
194206

195-
let err = new Error(errMsg);
196-
err.line = lineNumber;
207+
const err = new TemplatingTools.CompileError();
208+
err.message = output
209+
err.file = file
210+
err.line = lineNumber
197211
throw err;
198212
}
213+
214+
getFileContents = (path) => {
215+
if (!fs.existsSync(path)) {
216+
throw new Error('file-not-found')
217+
} else {
218+
return fs.readFileSync(path, {
219+
encoding: 'utf8',
220+
})
221+
}
222+
}

packages/vue-component/plugin/vue-compiler.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import async from 'async';
55
import { Meteor } from 'meteor/meteor';
66
import { EventEmitter } from 'events';
77
import _ from 'lodash';
8+
import 'colors';
89

910
let templateCompiler, transpile;
1011

@@ -554,11 +555,27 @@ function generateJs (vueId, inputFile, compileResult, isHotReload = false) {
554555
} else if(vueVersion === 2) {
555556
const templateCompilationResult = templateCompiler.compile(compileResult.template, {
556557
id: vueId,
558+
warn: (message) => {
559+
const msg = `${inputFilePath}: ${message}`
560+
console.warn(` (!) Warning: ${msg}`.yellow)
561+
if (isDev) {
562+
global._dev_server.emit('message', {
563+
type: 'warn',
564+
message: msg,
565+
})
566+
}
567+
},
557568
});
558569
if(templateCompilationResult.errors && templateCompilationResult.errors.length !== 0) {
559-
console.error(templateCompilationResult.errors);
570+
console.error(...templateCompilationResult.errors);
560571
js += `__vue_options__.render = function(){};\n`;
561572
js += `__vue_options__.staticRenderFns = [];\n`;
573+
if (isDev) {
574+
global._dev_server.emit('message', {
575+
type: 'error',
576+
message: templateCompilationResult.errors.map(e => e.toString()).join('\n'),
577+
})
578+
}
562579
} else {
563580
render = toFunction(templateCompilationResult.render)
564581
staticRenderFns = `[${ templateCompilationResult.staticRenderFns.map(toFunction).join(',')}]`

0 commit comments

Comments
 (0)