Skip to content

Commit

Permalink
Error handling by events. Execute event has been moved to manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
ViShell committed Mar 22, 2020
1 parent 319c7f8 commit e07966d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 21 deletions.
17 changes: 11 additions & 6 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const {parse, parseCommand, tokensToCommand} = require('./parser')
const {Manager} = require('./manager')
const {parse, parseCommand, tokensToCommand, extractValue} = require('./parser')
const {Manager, EVENTS} = require('./manager')
const readline = require('readline')
const EventEmmiter = require('events')
const EVENTS = {
commandExecuted: 'commandExecuted'
}

const commands = [
// List commands.
Expand Down Expand Up @@ -110,7 +107,15 @@ function runCommand(_commands, injection = null, tokens) {
injection = _injection
}
const manager = new Manager(allCommands, injection)
const request = tokensToCommand(tokens)
manager.events.on(EVENTS.error, ({e, request}) => {
const { console } = injection
console.log(e.toString())
const stack = extractValue(request.args, 'stack', false)
if (stack) {
console.log(e.stack)
}
})
request = tokensToCommand(tokens)

return manager.execute(request)
}
Expand Down
12 changes: 8 additions & 4 deletions src/manager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const EventEmitter = require('events')
const EVENTS = {
onError: 'onError'
error: 'error',
executed: 'executed'
}

class Manager {
Expand All @@ -10,13 +11,16 @@ class Manager {
this.events = new EventEmitter()
}

execute(request) {
async execute(request) {
const command = this.get(request)
try {
return command.handler({command, request, injection: this.injection, manager: this})
const result = await command.handler({command, request, injection: this.injection, manager: this})
this.events.emit(EVENTS.executed, { request, result })

return result
}
catch(e) {
this.events.emit(EVENTS.onError, e)
this.events.emit(EVENTS.error, {e, request})
}
}

Expand Down
37 changes: 35 additions & 2 deletions test/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ describe('Commander', () => {
title: 'Prints values',
handler: async ({ injection: { console } }) => {
console.log('Run commander testing.')

return true
}
},
],
Expand All @@ -29,6 +27,41 @@ describe('Commander', () => {
const line = _console.objects.pop()
assert.equal(line, 'Run commander testing.')
})

describe('Handle error message', async () => {
const runWithError = async (tokens) => {
await runCommand(
[
{
name: 'print',
title: 'Prints values',
handler: async ({ injection: { console } }) => {
throw new Error('Printer does not work.')
}
},
],
{
console: _console
},
tokens
)
}

it('Error', async () => {
await runWithError(['print'])
const line = _console.objects.pop()
const etalon = 'Error: Printer does not work.'
assert.equal(line, etalon)
})

it('Error with stack', async () => {
await runWithError(['print', '--stack'])
const lines = _console.objects.pop().split('\n')
const etalon = 'Error: Printer does not work.'
assert.equal(lines[0], etalon)
assert.notEqual(lines[6].match(/at callFn/i), null)
})
})
})

describe('CLI', () => {
Expand Down
32 changes: 23 additions & 9 deletions test/manager.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const assert = require('assert')
const { Manager, EVENTS } = require('../src/manager')

describe('CLI commander testing', () => {
describe('Command manager testing', () => {
let manager
const injection = {
time: 'timeString'
Expand Down Expand Up @@ -62,15 +62,29 @@ describe('CLI commander testing', () => {
assert.equal(command.group, 'default')
})

it('Error invoking command.', (done) => {
const request = {
name: 'invokeError',
}
manager.events.once(EVENTS.onError, (e) => {
assert.equal('Test error.', e.message)
done()
describe('Events', () => {
it('Error invoking command.', (done) => {
const request = {
name: 'invokeError',
}
manager.events.once(EVENTS.error, ({e, request: _request}) => {
assert.equal('Test error.', e.message)
assert.equal(request.name, _request.name)
done()
})
manager.execute(request)
})

it('Executed', (done) => {
const request = {
name: 'command',
}
manager.events.once(EVENTS.executed, ({request: _request}) => {
assert.equal(request.name, _request.name)
done()
})
manager.execute(request)
})
manager.execute(request)
})

it('Get', () => {
Expand Down

0 comments on commit e07966d

Please sign in to comment.