Skip to content

Commit

Permalink
chore: Augment task not found error with helpful properties (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk authored Mar 22, 2024
1 parent e5c7983 commit 9a1d013
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/versioned/^4.0.0/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var tildify = require('../../shared/tildify');
var logTasks = require('../../shared/log/tasks');
var logEvents = require('./log/events');
var logSyncTask = require('./log/sync-task');
var normalizeError = require('./normalize-error');
var logTasksSimple = require('./log/tasks-simple');
var registerExports = require('../../shared/register-exports');

Expand Down Expand Up @@ -85,8 +86,13 @@ function execute(env, cfg, opts) {
}
});
} catch (err) {
log.error(chalk.red(err.message));
log.error('To list available tasks, try running: gulp --tasks');
normalizeError(err);
if (err.task) {
log.error(chalk.red(err.message));
log.error(chalk.red('To list available tasks, try running: gulp --tasks'));
} else /* istanbul ignore next */ {
log.error(chalk.red(err.message));
}
exit(1);
}
});
Expand Down
26 changes: 26 additions & 0 deletions lib/versioned/^4.0.0/normalize-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

// Normalize an undertaker v1 error like an undertaker v2 error
function normalizeError(err) {
/* istanbul ignore if */
if (!err || !err.message) {
return;
}

var fixed0 = 'Task never defined: ';
var fixed1 = ' - did you mean? ';

if (err.message.startsWith(fixed0)) {
var task = err.message.slice(fixed0.length);
var index = task.indexOf(fixed1);

if (index >= 0) {
err.similar = task.slice(index + fixed1.length).split(', ');
err.task = task.slice(0, index);
} else {
err.task = task
}
}
}

module.exports = normalizeError;
15 changes: 15 additions & 0 deletions test/execution-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ describe('execution error', function() {
}
});

it('should output an error if a task is not defined but a similar task is found', function(done) {
var opts = { cwd: path.join(__dirname, './fixtures/gulpfiles') };
exec(gulp('test0'), opts, cb);

function cb(err, stdout, stderr) {
expect(err).not.toBeNull();
expect(err.code).toEqual(1);
expect(eraseTime(stdout)).toMatch('Using gulpfile ');
expect(eraseTime(stderr)).toEqual(
'Task never defined: test0 - did you mean? test1, test2, test3, test4, test5, test6, test7, test8\n' +
'To list available tasks, try running: gulp --tasks\n');
done();
}
});

it('should output an error if gulp version is unsupported', function(done) {
var opts = { cwd: path.join(__dirname, './fixtures/errors/bad-gulp-version') };
exec(gulp(), opts, cb);
Expand Down
31 changes: 31 additions & 0 deletions test/lib/check-task-not-found.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

var expect = require('expect');
var normalizeError = require('../../lib/versioned/^4.0.0/normalize-error');

describe('lib: normalizeError', function() {

it('Should return target task and similar tasks if both are included in error message', function(done) {
var err = new Error('Task never defined: task2 - did you mean? task0, task1');
normalizeError(err);
expect(err).toHaveProperty('task', 'task2');
expect(err).toHaveProperty('similar', ['task0', 'task1']);
done();
});

it('Should return only target task if similar tasks is not included in error message', function(done) {
var err = new Error('Task never defined: task2');
normalizeError(err)
expect(err).toHaveProperty('task', 'task2');
expect(err).not.toHaveProperty('similar');
done();
});

it('Should return undefined if error is other', function(done) {
var err = new Error('xxx');
normalizeError(err)
expect(err).not.toHaveProperty('task');
expect(err).not.toHaveProperty('similar');
done();
});
});

0 comments on commit 9a1d013

Please sign in to comment.