Skip to content
This repository has been archived by the owner on Apr 8, 2020. It is now read-only.

Commit

Permalink
Fixing KeywordRunner and adding methods to output to match the Java API
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromecomte committed Mar 11, 2019
1 parent 0eab664 commit 36bf036
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 36 deletions.
3 changes: 2 additions & 1 deletion step-node-agent/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
.npm
!/bin
!/bin
filemanager/work/
14 changes: 9 additions & 5 deletions step-node-agent/api/controllers/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ module.exports = function Controller (agentContext, fileManager) {
exports.executeKeyword(keywordName, keywordPackageFile, tokenId, argument, properties, outputBuilder, agentContext)
}, function (err) {
console.log('[Controller] Error while attempting to run keyword ' + keywordName + ' :' + err)
outputBuilder.fail('Error while attempting to run keyword', err)
})
} catch (e) {
outputBuilder.failWithException(e)
outputBuilder.fail(e)
}
}

Expand All @@ -100,6 +101,9 @@ module.exports = function Controller (agentContext, fileManager) {
} else {
kwDir = path.resolve(keywordPackageFile + '/' + exports.filemanager.getFolderName(keywordPackageFile) + '/keywords')
}
} else {
// Local execution with KeywordRunner
kwDir = path.resolve(keywordPackageFile + '/keywords')
}

console.log('[Controller] Search keyword file in ' + kwDir + ' for token ' + tokenId)
Expand All @@ -121,21 +125,21 @@ module.exports = function Controller (agentContext, fileManager) {
if (onError) {
if (await onError(e, argument, outputBuilder, session, properties)) {
console.log('[Controller] Keyword execution marked as failed: onError function returned \'true\' on token ' + tokenId)
outputBuilder.failWithException(e)
outputBuilder.fail(e)
} else {
console.log('[Controller] Keyword execution marked as successful: execution failed but the onError function returned \'false\' on token ' + tokenId)
outputBuilder.send()
}
} else {
console.log('[Controller] Keyword execution marked as failed: Keyword execution failed and no onError function found on token ' + tokenId)
outputBuilder.failWithException(e)
outputBuilder.fail(e)
}
}
} else {
outputBuilder.failWithMessage('Unable to find keyword ' + keywordName)
outputBuilder.fail('Unable to find keyword ' + keywordName)
}
} catch (e) {
outputBuilder.failWithMessageAndException('An error occured while attempting to execute the keyword ' + keywordName, e)
outputBuilder.fail('An error occured while attempting to execute the keyword ' + keywordName, e)
}
}

Expand Down
56 changes: 28 additions & 28 deletions step-node-agent/api/controllers/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,49 @@ module.exports = function OutputBuilder (callback) {
}
}

