Skip to content

Commit dd3b69c

Browse files
committed
Do not use panic!s and assert!s for erroring
Fixes #29 Signed-off-by: Šimon Brandner <[email protected]>
1 parent 6217312 commit dd3b69c

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

crates/oblichey-cli/src/camera/mod.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use v4l::{Device, FourCC};
1818

1919
/// Some (at least mine) IR cameras occasionally produce very dark frames which we ignore
2020
const MAX_BRIGHTNESS_DECREASE: f32 = 24.0;
21-
/// How many times we are allowed to fail getting a frame before we panic
21+
/// How many times we are allowed to fail getting a frame before we return an error
2222
const MAX_FAILED_FRAMES_IN_ROW: u8 = 10;
2323

2424
/// The type of a frame coming from the camera
@@ -79,7 +79,7 @@ pub struct Camera {
7979
impl Camera {
8080
/// Creates a new Camera which can be used to get frames from the given device.
8181
///
82-
/// This is going to panic if a supported output pixel format cannot be found
82+
/// This is going to return an error if a supported output pixel format cannot be found
8383
pub fn new(camera_path: &str) -> Result<Self, Error> {
8484
trace!("Creating Camera");
8585

@@ -120,17 +120,20 @@ impl Camera {
120120
}
121121

122122
/// Starts the camera loop
123-
pub fn start(frame: &Arc<Mutex<Option<Frame>>>, finished: &Arc<AtomicBool>, camera_path: &str) {
123+
pub fn start(
124+
frame: &Arc<Mutex<Option<Frame>>>,
125+
finished: &Arc<AtomicBool>,
126+
camera_path: &str,
127+
) -> Result<(), String> {
124128
let mut camera = match Camera::new(camera_path) {
125129
Ok(c) => c,
126-
Err(e) => panic!("Failed construct camera: {e}"),
130+
Err(e) => return Err(format!("Failed to create camera: {e}")),
127131
};
128-
129132
let mut failed_frames_in_row = 0;
130133
let mut last_brightness = 255.0;
131134
loop {
132135
if finished.load(Ordering::SeqCst) {
133-
return;
136+
return Ok(());
134137
}
135138

136139
let new_frame = match camera.get_frame() {
@@ -139,10 +142,9 @@ pub fn start(frame: &Arc<Mutex<Option<Frame>>>, finished: &Arc<AtomicBool>, came
139142
error!("Failed to get frame: {e}");
140143

141144
failed_frames_in_row += 1;
142-
assert!(
143-
failed_frames_in_row < MAX_FAILED_FRAMES_IN_ROW,
144-
"Failed to get {MAX_FAILED_FRAMES_IN_ROW} frames in row: {e}"
145-
);
145+
if failed_frames_in_row < MAX_FAILED_FRAMES_IN_ROW {
146+
return Err(String::from("Failed to get too many frames in a row"));
147+
}
146148
continue;
147149
}
148150
};

crates/oblichey-cli/src/gui/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub fn start(
5555
frame: Arc<Mutex<Option<Frame>>>,
5656
faces: Arc<Mutex<Vec<FaceForGUI>>>,
5757
finished: Arc<AtomicBool>,
58-
) {
58+
) -> Result<(), String> {
5959
trace!("Creating GUI");
6060

6161
let event_loop_builder: Option<EventLoopBuilderHook> = Some(Box::new(|event_loop_builder| {
@@ -79,8 +79,10 @@ pub fn start(
7979
},
8080
Box::new(|_| Ok(Box::new(Gui::new(frame, faces, finished)))),
8181
) {
82-
panic!("Running eframe failed: {e}");
82+
return Err(format!("Running eframe failed: {e}"));
8383
};
84+
85+
Ok(())
8486
}
8587

8688
struct Gui {

crates/oblichey-cli/src/main.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ fn start_threads(
201201
let finished_clone = finished.clone();
202202
let camera_path_clone = config.camera.path.clone();
203203
thread_handles.push(thread::spawn(move || {
204-
camera::start(&frame_clone, &finished_clone, &camera_path_clone);
204+
camera::start(&frame_clone, &finished_clone, &camera_path_clone)
205205
}));
206206

207207
let faces_for_gui_clone = faces_for_gui.clone();
@@ -213,13 +213,13 @@ fn start_threads(
213213
&faces_for_gui_clone,
214214
&finished_clone,
215215
&face_processor,
216-
);
216+
)
217217
}));
218218

219219
if gui {
220220
let finished_clone = finished.clone();
221221
thread_handles.push(thread::spawn(move || {
222-
gui::start(frame, faces_for_gui, finished_clone);
222+
gui::start(frame, faces_for_gui, finished_clone)
223223
}));
224224
}
225225

@@ -236,11 +236,17 @@ fn start_threads(
236236

237237
// Join all threads and print any errors
238238
for thread_handle in thread_handles {
239-
if let Err(e) = thread_handle.join() {
240-
if let Some(panic_msg) = e.downcast_ref::<String>() {
241-
println!("{panic_msg}");
242-
} else {
243-
println!("Thread panicked but with an unknown error");
239+
match thread_handle.join() {
240+
Ok(Ok(())) => (),
241+
Ok(Err(e)) => {
242+
log_and_print_error!("Thread returned an error: {e}");
243+
}
244+
Err(e) => {
245+
if let Some(panic_msg) = e.downcast_ref::<String>() {
246+
log_and_print_error!("Thread panicked: {panic_msg}");
247+
} else {
248+
log_and_print_error!("Thread panicked but with an unknown error");
249+
}
244250
}
245251
}
246252
}

crates/oblichey-cli/src/processors/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ pub fn start(
1919
faces_for_gui: &Arc<Mutex<Vec<FaceForGUI>>>,
2020
finished: &Arc<AtomicBool>,
2121
face_processor: &Arc<Mutex<dyn FaceProcessor + Send + Sync>>,
22-
) {
22+
) -> Result<(), String> {
2323
let frame_processor = FrameProcessor::new();
2424

2525
loop {
2626
if finished.load(Ordering::SeqCst) {
27-
return;
27+
return Ok(());
2828
}
2929

3030
let frame_lock = match frame.lock() {
@@ -44,7 +44,7 @@ pub fn start(
4444
};
4545
let new_faces_for_gui = face_processor_lock.process_faces(faces_for_processing);
4646
if face_processor_lock.is_finished() {
47-
return;
47+
return Ok(());
4848
}
4949
drop(face_processor_lock);
5050

0 commit comments

Comments
 (0)