|
18 | 18 | 'use strict'; |
19 | 19 |
|
20 | 20 | 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 | + } |
22 | 30 | if (!(eid instanceof Eid)) { |
23 | 31 | eid = new Eid(eid.toString()); |
24 | 32 | } |
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 | + }; |
28 | 51 |
|
29 | 52 | /** |
30 | 53 | * <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 | 68 | * @author Krzysztof Suszyński <krzysztof.suszynski@wavesoftware.pl> |
46 | 69 | */ |
47 | 70 | 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; |
50 | 79 | } |
51 | | - EidRuntimeException.prototype = new Error(); |
| 80 | + var IntermediateInheritor = function() {}; |
| 81 | + IntermediateInheritor.prototype = Error.prototype; |
| 82 | + EidRuntimeException.prototype = new IntermediateInheritor(); |
52 | 83 |
|
53 | 84 | /** |
54 | 85 | * <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 | 95 | * @author Krzysztof Suszyński <krzysztof.suszynski@wavesoftware.pl> |
65 | 96 | */ |
66 | 97 | 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; |
69 | 102 | } |
70 | | - EidNullPointerException.prototype = EidRuntimeException.prototype; |
| 103 | + EidNullPointerException.prototype = Object.create(EidRuntimeException.prototype, { |
| 104 | + constructor: { value: EidNullPointerException } |
| 105 | + }); |
71 | 106 |
|
72 | 107 | /** |
73 | 108 | * <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 | 118 | * @author Krzysztof Suszyński <krzysztof.suszynski@wavesoftware.pl> |
84 | 119 | */ |
85 | 120 | 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; |
88 | 125 | } |
89 | | - EidIllegalArgumentException.prototype = EidRuntimeException.prototype; |
| 126 | + EidIllegalArgumentException.prototype = Object.create(EidRuntimeException.prototype, { |
| 127 | + constructor: { value: EidIllegalArgumentException } |
| 128 | + }); |
90 | 129 |
|
91 | 130 | /** |
92 | 131 | * <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 | 141 | * @author Krzysztof Suszyński <krzysztof.suszynski@wavesoftware.pl> |
103 | 142 | */ |
104 | 143 | 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; |
107 | 148 | } |
108 | | - EidIllegalStateException.prototype = EidRuntimeException.prototype; |
| 149 | + EidIllegalStateException.prototype = Object.create(EidRuntimeException.prototype, { |
| 150 | + constructor: { value: EidIllegalStateException } |
| 151 | + }); |
109 | 152 |
|
110 | 153 | /** |
111 | 154 | * <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 | 164 | * @author Krzysztof Suszyński <krzysztof.suszynski@wavesoftware.pl> |
122 | 165 | */ |
123 | 166 | 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; |
126 | 171 | } |
127 | | - EidIndexOutOfBoundsException.prototype = EidRuntimeException.prototype; |
| 172 | + EidIndexOutOfBoundsException.prototype = Object.create(EidRuntimeException.prototype, { |
| 173 | + constructor: { value: EidIndexOutOfBoundsException } |
| 174 | + }); |
128 | 175 |
|
129 | 176 | exports.EidRuntimeException = EidRuntimeException; |
130 | 177 | exports.EidNullPointerException = EidNullPointerException; |
|
0 commit comments