Skip to content

Commit f2fcc3e

Browse files
committed
This fixes a bug #7
1 parent 59004e4 commit f2fcc3e

File tree

5 files changed

+136
-36
lines changed

5 files changed

+136
-36
lines changed

dist/browser/index.html

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ <h1>ExceptionID.js - showcase</h1>
3838

3939
<h3>Code:</h3>
4040
<pre><code class="javascript" id="code">
41+
var EidRuntimeException = Eid.exceptions.EidRuntimeException;
4142
try {
4243
// Example entity objects
4344
var users = [];
@@ -61,9 +62,11 @@ <h3>Code:</h3>
6162
EidPreconditions.checkState(false, '20160111:224215', 'A extra message');
6263
} catch(e) {
6364
// Top level of your application
64-
log.error(e.toString() + ' - ' + e.stack);
65-
// or better save your exceptions!
66-
logGateway.put(e, {eid: e.eid});
65+
log.error(e.stack);
66+
if (e instanceof EidRuntimeException) {
67+
// or better save your exceptions!
68+
logGateway.put(e, {eid: e.eid});
69+
}
6770
}
6871
</code></pre>
6972
<h3>Console:</h3>
@@ -95,7 +98,7 @@ <h3>Console:</h3>
9598
};
9699
var logGateway = {
97100
put: function(ex, extra) {
98-
logentry('INFO', 'LogzGetewayFake.io sucessfully PUT an exception: `' + ex.toString() + '` with extra data of: `' + JSON.stringify(extra) + '`');
101+
logentry('INFO', 'FakeLogz.io PUT an exception: `' + ex.toString() + '` with extra data of: `' + JSON.stringify(extra) + '`');
99102
}
100103
};
101104
var code = document.getElementById('code').textContent;

