-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from KernelDeimos/master
dev: add last-error command, context, and modules
- Loading branch information
Showing
6 changed files
with
58 additions
and
6 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
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
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,33 @@ | ||
const ERROR_BUFFER_LIMIT = 20; | ||
|
||
export const ErrorAPI = Symbol('ErrorAPI'); | ||
|
||
export default ({ context }) => { | ||
// State Variables | ||
const errors = []; | ||
|
||
context.events.on('error', (error) => { | ||
context[ErrorAPI].report(error); | ||
}); | ||
|
||
// Module Methods | ||
context[ErrorAPI] = { | ||
// Add an error to the error history | ||
report (error) { | ||
errors.push(error); | ||
if (errors.length > ERROR_BUFFER_LIMIT) { | ||
errors = errors.slice(errors.length - ERROR_BUFFER_LIMIT); | ||
} | ||
}, | ||
// Print the last error from the error history, | ||
// and remove it from the history | ||
showLast () { | ||
const err = errors.pop(); | ||
if (err) { | ||
console.error(err); | ||
} else { | ||
console.log('No errors to report'); | ||
} | ||
} | ||
}; | ||
}; |