Skip to content
This repository was archived by the owner on Jun 9, 2019. It is now read-only.

Commit e0a22cd

Browse files
committed
improve error message to enhance readability
fixed: #36
1 parent 852c8b7 commit e0a22cd

32 files changed

+224
-137
lines changed

src/AudioBuffer.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ export default class AudioBuffer {
5656
@methods.contract({
5757
precondition(channel) {
5858
if (this._.data.length <= channel) {
59-
throw new TypeError(`channel index (${channel}) exceeds number of channels (${this._.data.length})`);
59+
throw new TypeError(`The {{channel}} index (${channel}) exceeds number of channels (${this._.data.length}).`);
6060
}
6161
}
6262
})
63+
@methods.returns(validators.isInstanceOf(Float32Array))
6364
getChannelData(channel) {
6465
return this._.data[channel];
6566
}
@@ -69,11 +70,11 @@ export default class AudioBuffer {
6970
@methods.param("[ startInChannel ]", validators.isPositiveInteger)
7071
@methods.contract({
7172
precondition(destination, channelNumber, startInChannel = 0) {
72-
if (channelNumber < 0 || this._.data.length <= channelNumber) {
73-
throw new TypeError(`The channelNumber provided (${channelNumber}) is outside the range [0, ${this._.data.length})`);
73+
if (this._.data.length <= channelNumber) {
74+
throw new TypeError(`The {{channelNumber}} provided (${channelNumber}) is outside the range [0, ${this._.data.length}).`);
7475
}
75-
if (startInChannel < 0 || this._.length <= startInChannel) {
76-
throw new TypeError(`The startInChannel provided (${startInChannel}) is outside the range [0, ${this._.length}).`);
76+
if (this._.length <= startInChannel) {
77+
throw new TypeError(`The {{startInChannel}} provided (${startInChannel}) is outside the range [0, ${this._.length}).`);
7778
}
7879
if (configuration.getState("AudioBuffer#copyFromChannel") !== "enabled") {
7980
throw new TypeError("not enabled");
@@ -91,11 +92,11 @@ export default class AudioBuffer {
9192
@methods.param("[ startInChannel ]", validators.isPositiveInteger)
9293
@methods.contract({
9394
precondition(source, channelNumber, startInChannel = 0) {
94-
if (channelNumber < 0 || this._.data.length <= channelNumber) {
95-
throw new TypeError(`The channelNumber provided (${channelNumber}) is outside the range [0, ${this._.data.length})`);
95+
if (this._.data.length <= channelNumber) {
96+
throw new TypeError(`The {{channelNumber}} provided (${channelNumber}) is outside the range [0, ${this._.data.length}).`);
9697
}
97-
if (startInChannel < 0 || this._.length <= startInChannel) {
98-
throw new TypeError(`The startInChannel provided (${startInChannel}) is outside the range [0, ${this._.length}).`);
98+
if (this._.length <= startInChannel) {
99+
throw new TypeError(`The {{startInChannel}} provided (${startInChannel}) is outside the range [0, ${this._.length}).`);
99100
}
100101
if (configuration.getState("AudioBuffer#copyToChannel") !== "enabled") {
101102
throw new TypeError("not enabled");

src/AudioBufferSourceNode.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default class AudioBufferSourceNode extends AudioNode {
5252
@methods.contract({
5353
precondition() {
5454
if (this._.startTime !== Infinity) {
55-
throw new TypeError("cannot start more than once");
55+
throw new TypeError("Cannot start more than once.");
5656
}
5757
}
5858
})
@@ -66,10 +66,10 @@ export default class AudioBufferSourceNode extends AudioNode {
6666
@methods.contract({
6767
precondition() {
6868
if (this._.startTime === Infinity) {
69-
throw new TypeError("cannot call stop without calling start first");
69+
throw new TypeError("Cannot call stop without calling start first.");
7070
}
7171
if (this._.stopTime !== Infinity) {
72-
throw new TypeError("cannot stop more than once");
72+
throw new TypeError("Cannot stop more than once.");
7373
}
7474
}
7575
})

src/AudioContext.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import Configuration from "./utils/Configuration";
22
import Immigration from "./utils/Immigration";
33
import Event from "./dom/Event";
44
import EventTarget from "./dom/EventTarget";
5+
import HTMLMediaElement from "./dom/HTMLMediaElement";
6+
import MediaStream from "./dom/MediaStream";
57
import AudioBuffer from "./AudioBuffer";
68
import AnalyserNode from "./AnalyserNode";
79
import AudioBufferSourceNode from "./AudioBufferSourceNode";
@@ -98,6 +100,7 @@ export default class AudioContext extends EventTarget {
98100
@props.on("statechange")
99101
onstatechange() {}
100102

103+
@methods.returns(validators.isInstanceOf(Promise))
101104
suspend() {
102105
return this.__transitionToState("suspend", () => {
103106
if (this._.state === "running") {
@@ -107,6 +110,7 @@ export default class AudioContext extends EventTarget {
107110
});
108111
}
109112

113+
@methods.returns(validators.isInstanceOf(Promise))
110114
resume() {
111115
return this.__transitionToState("resume", () => {
112116
if (this._.state === "suspended") {
@@ -116,6 +120,7 @@ export default class AudioContext extends EventTarget {
116120
});
117121
}
118122

123+
@methods.returns(validators.isInstanceOf(Promise))
119124
close() {
120125
return this.__transitionToState("close", () => {
121126
if (this._.state !== "closed") {
@@ -129,6 +134,7 @@ export default class AudioContext extends EventTarget {
129134
@methods.param("numberOfChannels", validators.isPositiveInteger)
130135
@methods.param("length", validators.isPositiveInteger)
131136
@methods.param("sampleRate", validators.isPositiveInteger)
137+
@methods.returns(validators.isInstanceOf(AudioBuffer))
132138
createBuffer(numberOfChannels, length, sampleRate) {
133139
return immigration.apply(admission =>
134140
new AudioBuffer(admission, this, numberOfChannels, length, sampleRate)
@@ -165,24 +171,30 @@ export default class AudioContext extends EventTarget {
165171
}
166172
}
167173

174+
@methods.returns(validators.isInstanceOf(AudioBufferSourceNode))
168175
createBufferSource() {
169176
return immigration.apply(admission =>
170177
new AudioBufferSourceNode(admission, this)
171178
);
172179
}
173180

181+
@methods.param("mediaElement", validators.isInstanceOf(HTMLMediaElement))
182+
@methods.returns(validators.isInstanceOf(MediaElementAudioSourceNode))
174183
createMediaElementSource(mediaElement) {
175184
return immigration.apply(admission =>
176185
new MediaElementAudioSourceNode(admission, this, mediaElement)
177186
);
178187
}
179188

189+
@methods.param("mediaStream", validators.isInstanceOf(MediaStream))
190+
@methods.returns(validators.isInstanceOf(MediaStreamAudioSourceNode))
180191
createMediaStreamSource(mediaStream) {
181192
return immigration.apply(admission =>
182193
new MediaStreamAudioSourceNode(admission, this, mediaStream)
183194
);
184195
}
185196

197+
@methods.returns(validators.isInstanceOf(MediaStreamAudioDestinationNode))
186198
createMediaStreamDestination() {
187199
return immigration.apply(admission =>
188200
new MediaStreamAudioDestinationNode(admission, this)
@@ -199,43 +211,50 @@ export default class AudioContext extends EventTarget {
199211
@methods.param("bufferSize", validators.isPositiveInteger)
200212
@methods.param("[ numberOfInputChannels ]", validators.isPositiveInteger)
201213
@methods.param("[ numberOfOutputChannels ]", validators.isPositiveInteger)
214+
@methods.returns(validators.isInstanceOf(ScriptProcessorNode))
202215
createScriptProcessor(bufferSize, numberOfInputChannels = 2, numberOfOutputChannels = 2) {
203216
return immigration.apply(admission =>
204217
new ScriptProcessorNode(admission, this, bufferSize, numberOfInputChannels, numberOfOutputChannels)
205218
);
206219
}
207220

221+
@methods.returns(validators.isInstanceOf(AnalyserNode))
208222
createAnalyser() {
209223
return immigration.apply(admission =>
210224
new AnalyserNode(admission, this)
211225
);
212226
}
213227

228+
@methods.returns(validators.isInstanceOf(GainNode))
214229
createGain() {
215230
return immigration.apply(admission =>
216231
new GainNode(admission, this)
217232
);
218233
}
219234

220235
@methods.param("[ maxDelayTime ]", validators.isPositiveNumber)
236+
@methods.returns(validators.isInstanceOf(DelayNode))
221237
createDelay(maxDelayTime = 1) {
222238
return immigration.apply(admission =>
223239
new DelayNode(admission, this, maxDelayTime)
224240
);
225241
}
226242

243+
@methods.returns(validators.isInstanceOf(BiquadFilterNode))
227244
createBiquadFilter() {
228245
return immigration.apply(admission =>
229246
new BiquadFilterNode(admission, this)
230247
);
231248
}
232249

250+
@methods.returns(validators.isInstanceOf(WaveShaperNode))
233251
createWaveShaper() {
234252
return immigration.apply(admission =>
235253
new WaveShaperNode(admission, this)
236254
);
237255
}
238256

257+
@methods.returns(validators.isInstanceOf(PannerNode))
239258
createPanner() {
240259
return immigration.apply(admission =>
241260
new PannerNode(admission, this)
@@ -249,38 +268,44 @@ export default class AudioContext extends EventTarget {
249268
}
250269
}
251270
})
271+
@methods.returns(validators.isInstanceOf(StereoPannerNode))
252272
createStereoPanner() {
253273
return immigration.apply(admission =>
254274
new StereoPannerNode(admission, this)
255275
);
256276
}
257277

278+
@methods.returns(validators.isInstanceOf(ConvolverNode))
258279
createConvolver() {
259280
return immigration.apply(admission =>
260281
new ConvolverNode(admission, this)
261282
);
262283
}
263284

264285
@methods.param("[ numberOfOutputs ]", validators.isPositiveInteger)
286+
@methods.returns(validators.isInstanceOf(ChannelSplitterNode))
265287
createChannelSplitter(numberOfOutputs = 6) {
266288
return immigration.apply(admission =>
267289
new ChannelSplitterNode(admission, this, numberOfOutputs)
268290
);
269291
}
270292

271293
@methods.param("[ numberOfInputs ]", validators.isPositiveInteger)
294+
@methods.returns(validators.isInstanceOf(ChannelMergerNode))
272295
createChannelMerger(numberOfInputs = 6) {
273296
return immigration.apply(admission =>
274297
new ChannelMergerNode(admission, this, numberOfInputs)
275298
);
276299
}
277300

301+
@methods.returns(validators.isInstanceOf(DynamicsCompressorNode))
278302
createDynamicsCompressor() {
279303
return immigration.apply(admission =>
280304
new DynamicsCompressorNode(admission, this)
281305
);
282306
}
283307

308+
@methods.returns(validators.isInstanceOf(OscillatorNode))
284309
createOscillator() {
285310
return immigration.apply(admission =>
286311
new OscillatorNode(admission, this)
@@ -289,6 +314,7 @@ export default class AudioContext extends EventTarget {
289314

290315
@methods.param("real", validators.isInstanceOf(Float32Array))
291316
@methods.param("imag", validators.isInstanceOf(Float32Array))
317+
@methods.returns(validators.isInstanceOf(PeriodicWave))
292318
createPeriodicWave(real, imag) {
293319
return immigration.apply(admission =>
294320
new PeriodicWave(admission, this, real, imag)
@@ -305,7 +331,7 @@ export default class AudioContext extends EventTarget {
305331
__transitionToState(methodName, callback) {
306332
return new Promise((resolve) => {
307333
if (this._.state === "close") {
308-
throw new TypeError(`Cannot ${methodName} a context that is being closed or has already been closed`);
334+
throw new TypeError(`Cannot ${methodName} a context that is being closed or has already been closed.`);
309335
}
310336
callback();
311337
resolve();

src/AudioNode.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ export default class AudioNode extends EventTarget {
7676
@methods.contract({
7777
precondition(destination, output = 0, input = 0) {
7878
if (this.$context !== destination.$context) {
79-
throw new TypeError(`cannot connect to a destination belonging to a different audio context`);
79+
throw new TypeError("Cannot connect to a destination belonging to a different AudioContext.");
8080
}
8181
if (this.numberOfOutputs <= output) {
82-
throw new TypeError(`output index (${output}) exceeds number of outputs (${this.numberOfOutputs})`);
82+
throw new TypeError(`The {{output}} index (${output}) exceeds number of outputs (${this.numberOfOutputs}).`);
8383
}
8484
if ((destination.numberOfInputs || 1) <= input) {
85-
throw new TypeError(`input index (${input}) exceeds number of inputs (${destination.numberOfInputs})`);
85+
throw new TypeError(`The {{input}} index (${input}) exceeds number of inputs (${destination.numberOfInputs}).`);
8686
}
8787
}
8888
})
@@ -122,7 +122,7 @@ export default class AudioNode extends EventTarget {
122122
@methods.contract({
123123
precondition(output) {
124124
if (this.numberOfOutputs <= output) {
125-
throw new TypeError(`output index (${output}) exceeds number of outputs (${this.numberOfOutputs})`);
125+
throw new TypeError(`The {{output}} index (${output}) exceeds number of outputs (${this.numberOfOutputs}).`);
126126
}
127127
}
128128
})
@@ -134,7 +134,7 @@ export default class AudioNode extends EventTarget {
134134
@methods.contract({
135135
precondition(destination) {
136136
if (!this._.outputs.some(junction => junction.isConnected(destination))) {
137-
throw new TypeError("the given destination is not connected");
137+
throw new TypeError("The given {{destination}} is not connected.");
138138
}
139139
}
140140
})
@@ -149,10 +149,10 @@ export default class AudioNode extends EventTarget {
149149
@methods.contract({
150150
precondition(destination, output) {
151151
if (!this._.outputs.some(junction => junction.isConnected(destination))) {
152-
throw new TypeError("the given destination is not connected");
152+
throw new TypeError("The given {{destination}} is not connected.");
153153
}
154-
if (output < 0 || this.numberOfOutputs <= output) {
155-
throw new TypeError(`output provided (${output}) is outside the range [0, ${this.numberOfOutputs})`);
154+
if (this.numberOfOutputs <= output) {
155+
throw new TypeError(`The {{output}} provided (${output}) is outside the range [0, ${this.numberOfOutputs}).`);
156156
}
157157
}
158158
})
@@ -166,13 +166,13 @@ export default class AudioNode extends EventTarget {
166166
@methods.contract({
167167
precondition(destination, output, input) {
168168
if (!this._.outputs.some(junction => junction.isConnected(destination))) {
169-
throw new TypeError("the given destination is not connected");
169+
throw new TypeError("The given {{destination}} is not connected.");
170170
}
171171
if (output < 0 || this.numberOfOutputs <= output) {
172-
throw new TypeError(`output provided (${output}) is outside the range [0, ${this.numberOfOutputs})`);
172+
throw new TypeError(`The {{output}} provided (${output}) is outside the range [0, ${this.numberOfOutputs}).`);
173173
}
174174
if (input < 0 || destination.numberOfInputs <= input) {
175-
throw new TypeError(`input provided (${input}) is outside the range [0, ${this.numberOfInputs})`);
175+
throw new TypeError(`The {{input}} provided (${input}) is outside the range [0, ${this.numberOfInputs}).`);
176176
}
177177
}
178178
})

src/OfflineAudioContext.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default class OfflineAudioContext extends AudioContext {
4747
@methods.contract({
4848
precondition() {
4949
if (this._.rendering) {
50-
throw new TypeError("cannot call startRendering more than once");
50+
throw new TypeError("Cannot call startRendering more than once.");
5151
}
5252
}
5353
})
@@ -77,7 +77,7 @@ export default class OfflineAudioContext extends AudioContext {
7777
})
7878
__transitionToState(methodName) {
7979
return new Promise(() => {
80-
throw new TypeError(`Cannot ${methodName} on an OfflineAudioContext`);
80+
throw new TypeError(`Cannot ${methodName} on an OfflineAudioContext.`);
8181
});
8282
}
8383

src/OscillatorNode.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default class OscillatorNode extends AudioNode {
4141
@methods.contract({
4242
precondition() {
4343
if (this._.startTime !== Infinity) {
44-
throw new Error(`cannot start more than once`);
44+
throw new Error("Cannot start more than once.");
4545
}
4646
}
4747
})
@@ -53,10 +53,10 @@ export default class OscillatorNode extends AudioNode {
5353
@methods.contract({
5454
precondition() {
5555
if (this._.startTime === Infinity) {
56-
throw new Error(`cannot call stop without calling start first`);
56+
throw new Error("Cannot call stop without calling start first.");
5757
}
5858
if (this._.stopTime !== Infinity) {
59-
throw new Error(`cannot stop more than once`);
59+
throw new Error("Cannot stop more than once.");
6060
}
6161
}
6262
})

src/PeriodicWave.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ export default class PeriodicWave {
2020
@methods.contract({
2121
precondition(real, imag) {
2222
if (4096 < real.length) {
23-
throw new TypeError(`length of "real" array (${real.length}) exceeds allow maximum of 4096`);
23+
throw new TypeError(`The length of "{{real}}" array (${real.length}) exceeds allow maximum of 4096.`);
2424
}
2525
if (4096 < imag.length) {
26-
throw new TypeError(`length of "imag" array (${imag.length}) exceeds allow maximum of 4096`);
26+
throw new TypeError(`The length of "{{imag}}" array (${imag.length}) exceeds allow maximum of 4096.`);
2727
}
2828
if (real.length !== imag.length) {
29-
throw new TypeError(`length of real array (${real.length}) and length of imaginary array (${imag.length}) must match`);
29+
throw new TypeError(`The length of "{{real}}" array (${real.length}) and length of "imag" array (${imag.length}) must match.`);
3030
}
3131
}
3232
})

src/ScriptProcessorNode.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default class ScriptProcessorNode extends AudioNode {
3030
@methods.contract({
3131
precondition(bufferSize) {
3232
if ([ 256, 512, 1024, 2048, 4096, 8192, 16384 ].indexOf(bufferSize) === -1) {
33-
throw new TypeError(`bufferSize should be an enum [ 256, 512, 1024, 2048, 4096, 8192, 16384 ], but got ${bufferSize}`);
33+
throw new TypeError(`The {{bufferSize}} should be one of [ 256, 512, 1024, 2048, 4096, 8192, 16384 ], but got ${bufferSize}.`);
3434
}
3535
}
3636
})

0 commit comments

Comments
 (0)