lib/eid/eid.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,25 +109,28 @@ function MathRandom() {
109109
var LONG_MAX = Math.pow(2, 53) - 1;
110110
var LONG_MIN = -1 * Math.pow(2, 53);
111111
this.nextLong = function() {
112-
return random(LONG_MIN, LONG_MAX);
112+
return this.nextNumber(LONG_MIN, LONG_MAX);
113113
};
114114
this.nextInt = function(max) {
115-
return random(0, max);
115+
return this.nextNumber(0, max);
116116
};
117-
var random = function(min, max) {
117+
this.nextNumber = function(min, max) {
118118
return Math.floor(Math.random() * (max - min + 1)) + min;
119119
};
120120
}
121121

122122
function StdUniqIdGenerator() {
123123
var BASE36 = 36;
124124
var INTEGER_MAX_VALUE = Math.pow(2, 31);
125+
var MIN_6_CHAR_HASH = 60466177; // for '100001' in base64
125126
var generator = new MathRandom();
127+
var padding = generator.nextNumber(MIN_6_CHAR_HASH, INTEGER_MAX_VALUE).toString(BASE36);
126128
this.generateUniqId = function() {
127129
var first = Math.abs(generator.nextLong() + 1);
128130
var second = Math.abs(generator.nextInt(INTEGER_MAX_VALUE));
129131
var calc = first + second;
130-
return (Math.abs(calc) % INTEGER_MAX_VALUE).toString(BASE36);
132+
var hash = (Math.abs(calc) % INTEGER_MAX_VALUE).toString(BASE36);
133+
return String(padding + hash).slice(-6); // pad output string to six chars using random padding
131134
};
132135
}
133136

lib/eid/exceptions.js

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,36 @@
1818
'use strict';
1919

2020
var Eid = require('./eid');
21-
var commonExConstructor = function(eid, message) {
21+
22+
var prepareMessage = function(eid, message) {
23+
return message !== undefined ? (eid + " " + message) : eid;
24+
};
25+
26+
var validateEid = function(eid) {
27+
if (eid === undefined) {
28+
throw new TypeError('You need to provide an valid Eid number to EidRuntimeExceptions, but given undefined');
29+
}
2230
if (!(eid instanceof Eid)) {
2331
eid = new Eid(eid.toString());
2432
}
25-
this.message = message !== undefined ? (eid + " " + message) : eid;
26-
this.eid = eid;
27-
}
33+
return eid;
34+
};
35+
36+
var CorrectStackTrace = {
37+
modern: function(target, constructor) {
38+
Error.captureStackTrace(target, constructor);
39+
},
40+
legancy: function(target, constructor, exception) {
41+
target.stack = (new exception()).stack;
42+
}
43+
};
44+
CorrectStackTrace.legancy({}, null, Object);
45+
46+
var correctStackTrace = function(target, constructor) {
47+
var handler = typeof(Error.captureStackTrace) === 'function' ?
48+
CorrectStackTrace.modern : CorrectStackTrace.legancy;
49+
handler(target, constructor, Error);
50+
};
2851

2952
/**
3053
* <strong>This class shouldn't be used in any public API or library.</strong> It is designed to be used for in-house development
@@ -45,10 +68,18 @@
4568
* @author Krzysztof Suszyński <krzysztof.suszynski@wavesoftware.pl>
4669
*/
4770
function EidRuntimeException(eid, message) {
48-
this.name = 'EidRuntimeException';
49-
commonExConstructor.apply(this, [eid, message]);
71+
eid = validateEid(eid);
72+
message = prepareMessage(eid, message);
73+
var tmp = Error.apply(this, [message]);
74+
tmp.name = this.name = 'EidRuntimeException';
75+
this.message = tmp.message;
76+
correctStackTrace(this, EidRuntimeException);
77+
78+
return this;
5079
}
51-
EidRuntimeException.prototype = new Error();
80+
var IntermediateInheritor = function() {};
81+
IntermediateInheritor.prototype = Error.prototype;
82+
EidRuntimeException.prototype = new IntermediateInheritor();
5283

5384
/**
5485
* <strong>This class shouldn't be used in any public API or library.</strong> It is designed to be used for in-house development
@@ -64,10 +95,14 @@
6495
* @author Krzysztof Suszyński <krzysztof.suszynski@wavesoftware.pl>
6596
*/
6697
function EidNullPointerException(eid, message) {
67-
this.name = 'EidNullPointerException';
68-
commonExConstructor.apply(this, [eid, message]);
98+
var tmp = EidRuntimeException.apply(this, [eid, message]);
99+
tmp.name = this.name = 'EidNullPointerException';
100+
correctStackTrace(this, EidNullPointerException);
101+
return this;
69102
}
70-
EidNullPointerException.prototype = EidRuntimeException.prototype;
103+
EidNullPointerException.prototype = Object.create(EidRuntimeException.prototype, {
104+
constructor: { value: EidNullPointerException }
105+
});
71106

72107
/**
73108
* <strong>This class shouldn't be used in any public API or library.</strong> It is designed to be used for in-house development
@@ -83,10 +118,14 @@
83118
* @author Krzysztof Suszyński <krzysztof.suszynski@wavesoftware.pl>
84119
*/
85120
function EidIllegalArgumentException(eid, message) {
86-
this.name = 'EidIllegalArgumentException';
87-
commonExConstructor.apply(this, [eid, message]);
121+
var tmp = EidRuntimeException.apply(this, [eid, message]);
122+
tmp.name = this.name = 'EidIllegalArgumentException';
123+
correctStackTrace(this, EidIllegalArgumentException);
124+
return this;
88125
}
89-
EidIllegalArgumentException.prototype = EidRuntimeException.prototype;
126+
EidIllegalArgumentException.prototype = Object.create(EidRuntimeException.prototype, {
127+
constructor: { value: EidIllegalArgumentException }
128+
});
90129

91130
/**
92131
* <strong>This class shouldn't be used in any public API or library.</strong> It is designed to be used for in-house development
@@ -102,10 +141,14 @@
102141
* @author Krzysztof Suszyński <krzysztof.suszynski@wavesoftware.pl>
103142
*/
104143
function EidIllegalStateException(eid, message) {
105-
this.name = 'EidIllegalStateException';
106-
commonExConstructor.apply(this, [eid, message]);
144+
var tmp = EidRuntimeException.apply(this, [eid, message]);
145+
tmp.name = this.name = 'EidIllegalStateException';
146+
correctStackTrace(this, EidIllegalStateException);
147+
return this;
107148
}
108-
EidIllegalStateException.prototype = EidRuntimeException.prototype;
149+
EidIllegalStateException.prototype = Object.create(EidRuntimeException.prototype, {
150+
constructor: { value: EidIllegalStateException }
151+
});
109152

110153
/**
111154
* <strong>This class shouldn't be used in any public API or library.</strong> It is designed to be used for in-house development
@@ -121,10 +164,14 @@
121164
* @author Krzysztof Suszyński <krzysztof.suszynski@wavesoftware.pl>
122165
*/
123166
function EidIndexOutOfBoundsException(eid, message) {
124-
this.name = 'EidIndexOutOfBoundsException';
125-
commonExConstructor.apply(this, [eid, message]);
167+
var tmp = EidRuntimeException.apply(this, [eid, message]);
168+
tmp.name = this.name = 'EidIndexOutOfBoundsException';
169+
correctStackTrace(this, EidIndexOutOfBoundsException);
170+
return this;
126171
}
127-
EidIndexOutOfBoundsException.prototype = EidRuntimeException.prototype;
172+
EidIndexOutOfBoundsException.prototype = Object.create(EidRuntimeException.prototype, {
173+
constructor: { value: EidIndexOutOfBoundsException }
174+
});
128175

129176
exports.EidRuntimeException = EidRuntimeException;
130177
exports.EidNullPointerException = EidNullPointerException;

test/eid.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,21 +118,21 @@ describe('Eid', function() {
118118

119119
describe('.DEFAULT_UNIQ_ID_GENERATOR', function() {
120120
it('should be instance of UniqIdGenerator', function() {
121-
expect(Eid.DEFAULT_UNIQ_ID_GENERATOR.generateUniqId()).to.match(/^[a-z0-9]{5,6}$/);
121+
expect(Eid.DEFAULT_UNIQ_ID_GENERATOR.generateUniqId()).to.match(/^[a-z0-9]{6}$/);
122122
});
123123
});
124124

125125
describe('#toString()', function() {
126126
describe('without ref number', function() {
127127
var subject = new Eid('20160110:214452');
128128
it('should print object with eid and uniq id', function() {
129-
expect(subject.toString()).to.match(/^\[20160110:214452]\<[a-z0-9]{5,6}\>$/);
129+
expect(subject.toString()).to.match(/^\[20160110:214452]\<[a-z0-9]{6}\>$/);
130130
});
131131
});
132132
describe('with ref number', function() {
133133
var subject = new Eid('20160110:214944', 'ORA-1029');
134134
it('should print object with eid, ref and uniq id', function() {
135-
expect(subject.toString()).to.match(/^\[20160110:214944\|ORA-1029\]\<[a-z0-9]{5,6}\>$/);
135+
expect(subject.toString()).to.match(/^\[20160110:214944\|ORA-1029\]\<[a-z0-9]{6}\>$/);
136136
});
137137
});
138138
});
@@ -145,7 +145,7 @@ describe('Eid', function() {
145145
return JSON.stringify(this);
146146
};
147147
it('should print log message as: [20160110:215138]<xxxxx> => My test object is: {"a":67}', function() {
148-
expect(eid.makeLogMessage(messageFormat, testObject)).to.match(/^\[20160110:215138\]\<[a-z0-9]{5,6}\> => My test object is: {"a":67}$/);
148+
expect(eid.makeLogMessage(messageFormat, testObject)).to.match(/^\[20160110:215138\]\<[a-z0-9]{6}\> => My test object is: {"a":67}$/);
149149
});
150150
});
151151

@@ -163,7 +163,7 @@ describe('Eid', function() {
163163
});
164164
describe('#getUniq()', function() {
165165
it('should return xxxxxx as uniq id', function() {
166-
expect(eid.getUniq()).to.match(/^[a-z0-9]{5,6}$/);
166+
expect(eid.getUniq()).to.match(/^[a-z0-9]{6}$/);
167167
});
168168
});
169169
});

