Skip to content

Commit b6e61de

Browse files
committed
Display error log when invocation fail
1 parent 65064f4 commit b6e61de

File tree

3 files changed

+103
-11
lines changed

3 files changed

+103
-11
lines changed

index.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const BbPromise = require('bluebird');
33
const path = require('path');
44
const _ = require('lodash');
5+
const chalk = require('chalk');
56

67
class ServerlessStepFunctions {
78
constructor(serverless, options) {
@@ -363,26 +364,48 @@ class ServerlessStepFunctions {
363364
this.serverless.cli.printDot();
364365
setTimeout(this.describeExecution.bind(this), 5000);
365366
} else {
367+
this.serverless.cli.consoleLog('');
368+
this.serverless.cli.consoleLog('');
369+
const msg = 'Execution Result -----------------------------------------';
370+
this.serverless.cli.consoleLog(chalk.yellow(msg));
366371
this.serverless.cli.consoleLog('');
367372
this.serverless.cli.consoleLog(result);
373+
374+
if (result.status === 'FAILED') {
375+
return this.getExecutionHistory();
376+
}
368377
}
369378
return BbPromise.resolve();
370379
});
371380
}
372381

382+
getExecutionHistory() {
383+
return this.provider.request('StepFunctions',
384+
'getExecutionHistory',
385+
{
386+
executionArn: this.executionArn,
387+
},
388+
this.options.stage,
389+
this.options.region)
390+
.then((result) => {
391+
this.serverless.cli.consoleLog('');
392+
const msg = 'Error Log ------------------------------------------------';
393+
this.serverless.cli.consoleLog(chalk.yellow(msg));
394+
this.serverless.cli.consoleLog('');
395+
this.serverless.cli.consoleLog(result.events[result.events.length - 1]
396+
.executionFailedEventDetails);
397+
return BbPromise.resolve();
398+
});
399+
}
400+
373401
yamlParse() {
374402
const servicePath = this.serverless.config.servicePath;
375403

376404
if (!servicePath) {
377405
return BbPromise.resolve();
378406
}
379407

380-
let serverlessYmlPath = path.join(servicePath, 'serverless.yml');
381-
if (!this.serverless.utils.fileExistsSync(serverlessYmlPath)) {
382-
serverlessYmlPath = path
383-
.join(this.serverless.config.servicePath, 'serverless.yaml');
384-
}
385-
408+
const serverlessYmlPath = path.join(servicePath, 'serverless.yml');
386409
return this.serverless.yamlParser
387410
.parse(serverlessYmlPath)
388411
.then((serverlessFileParam) => {
@@ -393,7 +416,10 @@ class ServerlessStepFunctions {
393416

394417
compile() {
395418
if (!this.stepFunctions) {
396-
return BbPromise.resolve();
419+
const errorMessage = [
420+
'stepFunctions statement does not exists in serverless.yml',
421+
].join('');
422+
throw new this.serverless.classes.Error(errorMessage);
397423
}
398424

399425
if (typeof this.stepFunctions[this.options.state] === 'undefined') {

index.test.js

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,13 +435,34 @@ describe('ServerlessStepFunctions', () => {
435435

436436
describe('#describeExecution()', () => {
437437
let describeExecutionStub;
438-
beforeEach(() => {
438+
it('should describeExecution with correct params', () => {
439439
describeExecutionStub = sinon.stub(serverlessStepFunctions.provider, 'request')
440440
.returns(BbPromise.resolve({ status: 'SUCCESS' }));
441+
442+
serverlessStepFunctions.describeExecution()
443+
.then(() => {
444+
expect(describeExecutionStub.calledOnce).to.be.equal(true);
445+
expect(describeExecutionStub.calledWithExactly(
446+
'StepFunctions',
447+
'describeExecution',
448+
{
449+
executionArn: serverlessStepFunctions.executionArn,
450+
},
451+
serverlessStepFunctions.options.stage,
452+
serverlessStepFunctions.options.region
453+
)).to.be.equal(true);
454+
serverlessStepFunctions.provider.request.restore();
455+
});
441456
});
442457

443-
it('should describeExecution with correct params'
444-
, () => serverlessStepFunctions.describeExecution()
458+
it('should describeExecution with status FAILED', () => {
459+
describeExecutionStub = sinon.stub(serverlessStepFunctions.provider, 'request')
460+
.returns(BbPromise.resolve({ status: 'FAILED' }));
461+
const getExecutionHistoryStub = sinon
462+
.stub(serverlessStepFunctions, 'getExecutionHistory')
463+
.returns(BbPromise.resolve({ events: [{ executionFailedEventDetails: 'error' }] }));
464+
465+
serverlessStepFunctions.describeExecution()
445466
.then(() => {
446467
expect(describeExecutionStub.calledOnce).to.be.equal(true);
447468
expect(describeExecutionStub.calledWithExactly(
@@ -453,6 +474,33 @@ describe('ServerlessStepFunctions', () => {
453474
serverlessStepFunctions.options.stage,
454475
serverlessStepFunctions.options.region
455476
)).to.be.equal(true);
477+
expect(getExecutionHistoryStub.calledOnce).to.be.equal(true);
478+
serverlessStepFunctions.provider.request.restore();
479+
serverlessStepFunctions.getExecutionHistory.restore();
480+
});
481+
});
482+
});
483+
484+
describe('#getExecutionHistory()', () => {
485+
let getExecutionHistoryStub;
486+
beforeEach(() => {
487+
getExecutionHistoryStub = sinon.stub(serverlessStepFunctions.provider, 'request')
488+
.returns(BbPromise.resolve({ events: [{ executionFailedEventDetails: 'error' }] }));
489+
});
490+
491+
it('should getExecutionHistory with correct params'
492+
, () => serverlessStepFunctions.getExecutionHistory()
493+
.then(() => {
494+
expect(getExecutionHistoryStub.calledOnce).to.be.equal(true);
495+
expect(getExecutionHistoryStub.calledWithExactly(
496+
'StepFunctions',
497+
'getExecutionHistory',
498+
{
499+
executionArn: serverlessStepFunctions.executionArn,
500+
},
501+
serverlessStepFunctions.options.stage,
502+
serverlessStepFunctions.options.region
503+
)).to.be.equal(true);
456504
serverlessStepFunctions.provider.request.restore();
457505
})
458506
);
@@ -473,9 +521,26 @@ describe('ServerlessStepFunctions', () => {
473521
expect(serverlessStepFunctions.stepFunctions).to.be.equal('stepFunctions');
474522
})
475523
);
524+
525+
it('should return resolve when servicePath does not exists', () => {
526+
serverlessStepFunctions.serverless.config.servicePath = null;
527+
serverlessStepFunctions.yamlParse()
528+
.then(() => {
529+
expect(yamlParserStub.callCount).to.be.equal(0);
530+
});
531+
});
476532
});
477533

478534
describe('#compile()', () => {
535+
it('should throw error when stepFunction state does not exists', () => {
536+
expect(() => serverlessStepFunctions.compile()).to.throw(Error);
537+
});
538+
539+
it('should throw error when stateMachine name does not exists', () => {
540+
serverlessStepFunctions.stepFunctions = {};
541+
expect(() => serverlessStepFunctions.compile()).to.throw(Error);
542+
});
543+
479544
it('should comple with correct params', () => {
480545
serverlessStepFunctions.stepFunctions = {
481546
stateMachine: {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"dependencies": {
3939
"lodash": "^4.13.1",
4040
"aws-sdk": "^2.7.19",
41-
"bluebird": "^3.4.0"
41+
"bluebird": "^3.4.0",
42+
"chalk": "^1.1.1"
4243
}
4344
}

0 commit comments

Comments
 (0)