exports.buildDefaultTechnicalError = function (message) {
function buildDefaultTechnicalError (message) {
return { msg: message, type: 'TECHNICAL', root: true, code: 0 }
}

exports.dealWithExceptionObject = function (e) {
if (e instanceof Error) {
exports.builder.payload.error = exports.buildDefaultTechnicalError('[' + e.name + '] ' + e.message)
exports.attach(
{
'name': 'exception.log',
'isDirectory': false,
'description': 'exception stacktrace from keyword',
'hexContent': Buffer.from(e.stack).toString('base64')
})
} else if (typeof e === 'object') {
exports.builder.payload.error = e
} else {
exports.builder.payload.error = exports.buildDefaultTechnicalError(e)
}
function buildDefaultBusinessError (message) {
return { msg: message, type: 'BUSINESS', root: true, code: 0 }
}

exports.failWithException = function (e) {
exports.dealWithExceptionObject(e)
if (callback) {
callback(exports.builder)
}
function attachException (e) {
exports.attach(
{
'name': 'exception.log',
'isDirectory': false,
'description': 'exception stacktrace from keyword',
'hexContent': Buffer.from(e.stack).toString('base64')
})
}

exports.failWithMessage = function (msg) {
exports.builder.payload.error = exports.buildDefaultTechnicalError(msg)
exports.fail = function (arg1, arg2) {
exports.setError(arg1, arg2)
if (callback) {
callback(exports.builder)
}
}

exports.failWithMessageAndException = function (msg, e) {
exports.dealWithExceptionObject(e)
exports.builder.payload.error.msg = msg + '. Message was: ' + exports.builder.payload.error.msg
if (callback) {
callback(exports.builder)
exports.setError = function (arg1, arg2) {
if (typeof arg1 === 'string' || arg1 instanceof String) {
exports.builder.payload.error = buildDefaultTechnicalError(arg1)
if (arg2 && arg2 instanceof Error) {
attachException(arg2)
}
} else if (arg1 instanceof Error) {
exports.builder.payload.error = buildDefaultTechnicalError(arg1.message)
attachException(arg1)
} else if (typeof arg1 === 'object') {
exports.builder.payload.error = arg1
}
}

exports.setBusinessError = function (errorMessage) {
exports.builder.payload.error = buildDefaultBusinessError(errorMessage)
}

exports.attach = function (attachment) {
exports.builder.payload.attachments.push(attachment)
}
Expand Down
24 changes: 24 additions & 0 deletions step-node-agent/keywords/keywords.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ exports.ErrorTestKW = async (input, output, session, properties) => {
throw new Error(input['ErrorMsg'])
}

exports.SetErrorTestKW = async (input, output, session, properties) => {
output.setError(input['ErrorMsg'])
output.send()
}

exports.SetErrorWithExceptionKW = async (input, output, session, properties) => {
output.setError(new Error(input['ErrorMsg']))
output.send()
}

exports.SetErrorWithMessageAndExceptionKW = async (input, output, session, properties) => {
output.setError(input['ErrorMsg'], new Error(input['ErrorMsg']))
output.send()
}

exports.FailKW = async (input, output, session, properties) => {
output.fail(input['ErrorMsg'])
}

exports.BusinessErrorTestKW = async (input, output, session, properties) => {
output.setBusinessError(input['ErrorMsg'])
output.send()
}

exports.ErrorRejectedPromiseTestKW = async (input, output, session, properties) => {
Promise.reject(new Error('test'))
output.send()
Expand Down
34 changes: 32 additions & 2 deletions step-node-agent/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,57 @@ const runner = require('../api/runner/runner')({'Property1': 'Prop1'})
const assert = require('assert')

;(async () => {
// Test the happy path
var output = await runner.run('Echo', {Param1: 'Val1'})
assert.equal(output.payload.Param1, 'Val1')
assert.equal(output.payload.properties.Property1, 'Prop1')

// Test the method output.setError
var errorMsg = 'MyError'
output = await runner.run('SetErrorTestKW', {ErrorMsg: errorMsg, rethrow_error: true})
assert.equal(output.error.msg, errorMsg)
assert.equal(output.error.type, 'TECHNICAL')

// Test the method output.setError with an exception as argument
errorMsg = 'MyError2'
output = await runner.run('SetErrorWithExceptionKW', {ErrorMsg: errorMsg, rethrow_error: true})
assert.equal(output.error.msg, errorMsg)

// Test the method output.setError with an error message and an exception as argument
errorMsg = 'MyError3'
output = await runner.run('SetErrorWithMessageAndExceptionKW', {ErrorMsg: errorMsg, rethrow_error: true})
assert.equal(output.error.msg, errorMsg)
assert.equal(output.attachments.length, 1)

// Test the method output.fail
errorMsg = 'MyError4'
output = await runner.run('FailKW', {ErrorMsg: errorMsg, rethrow_error: true})
assert.equal(output.error.msg, errorMsg)

// Test the method output.setBusinessError
errorMsg = 'MyBusinessError'
output = await runner.run('BusinessErrorTestKW', {ErrorMsg: errorMsg, rethrow_error: true})
assert.equal(output.error.msg, errorMsg)

// Test onError hook
global.isOnErrorCalled = false

const errorMsg1 = 'Error - rethrow'
const output1 = await runner.run('ErrorTestKW', {ErrorMsg: errorMsg1, rethrow_error: true})
assert.equal(output1.error.msg, errorMsg1)
assert.equal(global.isOnErrorCalled, true)

// Test onError hook with no rethrow
global.isOnErrorCalled = false

const errorMsg2 = 'Error - do not rethrow'
const output2 = await runner.run('ErrorTestKW', {ErrorMsg: errorMsg2, rethrow_error: false})
assert.equal(output2.error, undefined)
assert.equal(global.isOnErrorCalled, true)

// Test rejected promises
output = await runner.run('ErrorRejectedPromiseTestKW', {Param1: 'Val1'})
assert.equal(output.error, undefined)

// Test uncaught exceptions
output = await runner.run('ErrorUncaughtExceptionTestKW', {Param1: 'Val1'})
assert.equal(output.error, undefined)

Expand Down

0 comments on commit 36bf036

Please sign in to comment.