crypto-crawler / crypto-crawler-rs

A rock-solid cryptocurrency crawler library.
Apache License 2.0
227 stars 73 forks source link

Prevent using `.unwrap()` and `.expect()` in codebase #41

Open pan93412 opened 2 years ago

pan93412 commented 2 years ago

Abstract

crypto-crawler panics when the data format is invalid, which may have better recoverable way to indicate instead of just aborting the whole tasks.

Solution

Replace all of them to Result<T, SimpleError>:

ws_msg.data.B.parse::<f64>().map_err(|_| SimpleError::new("Failed to parse ws_msg.data.B"));

Besides, I recommended replacing SimpleError to anyhow. It allows us wrapping all the error with a generic Error:

fn parse() -> anyhow::Result<T> {
  // Even simpler than `.unwrap()`. Besides, the `parse()` is recoverable now!
  ws_msg.data.B.parse::<f64>()?;

  // If you need context:
  ws_msg.data.B.parse::<f64>().context("Failed to parse ws_msg.data.B")?;
}

TBDs (To Be Discussed)

soulmachine commented 2 years ago

Ideally there should be zero unwrap() in code, but it is very difficult to eliminate all unwrap().

I'll look into this issue and decide next step soon.