Skip to content

Commit

Permalink
Added ustomMessage feature to rejectWithErrorString()
Browse files Browse the repository at this point in the history
  • Loading branch information
mwittig committed Jun 18, 2016
1 parent b9c10ae commit 8a43b80
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
3 changes: 2 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Base object providing device helper functions. **The functions described
* **Object** *device* - the device object
* **String** *deviceName* - the device name to be used for log output

## rejectWithErrorString(reject, error)
## rejectWithErrorString(reject, error, [customMessage])

Outputs an error message and optionally rejects a Promise on return.
If the debug property is set on the device a stack trace is output.
Expand All @@ -38,6 +38,7 @@ Outputs an error message and optionally rejects a Promise on return.

* **Function** *reject* - function to reject a promise on return, may be null
* **Error** *error* - error object
* **String** *[customMessage]* - a custom message to be used as prefix to the error message

## rejectWithError(reject, error)

Expand Down
13 changes: 10 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,23 @@ module.exports = function(env) {
@param {Function} reject - function to reject a promise on return,
may be null
@param {Error} error - error object
@param {String} [customMessage] - a custom message to be used as prefix to the error message
*/
rejectWithErrorString: function(reject, error) {
rejectWithErrorString: function(reject, error, customMessage) {
var message, ref;
if (error == null) {
error = "Unknown";
}
if (customMessage == null) {
customMessage = null;
}
message = "" + ((ref = error.message) != null ? ref : error);
if (message.match(/^Error:\ /) == null) {
message = "Error: " + message;
}
if (customMessage != null) {
message = customMessage + ": " + message;
}
members.error(message);
if (device.debug === true) {
members.stack(error);
Expand Down Expand Up @@ -283,10 +290,10 @@ module.exports = function(env) {
generateDeviceId: function(framework, prefix, lastId) {
var i, m, matched, ref, result, start, x;
if (lastId == null) {
lastId = '';
lastId = null;
}
start = 1;
if ((lastId != null) && lastId !== '') {
if (lastId != null) {
m = lastId.match(/.*-([0-9]+)$/);
if ((m != null) && m.length === 2) {
start = +m[1] + 1;
Expand Down
10 changes: 7 additions & 3 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ module.exports = (env) ->
@param {Function} reject - function to reject a promise on return,
may be null
@param {Error} error - error object
@param {String} [customMessage] - a custom message to be used as prefix to the error message
###
rejectWithErrorString: (reject, error="Unknown") ->
rejectWithErrorString: (reject, error="Unknown", customMessage=null) ->
message = "" + (error.message ? error)
if not message.match(/^Error:\ /)?
message = "Error: " + message

if customMessage?
message = "#{customMessage}: #{message}"

members.error message
if device.debug is true
members.stack error
Expand Down Expand Up @@ -220,9 +224,9 @@ module.exports = (env) ->
@param {String} [lastId] - the lastId returned by generateDeviceId
@returns {String} the id generated or undefined if id could not be generated
###
generateDeviceId: (framework, prefix, lastId = '') ->
generateDeviceId: (framework, prefix, lastId = null) ->
start = 1
if lastId? and lastId isnt ''
if lastId?
m = lastId.match /.*-([0-9]+)$/
start = +m[1] + 1 if m? and m.length is 2
for x in [start...1000] by 1
Expand Down
15 changes: 15 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,21 @@ describe("Testing the base device functions", function() {
});
});

it("shall prefix error message with customer message (rejectWithErrorString)", function(done) {
var numberOfMessages = fakeEnv.numberOfErrorMessages;
var promise = new Promise(function (resolve, reject) {
fakeDevice.debug = false;
base.rejectWithErrorString(null, { message: "Error: Message" }, "Custom Message");
fakeDevice.debug = true;
expect(fakeEnv.errorMessage[0]).toBe('[test] Custom Message: Error: Message');
expect(fakeEnv.numberOfErrorMessages).toBe(numberOfMessages + 1);
done();
});
promise.catch(function(error) {
expect(true).toBe(false);
});
});

it("shall return the entity name", function() {
expect(base._entityName("X")).toBe("[test#X]");
});
Expand Down

0 comments on commit 8a43b80

Please sign in to comment.