Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added error events #43

Merged
merged 2 commits into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/goblin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const goblin = {
remove: removeHook,
repository: []
},
errorEmitter: new EventEmitter(),
saveDataTask: undefined
};

Expand Down
36 changes: 25 additions & 11 deletions lib/logger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ const errors = require('./errors');
const mode = require('./mode');
const goblin = require('../goblin');

// Error Hooks Execution
goblin.errorEmitter.on('error', function (exception, error) {
goblin.hooks.repository.forEach(function (hook) {
if (hook.event === 'error') {
hook.callback({msg: exception.toString(), error: error});
}
});
});

function goblinException(prefix, log) {
this.prefix = prefix;
this.log = log;
Expand All @@ -14,19 +23,24 @@ function goblinException(prefix, log) {
module.exports = function(code, error, type) {
const currentMode = mode[goblin.config.mode];

if (currentMode !== mode.production) {
const prefix = goblin.config.logPrefix;
const log = errors[code] || code;
const exception = new goblinException(prefix, log);
const prefix = goblin.config.logPrefix;
const log = errors[code] || code;
const exception = new goblinException(prefix, log);

// Always emit error event
if (error) {
goblin.errorEmitter.emit('error', exception, error);
} else {
goblin.errorEmitter.emit('error', exception);
}

if (currentMode === mode.strict) {
throw exception.toString();
if (currentMode === mode.strict) {
throw exception.toString();
} else if (currentMode === mode.development) {
if (error) {
console[type || 'error'](exception.toString(), error);
} else {
if (error) {
console[type || 'error'](exception.toString(), error);
} else {
console[type || 'error'](exception.toString());
}
console[type || 'error'](exception.toString());
}
}
};