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

address InvalidRelationship and logging #4038

Merged
merged 2 commits into from
May 25, 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
12 changes: 11 additions & 1 deletion packages/composer-common/lib/log/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,17 @@ class Logger {
}

// use the local version of padding rather than sprintf etc for speed
_logger.log(logLevel,callbackData+':'+this.padRight(this.str25,this.className)+':'+this.padRight(this.str25,method+'()'),msg, args);
const preamble = callbackData + ':' + this.padRight(this.str25,this.className) + ':' + this.padRight(this.str25,method+'()');

try {
_logger.log(logLevel, preamble, msg, args);
} catch(error) {
// an error can be thrown if for example using the winsonInjector logger and an argument is
// an InvalidRelationship where attempts to get object defined properties (which the winstonInjecttor does)
// throws an error.
let safeArgs = args.map(arg => arg.toString());
_logger.log(logLevel, preamble, msg, safeArgs);
}
}

}
Expand Down
19 changes: 19 additions & 0 deletions packages/composer-common/test/log/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ describe('Logger', () => {
sinon.assert.calledWith(stubLogger.log, 'debug', sinon.match(/ScriptManager.*methodname\(\)/), 'message',['arg1','arg2','arg3']);
});

it('should handle internal logger throwing an error', () => {
let stubLogger= {
log: sinon.stub().onFirstCall().throws(new Error('I do not like the args'))
};

let stubArg2 = {
toString: sinon.stub().returns('My To String')
};

Logger.setFunctionalLogger(stubLogger);
Logger._envDebug='composer[debug]:*';
let logger = new Logger('ScriptManager');

logger.intlog('debug','methodname','message','arg1',stubArg2,'arg3');
sinon.assert.calledTwice(stubLogger.log);
sinon.assert.calledWith(stubLogger.log.firstCall, 'debug', sinon.match(/ScriptManager.*methodname\(\)/), 'message',['arg1',stubArg2,'arg3']);
sinon.assert.calledWith(stubLogger.log.secondCall, 'debug', sinon.match(/ScriptManager.*methodname\(\)/), 'message',['arg1','My To String','arg3']);
});

it('should log to the functional logger, errors', () => {
let stubLogger= {
log: sinon.stub()
Expand Down
8 changes: 6 additions & 2 deletions packages/composer-runtime/lib/invalidrelationship.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ class InvalidRelationship extends Relationship {
configurable: false,
enumerable: true,
get: () => {
throw error;
const err = new Error(`attempt to get property ${propertyName} on an InvalidRelationship is not allowed. InvalidRelationship created due to ${error.message}`);
LOG.error(err);
throw err;
},
set: () => {
throw error;
const err = new Error(`attempt to set property ${propertyName} on an InvalidRelationship is not allowed. InvalidRelationship created due to ${error.message}`);
LOG.error(err);
throw err;
}
});
}
Expand Down
24 changes: 12 additions & 12 deletions packages/composer-runtime/test/invalidrelationship.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,73 +73,73 @@ describe('InvalidRelationship', () => {
it('should throw attempting to get the primitive value', () => {
(() => {
relationship.value === 'hello';
}).should.throw(/such error/);
}).should.throw(/ such error/);
});

it('should throw attempting to set the primitive value', () => {
(() => {
relationship.value = 'hello';
}).should.throw(/such error/);
}).should.throw(/.*is not allowed.*such error/);
});

it('should throw attempting to get the primitive array value', () => {
(() => {
relationship.values[1] === 'hello';
}).should.throw(/such error/);
}).should.throw(/.*is not allowed.*such error/);
});

it('should throw attempting to set the primitive array value', () => {
(() => {
relationship.values[1] = 'hello';
}).should.throw(/such error/);
}).should.throw(/.*is not allowed.*such error/);
});

it('should throw attempting to get the concept value', () => {
(() => {
relationship.concept.value === 'hello';
}).should.throw(/such error/);
}).should.throw(/.*is not allowed.*such error/);
});

it('should throw attempting to set the concept value', () => {
(() => {
relationship.concept.value = 'hello';
}).should.throw(/such error/);
}).should.throw(/.*is not allowed.*such error/);
});

it('should throw attempting to get the concept array value', () => {
(() => {
relationship.concepts[1].value === 'hello';
}).should.throw(/such error/);
}).should.throw(/.*is not allowed.*such error/);
});

it('should throw attempting to set the concept array value', () => {
(() => {
relationship.concepts[1].value = 'hello';
}).should.throw(/such error/);
}).should.throw(/.*is not allowed.*such error/);
});

it('should throw attempting to get the relationship value', () => {
(() => {
relationship.owner.value === 'hello';
}).should.throw(/such error/);
}).should.throw(/.*is not allowed.*such error/);
});

it('should throw attempting to set the relationship value', () => {
(() => {
relationship.owner.value = 'hello';
}).should.throw(/such error/);
}).should.throw(/.*is not allowed.*such error/);
});

it('should throw attempting to get the relationship array value', () => {
(() => {
relationship.owners[1].value === 'hello';
}).should.throw(/such error/);
}).should.throw(/.*is not allowed.*such error/);
});

it('should throw attempting to set the relationship array value', () => {
(() => {
relationship.owners[1].value = 'hello';
}).should.throw(/such error/);
}).should.throw(/.*is not allowed.*such error/);
});

});
Expand Down