-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Augment
task not found
error with helpful properties (#262)
- Loading branch information
Showing
4 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |