return Error from minify() (#1880)
Have `minify()` return `Error` in `result.error` rather than throwing it.
This commit is contained in:
11
bin/uglifyjs
11
bin/uglifyjs
@@ -172,7 +172,7 @@ function run() {
|
|||||||
UglifyJS.AST_Node.warn_function = function(msg) {
|
UglifyJS.AST_Node.warn_function = function(msg) {
|
||||||
console.error("WARN:", msg);
|
console.error("WARN:", msg);
|
||||||
};
|
};
|
||||||
if (program.stats) program.stats = Date.now();
|
if (program.stats) program.stats = Date.now();
|
||||||
try {
|
try {
|
||||||
if (program.parse) {
|
if (program.parse) {
|
||||||
if (program.parse.acorn) {
|
if (program.parse.acorn) {
|
||||||
@@ -192,8 +192,12 @@ function run() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var result = UglifyJS.minify(files, options);
|
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
|
fatal("ERROR: " + ex.message);
|
||||||
|
}
|
||||||
|
var result = UglifyJS.minify(files, options);
|
||||||
|
if (result.error) {
|
||||||
|
var ex = result.error;
|
||||||
if (ex.name == "SyntaxError") {
|
if (ex.name == "SyntaxError") {
|
||||||
console.error("Parse error at " + ex.filename + ":" + ex.line + "," + ex.col);
|
console.error("Parse error at " + ex.filename + ":" + ex.line + "," + ex.col);
|
||||||
var col = ex.col;
|
var col = ex.col;
|
||||||
@@ -217,8 +221,7 @@ function run() {
|
|||||||
console.error(ex.defs);
|
console.error(ex.defs);
|
||||||
}
|
}
|
||||||
fatal("ERROR: " + ex.message);
|
fatal("ERROR: " + ex.message);
|
||||||
}
|
} else if (program.output == "ast") {
|
||||||
if (program.output == "ast") {
|
|
||||||
console.log(JSON.stringify(result.ast, function(key, value) {
|
console.log(JSON.stringify(result.ast, function(key, value) {
|
||||||
if (skip_key(key)) return;
|
if (skip_key(key)) return;
|
||||||
if (value instanceof UglifyJS.AST_Token) return;
|
if (value instanceof UglifyJS.AST_Token) return;
|
||||||
|
|||||||
@@ -146,6 +146,8 @@ function minify(files, options) {
|
|||||||
result.warnings = warnings;
|
result.warnings = warnings;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
} catch (ex) {
|
||||||
|
return { error: ex };
|
||||||
} finally {
|
} finally {
|
||||||
AST_Node.warn_function = warn_function;
|
AST_Node.warn_function = warn_function;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -114,17 +114,18 @@ describe("minify", function() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
it("Should fail with multiple input and inline source map", function() {
|
it("Should fail with multiple input and inline source map", function() {
|
||||||
assert.throws(function() {
|
var result = Uglify.minify([
|
||||||
Uglify.minify([
|
read("./test/input/issue-520/input.js"),
|
||||||
read("./test/input/issue-520/input.js"),
|
read("./test/input/issue-520/output.js")
|
||||||
read("./test/input/issue-520/output.js")
|
], {
|
||||||
], {
|
sourceMap: {
|
||||||
sourceMap: {
|
content: "inline",
|
||||||
content: "inline",
|
url: "inline"
|
||||||
url: "inline"
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
var err = result.error;
|
||||||
|
assert.ok(err instanceof Error);
|
||||||
|
assert.strictEqual(err.stack.split(/\n/)[0], "Error: inline source map only works with singular input");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -170,17 +171,14 @@ describe("minify", function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("JS_Parse_Error", function() {
|
describe("JS_Parse_Error", function() {
|
||||||
it("should throw syntax error", function() {
|
it("should return syntax error", function() {
|
||||||
assert.throws(function() {
|
var result = Uglify.minify("function f(a{}");
|
||||||
Uglify.minify("function f(a{}");
|
var err = result.error;
|
||||||
}, function(err) {
|
assert.ok(err instanceof Error);
|
||||||
assert.ok(err instanceof Error);
|
assert.strictEqual(err.stack.split(/\n/)[0], "SyntaxError: Unexpected token punc «{», expected punc «,»");
|
||||||
assert.strictEqual(err.stack.split(/\n/)[0], "SyntaxError: Unexpected token punc «{», expected punc «,»");
|
assert.strictEqual(err.filename, "0");
|
||||||
assert.strictEqual(err.filename, "0");
|
assert.strictEqual(err.line, 1);
|
||||||
assert.strictEqual(err.line, 1);
|
assert.strictEqual(err.col, 12);
|
||||||
assert.strictEqual(err.col, 12);
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user