test/eid/exceptions.js

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ describe('EidRuntimeException', function() {
3232
it('should inherits Error', function() {
3333
expect(ex instanceof Error).to.be.ok();
3434
});
35+
it('should not inherits TypeError or other types of Eid*Exception', function() {
36+
expect(ex instanceof TypeError).not.to.be.ok();
37+
expect(ex instanceof EidIllegalStateException).not.to.be.ok();
38+
expect(ex instanceof EidIllegalArgumentException).not.to.be.ok();
39+
expect(ex instanceof EidIndexOutOfBoundsException).not.to.be.ok();
40+
expect(ex instanceof EidNullPointerException).not.to.be.ok();
41+
});
42+
describe('construction without passing eid object', function() {
43+
it('should fail with TypeError', function() {
44+
expect(function() { new EidRuntimeException() }).to.throwException(function(e) {
45+
expect(e.message).to.eql('You need to provide an valid Eid number to EidRuntimeExceptions, but given undefined');
46+
expect(e).to.be.a(TypeError);
47+
});
48+
});
49+
});
3550
});
3651

3752
describe('EidNullPointerException', function() {
@@ -43,9 +58,16 @@ describe('EidNullPointerException', function() {
4358
expect(ex.stack).to.not.empty();
4459
});
4560
it('should inherits Error and EidRuntimeException', function() {
61+
expect(ex instanceof EidNullPointerException).to.be.ok();
4662
expect(ex instanceof Error).to.be.ok();
4763
expect(ex instanceof EidRuntimeException).to.be.ok();
4864
});
65+
it('should not inherits TypeError or other types of Eid*Exception', function() {
66+
expect(ex instanceof TypeError).not.to.be.ok();
67+
expect(ex instanceof EidIllegalStateException).not.to.be.ok();
68+
expect(ex instanceof EidIllegalArgumentException).not.to.be.ok();
69+
expect(ex instanceof EidIndexOutOfBoundsException).not.to.be.ok();
70+
});
4971
});
5072

