Skip to content

Commit

Permalink
augment the error
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Mar 22, 2024
1 parent 25f283e commit b9fb4e7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 41 deletions.
7 changes: 3 additions & 4 deletions lib/versioned/^4.0.0/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ 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 checkTaskNotFound = require('./log/check-task-not-found');
var registerExports = require('../../shared/register-exports');

var copyTree = require('../../shared/log/copy-tree');
Expand Down Expand Up @@ -86,13 +86,12 @@ function execute(env, cfg, opts) {
}
});
} catch (err) {
var task = checkTaskNotFound(err);
if (task) {
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));
log.error('Please check the documentation for proper gulpfile formatting');
}
exit(1);
}
Expand Down
29 changes: 0 additions & 29 deletions lib/versioned/^4.0.0/log/check-task-not-found.js

This file was deleted.

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;
19 changes: 11 additions & 8 deletions test/lib/check-task-not-found.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
'use strict';

var expect = require('expect');
var checkTaskNotFound = require('../../lib/versioned/^4.0.0/log/check-task-not-found');
var normalizeError = require('../../lib/versioned/^4.0.0/normalize-error');

describe('lib: checkTaskNotFound', function() {
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');
expect(checkTaskNotFound(err)).toEqual({
target: 'task2',
similar: ['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');
expect(checkTaskNotFound(err)).toEqual({ target: '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');
expect(checkTaskNotFound(err)).toBeUndefined();
normalizeError(err)
expect(err).not.toHaveProperty('task');
expect(err).not.toHaveProperty('similar');
done();
});
});

0 comments on commit b9fb4e7

Please sign in to comment.