Skip to content

Commit c740772

Browse files
committed
Unify user facing panic messages, rewrite asserts
1 parent 07c6d0f commit c740772

21 files changed

+283
-253
lines changed

src/analysis.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,41 +34,39 @@ const MAX_FFT_SIZE: usize = 32768;
3434
// [spec] This MUST be a power of two in the range 32 to 32768, otherwise an
3535
// IndexSizeError exception MUST be thrown.
3636
fn assert_valid_fft_size(fft_size: usize) {
37-
if !fft_size.is_power_of_two() {
38-
panic!(
39-
"IndexSizeError - Invalid fft size: {:?} is not a power of two",
40-
fft_size
41-
);
42-
}
43-
44-
if !(MIN_FFT_SIZE..=MAX_FFT_SIZE).contains(&fft_size) {
45-
panic!(
46-
"IndexSizeError - Invalid fft size: {:?} is outside range [{:?}, {:?}]",
47-
fft_size, MIN_FFT_SIZE, MAX_FFT_SIZE
48-
);
49-
}
37+
assert!(
38+
fft_size.is_power_of_two(),
39+
"IndexSizeError - Invalid fft size: {:?} is not a power of two",
40+
fft_size
41+
);
42+
43+
assert!(
44+
(MIN_FFT_SIZE..=MAX_FFT_SIZE).contains(&fft_size),
45+
"IndexSizeError - Invalid fft size: {:?} is outside range [{:?}, {:?}]",
46+
fft_size,
47+
MIN_FFT_SIZE,
48+
MAX_FFT_SIZE
49+
);
5050
}
5151

5252
// [spec] If the value of this attribute is set to a value less than 0 or more
5353
// than 1, an IndexSizeError exception MUST be thrown.
5454
fn assert_valid_smoothing_time_constant(smoothing_time_constant: f64) {
55-
if !(0. ..=1.).contains(&smoothing_time_constant) {
56-
panic!(
57-
"IndexSizeError - Invalid smoothing time constant: {:?} is outside range [0, 1]",
58-
smoothing_time_constant
59-
);
60-
}
55+
assert!(
56+
(0. ..=1.).contains(&smoothing_time_constant),
57+
"IndexSizeError - Invalid smoothing time constant: {:?} is outside range [0, 1]",
58+
smoothing_time_constant
59+
);
6160
}
6261

6362
// [spec] If the value of minDecibels is set to a value more than or equal to maxDecibels, an
6463
// IndexSizeError exception MUST be thrown.
6564
fn assert_valid_decibels(min_decibels: f64, max_decibels: f64) {
66-
if min_decibels >= max_decibels {
67-
panic!(
68-
"IndexSizeError - Invalid min decibels: {:?} is greater than or equals to max decibels {:?}",
69-
min_decibels, max_decibels
70-
);
71-
}
65+
assert!(
66+
min_decibels < max_decibels,
67+
"IndexSizeError - Invalid min decibels: {:?} is greater than or equals to max decibels {:?}",
68+
min_decibels, max_decibels
69+
);
7270
}
7371

7472
// as the queue is composed of AtomicF32 having only 1 render quantum of extra

