Skip to content

Commit

Permalink
TRY-44 Fancy error handling (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabus1184 committed Oct 26, 2023
1 parent 09be8ed commit 689376d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.75"
cargo-husky = "1.5.0"
chrono = { version = "0.4.31", default-features = false, features = [
"clock",
Expand Down
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,39 @@

This is the main repository for the raytracing project.

## `log` crate
## `log`

https://crates.io/crates/log

> Usage:
> The basic use of the log crate is through the five logging macros: `error!`, `warn!`, `info!`, `debug!` and `trace!` where `error!` represents the highest-priority log messages and trace! the lowest. The log messages are filtered by configuring the log level to exclude messages with a lower priority. Each of these macros accept format strings similarly to println!.
## `anyhow`

https://crates.io/crates/anyhow

> Usage:
> This library provides anyhow::Error, a trait object based error type for easy idiomatic error handling in Rust applications.
Example:
```rust
use anyhow::Context;

fn test() -> anyhow::Result<()> {
// watch out for the question mark operator

let file = std::fs::File::create("test.txt").context("Failed to create test file")?;

file.set_len(1234).context("Failed to set file length")?;

Ok(())
}
```

If this fails the result is a fancy error message:
```
Error: Failed to create test file
Caused by:
File exists (os error 17)
```
23 changes: 13 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::borrow::Cow;

use anyhow::Context;
use glium::{
glutin::{
dpi::PhysicalSize,
Expand All @@ -20,12 +21,8 @@ mod scene;

pub type Color = Vector3<f32>;

pub fn main() {
std::fs::create_dir_all("logs").expect("Failed to create logs directory");
let log_file_name = format!(
"logs/trayracer_{}.log",
chrono::Local::now().format("%Y-%m-%d_%H-%M-%S")
);
pub fn main() -> anyhow::Result<()> {
std::fs::create_dir_all("logs").context("Failed to create logs directory")?;

let log_level = if cfg!(debug_assertions) {
LevelFilter::Trace
Expand All @@ -42,10 +39,14 @@ pub fn main() {
WriteLogger::new(
log_level,
Config::default(),
File::create(log_file_name).unwrap(),
File::create(format!(
"logs/trayracer_{}.log",
chrono::Local::now().format("%Y-%m-%d_%H-%M-%S")
))
.context("Failed to create log file")?,
),
])
.expect("Failed to initialize logger");
.context("Failed to initialize logger")?;

let window_builder = WindowBuilder::new()
.with_title("TrayRacer!")
Expand All @@ -54,7 +55,8 @@ pub fn main() {
let context_builder = ContextBuilder::new();
let event_loop = EventLoop::new();

let display = glium::Display::new(window_builder, context_builder, &event_loop).unwrap();
let display = glium::Display::new(window_builder, context_builder, &event_loop)
.context("Failed to create display")?;

event_loop.run(move |e, _, c| match e {
Event::WindowEvent {
Expand Down Expand Up @@ -86,6 +88,7 @@ pub fn main() {
UncompressedFloatFormat::F32F32F32F32,
MipmapsOption::NoMipmap,
)
.context("Failed to create texture")
.unwrap();

let mut frame = display.draw();
Expand All @@ -106,7 +109,7 @@ pub fn main() {
MagnifySamplerFilter::Linear,
);

frame.finish().unwrap();
frame.finish().context("Failed to finish frame").unwrap();
}
Event::WindowEvent { .. } => {}
Event::RedrawRequested(_) => {}
Expand Down

0 comments on commit 689376d

Please sign in to comment.