Change how error messages are shown

Instead of throwing the syntax errors with their source file location and needing to then catch them and call a `prettyErrorMessage` function in order to get the formatted error message, now syntax errors know how to pretty-print themselves (their `toString` method gets overridden).

An intermediate `catch` & re-`throw` is needed at the level of `CoffeeScript.compile` and friends. But the benefit of this approach is that now libraries that use the `CoffeeScript` object directly don't need to bother catching the possible compilation errors and calling a special function in order to get the nice error messages; they can just print the error itself (or let it bubble up) and the error will know how to pretty-print itself.
This commit is contained in:
Demian Ferreiro
2013-07-31 08:27:49 -03:00
parent 51c625205b
commit 3f9cdcf1fa
9 changed files with 107 additions and 79 deletions

View File

@@ -150,7 +150,7 @@
};
compileScript = function(file, input, base) {
var compiled, err, message, o, options, t, task, useColors;
var compiled, err, message, o, options, t, task;
if (base == null) {
base = null;
}
@@ -195,8 +195,7 @@
if (CoffeeScript.listeners('failure').length) {
return;
}
useColors = process.stdout.isTTY && !process.env.NODE_DISABLE_COLORS;
message = helpers.prettyErrorMessage(err, file || '[stdin]', input, useColors);
message = err.stack || ("" + err);
if (o.watch) {
return printLine(message + '\x07');
} else {