src/context/offline.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,12 @@ impl OfflineAudioContext {
125125
/// Panics if this method is called multiple times
126126
#[must_use]
127127
pub fn start_rendering_sync(&mut self) -> AudioBuffer {
128-
let renderer = match self.renderer.lock().unwrap().take() {
129-
None => panic!("InvalidStateError: Cannot call `startRendering` twice"),
130-
Some(v) => v,
131-
};
128+
let renderer = self
129+
.renderer
130+
.lock()
131+
.unwrap()
132+
.take()
133+
.expect("InvalidStateError - Cannot call `startRendering` twice");
132134
let OfflineAudioContextRenderer {
133135
renderer,
134136
suspend_callbacks,
@@ -155,10 +157,12 @@ impl OfflineAudioContext {
155157
/// Panics if this method is called multiple times.
156158
pub async fn start_rendering(&self) -> AudioBuffer {
157159
// We are mixing async with a std Mutex, so be sure not to `await` while the lock is held
158-
let renderer = match self.renderer.lock().unwrap().take() {
159-
None => panic!("InvalidStateError: Cannot call `startRendering` twice"),
160-
Some(v) => v,
161-
};
160+
let renderer = self
161+
.renderer
162+
.lock()
163+
.unwrap()
164+
.take()
165+
.expect("InvalidStateError - Cannot call `startRendering` twice");
162166

163167
let OfflineAudioContextRenderer {
164168
renderer,
@@ -248,18 +252,15 @@ impl OfflineAudioContext {
248252
// We are mixing async with a std Mutex, so be sure not to `await` while the lock is held
249253
{
250254
let mut lock = self.renderer.lock().unwrap();
251-
let renderer = match lock.as_mut() {
252-
Some(r) => r,
253-
None => {
254-
panic!("InvalidStateError: cannot suspend when rendering has already started")
255-
}
256-
};
255+
let renderer = lock
256+
.as_mut()
257+
.expect("InvalidStateError - cannot suspend when rendering has already started");
257258

258259
let insert_pos = renderer
259260
.suspend_promises
260261
.binary_search_by_key(&quantum, |&(q, _)| q)
261262
.expect_err(
262-
"InvalidStateError: cannot suspend multiple times at the same render quantum",
263+
"InvalidStateError - cannot suspend multiple times at the same render quantum",
263264
);
264265

265266
renderer
@@ -316,16 +317,15 @@ impl OfflineAudioContext {
316317
let quantum = self.calculate_suspend_frame(suspend_time);
317318

318319
let mut lock = self.renderer.lock().unwrap();
319-
let renderer = match lock.as_mut() {
320-
Some(r) => r,
321-
None => panic!("InvalidStateError: cannot suspend when rendering has already started"),
322-
};
320+
let renderer = lock
321+
.as_mut()
322+
.expect("InvalidStateError - cannot suspend when rendering has already started");
323323

324324
let insert_pos = renderer
325325
.suspend_callbacks
326326
.binary_search_by_key(&quantum, |(q, _c)| *q)
327327
.expect_err(
328-
"InvalidStateError: cannot suspend multiple times at the same render quantum",
328+
"InvalidStateError - cannot suspend multiple times at the same render quantum",
329329
);
330330

331331
let boxed_callback = Box::new(|ctx: &mut OfflineAudioContext| {

src/decoding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl<R: Read> Read for MediaInput<R> {
3434

3535
impl<R> Seek for MediaInput<R> {
3636
fn seek(&mut self, _pos: SeekFrom) -> std::io::Result<u64> {
37-
panic!("MediaInput does not support seeking")
37+
panic!("NotSupportedError - MediaInput does not support seeking")
3838
}
3939
}
4040

src/io/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,11 @@ fn buffer_size_for_latency_category(
204204
#[allow(clippy::cast_sign_loss)]
205205
#[allow(clippy::cast_possible_truncation)]
206206
AudioContextLatencyCategory::Custom(latency) => {
207-
if latency <= 0. {
208-
panic!(
209-
"RangeError - Invalid custom latency: {:?}, should be strictly positive",
210-
latency
211-
);
212-
}
207+
assert!(
208+
latency > 0.,
209+
"RangeError - Invalid custom latency: {:?}, should be strictly positive",
210+
latency
211+
);
213212

214213
let buffer_size = (latency * sample_rate as f64) as usize;
215214
buffer_size.next_power_of_two()

src/lib.rs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,11 @@ impl AtomicF64 {
158158
pub(crate) fn assert_valid_sample_rate(sample_rate: f32) {
159159
// 1000 Hertz is a just a random cutoff, but it helps a if someone accidentally puts a
160160
// timestamp in the sample_rate variable
161-
if sample_rate <= 1000. {
162-
panic!(
163-
"NotSupportedError - Invalid sample rate: {:?}, should be greater than 1000",
164-
sample_rate
165-
);
166-
}
161+
assert!(
162+
sample_rate > 1000.,
163+
"NotSupportedError - Invalid sample rate: {:?}, should be greater than 1000",
164+
sample_rate
165+
);
167166
}
168167

169168
/// Assert that the given number of channels is valid.
@@ -177,12 +176,12 @@ pub(crate) fn assert_valid_sample_rate(sample_rate: f32) {
177176
#[track_caller]
178177
#[inline(always)]
179178
pub(crate) fn assert_valid_number_of_channels(number_of_channels: usize) {
180-
if number_of_channels == 0 || number_of_channels > MAX_CHANNELS {
181-
panic!(
182-
"NotSupportedError - Invalid number of channels: {:?} is outside range [1, {:?}]",
183-
number_of_channels, MAX_CHANNELS
184-
);
185-
}
179+
assert!(
180+
number_of_channels > 0 && number_of_channels <= MAX_CHANNELS,
181+
"NotSupportedError - Invalid number of channels: {:?} is outside range [1, {:?}]",
182+
number_of_channels,
183+
MAX_CHANNELS
184+
);
186185
}
187186

188187
/// Assert that the given channel number is valid according to the number of channels
@@ -196,12 +195,12 @@ pub(crate) fn assert_valid_number_of_channels(number_of_channels: usize) {
196195
#[track_caller]
197196
#[inline(always)]
198197
pub(crate) fn assert_valid_channel_number(channel_number: usize, number_of_channels: usize) {
199-
if channel_number >= number_of_channels {
200-
panic!(
201-
"IndexSizeError - Invalid channel number {:?} (number of channels: {:?})",
202-
channel_number, number_of_channels
203-
);
204-
}
198+
assert!(
199+
channel_number < number_of_channels,
200+
"IndexSizeError - Invalid channel number {:?} (number of channels: {:?})",
201+
channel_number,
202+
number_of_channels
203+
);
205204
}
206205

207206
/// Assert that the given value number is a valid time information, i.e. greater
@@ -215,16 +214,16 @@ pub(crate) fn assert_valid_channel_number(channel_number: usize, number_of_chann
215214
#[track_caller]
216215
#[inline(always)]
217216
pub(crate) fn assert_valid_time_value(value: f64) {
218-
if !value.is_finite() {
219-
panic!("TypeError - The provided time value is non-finite.");
220-
}
221-
222-
if value < 0. {
223-
panic!(
224-
"RangeError - The provided time value ({:?}) cannot be negative",
225-
value
226-
);
227-
}
217+
assert!(
218+
value.is_finite(),
219+
"TypeError - The provided time value is non-finite.",
220+
);
221+
222+
assert!(
223+
value >= 0.,
224+
"RangeError - The provided time value ({:?}) cannot be negative",
225+
value
226+
);
228227
}
229228

230229
pub(crate) trait AudioBufferIter: Iterator<Item = FallibleBuffer> + Send + 'static {}

src/media_recorder/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,11 @@ impl MediaRecorder {
204204
///
205205
/// Will panic when the recorder has already started
206206
pub fn start(&self) {
207-
if self.inner.active.swap(true, Ordering::Relaxed) {
208-
panic!("InvalidStateError: recorder has already started")
209-
}
207+
let prev_active = self.inner.active.swap(true, Ordering::Relaxed);
208+
assert!(
209+
!prev_active,
210+
"InvalidStateError - recorder has already started"
211+
);
210212

211213
let inner = Arc::clone(&self.inner);
212214
let blob = Vec::with_capacity(128 * 1024);

src/node/audio_buffer_source.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,10 @@ impl AudioBufferSourceNode {
280280
pub fn set_buffer(&mut self, audio_buffer: AudioBuffer) {
281281
let clone = audio_buffer.clone();
282282

283-
if self.buffer.is_some() {
284-
panic!("InvalidStateError - cannot assign buffer twice");
285-
}
283+
assert!(
284+
self.buffer.is_none(),
285+
"InvalidStateError - cannot assign buffer twice",
286+
);
286287
self.buffer = Some(audio_buffer);
287288

288289
self.registration.post_message(clone);

src/node/biquad_filter.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,10 @@ impl BiquadFilterNode {
474474
mag_response: &mut [f32],
475475
phase_response: &mut [f32],
476476
) {
477-
if frequency_hz.len() != mag_response.len() || mag_response.len() != phase_response.len() {
478-
panic!("InvalidAccessError - Parameter lengths must match");
479-
}
477+
assert!(
478+
frequency_hz.len() == mag_response.len() && mag_response.len() == phase_response.len(),
479+
"InvalidAccessError - Parameter lengths must match",
480+
);
480481

481482
let sample_rate = self.context().sample_rate();
482483
let n_quist = sample_rate / 2.;

src/node/channel_merger.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ impl AudioNode for ChannelMergerNode {
4646
}
4747

4848
fn set_channel_count(&self, _v: usize) {
49-
panic!("InvalidStateError: Cannot edit channel count of ChannelMergerNode")
49+
panic!("InvalidStateError - Cannot edit channel count of ChannelMergerNode")
5050
}
5151

5252
fn set_channel_count_mode(&self, _v: ChannelCountMode) {
53-
panic!("InvalidStateError: Cannot edit channel count mode of ChannelMergerNode")
53+
panic!("InvalidStateError - Cannot edit channel count mode of ChannelMergerNode")
5454
}
5555

5656
fn number_of_inputs(&self) -> usize {

src/node/channel_splitter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ impl AudioNode for ChannelSplitterNode {
4646
}
4747

4848
fn set_channel_count(&self, _v: usize) {
49-
panic!("InvalidStateError: Cannot edit channel count of ChannelSplitterNode")
49+
panic!("InvalidStateError - Cannot edit channel count of ChannelSplitterNode")
5050
}
5151

5252
fn set_channel_count_mode(&self, _v: ChannelCountMode) {
53-
panic!("InvalidStateError: Cannot edit channel count mode of ChannelSplitterNode")
53+
panic!("InvalidStateError - Cannot edit channel count mode of ChannelSplitterNode")
5454
}
5555

5656
fn set_channel_interpretation(&self, _v: ChannelInterpretation) {
57-
panic!("InvalidStateError: Cannot edit channel interpretation of ChannelSplitterNode")
57+
panic!("InvalidStateError - Cannot edit channel interpretation of ChannelSplitterNode")
5858
}
5959

6060
fn number_of_inputs(&self) -> usize {

0 commit comments

Comments
 (0)