5173
describe('EidIllegalArgumentException', function() {
@@ -59,33 +81,58 @@ describe('EidIllegalArgumentException', function() {
5981
it('should inherits Error and EidRuntimeException', function() {
6082
expect(ex instanceof Error).to.be.ok();
6183
expect(ex instanceof EidRuntimeException).to.be.ok();
84+
expect(ex instanceof EidIllegalArgumentException).to.be.ok();
85+
});
86+
it('should not inherits TypeError or other types of Eid*Exception', function() {
87+
expect(ex instanceof TypeError).not.to.be.ok();
88+
expect(ex instanceof EidIllegalStateException).not.to.be.ok();
89+
expect(ex instanceof EidNullPointerException).not.to.be.ok();
90+
expect(ex instanceof EidIndexOutOfBoundsException).not.to.be.ok();
6291
});
6392
});
6493

6594
describe('EidIllegalStateException', function() {
66-
var ex = new EidIllegalStateException('20160110:230112', 'A null pointer occured!');
95+
var ex = new EidIllegalStateException('20160110:230112', 'Illegal state!');
96+
var regex = /EidIllegalStateException: \[20160110:230112\]\<[a-z0-9]+\> Illegal state!/;
6797
it('should contain proper message', function() {
68-
expect(ex.toString()).to.match(/EidIllegalStateException: \[20160110:230112\]\<[a-z0-9]+\> A null pointer occured!/);
98+
expect(ex.toString()).to.match(regex);
6999
});
70100
it('should contain a stack trace', function() {
71101
expect(ex.stack).to.not.empty();
102+
expect(ex.stack).to.match(regex);
72103
});
73104
it('should inherits Error and EidRuntimeException', function() {
74105
expect(ex instanceof Error).to.be.ok();
75106
expect(ex instanceof EidRuntimeException).to.be.ok();
107+
expect(ex instanceof EidIllegalStateException).to.be.ok();
108+
});
109+
it('should not inherits TypeError or other types of Eid*Exception', function() {
110+
expect(ex instanceof TypeError).not.to.be.ok();
111+
expect(ex instanceof EidIllegalArgumentException).not.to.be.ok();
112+
expect(ex instanceof EidNullPointerException).not.to.be.ok();
113+
expect(ex instanceof EidIndexOutOfBoundsException).not.to.be.ok();
76114
});
77115
});
78116

79117
describe('EidIndexOutOfBoundsException', function() {
80-
var ex = new EidIndexOutOfBoundsException('20160110:230140', 'A null pointer occured!');
118+
var ex = new EidIndexOutOfBoundsException('20160110:230140', 'Index is out of bounds!');
119+
var regex = /EidIndexOutOfBoundsException: \[20160110:230140\]\<[a-z0-9]+\> Index is out of bounds!/;
81120
it('should contain proper message', function() {
82-
expect(ex.toString()).to.match(/EidIndexOutOfBoundsException: \[20160110:230140\]\<[a-z0-9]+\> A null pointer occured!/);
121+
expect(ex.toString()).to.match(regex);
83122
});
84123
it('should contain a stack trace', function() {
85124
expect(ex.stack).to.not.empty();
125+
expect(ex.stack).to.match(regex);
86126
});
87127
it('should inherits Error and EidRuntimeException', function() {
88128
expect(ex instanceof Error).to.be.ok();
89129
expect(ex instanceof EidRuntimeException).to.be.ok();
130+
expect(ex instanceof EidIndexOutOfBoundsException).to.be.ok();
131+
});
132+
it('should not inherits TypeError or other types of Eid*Exception', function() {
133+
expect(ex instanceof TypeError).not.to.be.ok();
134+
expect(ex instanceof EidIllegalArgumentException).not.to.be.ok();
135+
expect(ex instanceof EidIllegalStateException).not.to.be.ok();
136+
expect(ex instanceof EidNullPointerException).not.to.be.ok();
90137
});
91138
});

0 commit comments

Comments
 (0)