Skip to content

Commit

Permalink
fix(createError): separate createError into own module
Browse files Browse the repository at this point in the history
  • Loading branch information
terinjokes committed Oct 26, 2015
1 parent 735e04d commit e3709af
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
21 changes: 21 additions & 0 deletions lib/createError.js
@@ -0,0 +1,21 @@
'use strict';
var PluginError = require('gulp-util/lib/PluginError');
var pluginName = 'gulp-uglify';

module.exports = function createError(file, err) {
if (typeof err === 'string') {
return new PluginError(pluginName, file.path + ': ' + err, {
fileName: file.path,
showStack: false
});
}

var msg = err.message || err.msg || 'unspecified error';

return new PluginError(pluginName, file.path + ': ' + msg, {
fileName: file.path,
lineNumber: err.line,
stack: err.stack,
showStack: false
});
};
21 changes: 2 additions & 19 deletions minifier.js
Expand Up @@ -6,8 +6,9 @@ var log = require('fancy-log');
var applySourceMap = require('vinyl-sourcemaps-apply');
var saveLicense = require('uglify-save-license');
var isObject = require('isobject');
var createError = require('./lib/createError');

var reSourceMapComment = /\n\/\/# sourceMappingURL=.+?$/;
var pluginName = 'gulp-uglify';

function trycatch(fn, handle) {
try {
Expand Down Expand Up @@ -42,24 +43,6 @@ function setup(opts) {
return options;
}

function createError(file, err) {
if (typeof err === 'string') {
return new PluginError(pluginName, file.path + ': ' + err, {
fileName: file.path,
showStack: false
});
}

var msg = err.message || err.msg || 'unspecified error';

return new PluginError(pluginName, file.path + ': ' + msg, {
fileName: file.path,
lineNumber: err.line,
stack: err.stack,
showStack: false
});
}

module.exports = function (opts, uglify) {
function minify(file, encoding, callback) {
var options = setup(opts || {});
Expand Down
21 changes: 21 additions & 0 deletions test/createError.js
@@ -0,0 +1,21 @@
'use strict';
var test = require('tape');
var Vinyl = require('vinyl');
var createError = require('../lib/createError');

var testOkContentsInput = '"use strict"; (function(console, first, second) { console.log(first + second) }(5, 10))';
var testFile = new Vinyl({
cwd: '/home/terin/broken-promises/',
base: '/home/terin/broken-promises/test',
path: '/home/terin/broken-promises/test/test2.js',
contents: new Buffer(testOkContentsInput)
});

test('should report "unspecified error" if err has no msg or message properties', function (t) {
t.plan(3);
var e = createError(testFile, {});

t.ok(e instanceof Error, 'argument should be of type error');
t.equal(e.plugin, 'gulp-uglify', 'error is from gulp-uglify');
t.true(e.message.match(/unspecified error$/), 'error should not be specified');
});

0 comments on commit e3709af

Please sign in to comment.