Skip to content

Commit 0a2a521

Browse files
committed
chore: Remove unwrap() from non-test code
1 parent 0d9d69b commit 0a2a521

File tree

6 files changed

+51
-19
lines changed

6 files changed

+51
-19
lines changed

blueprint/cli/src/bin/db.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,19 @@ fn get_db_config(config: &DatabaseConfig) -> PgConnectOptions {
276276

277277
async fn get_db_client(config: &DatabaseConfig) -> PgConnection {
278278
let db_config = get_db_config(config);
279-
let connection: PgConnection = Connection::connect_with(&db_config).await.unwrap();
279+
let connection: PgConnection = Connection::connect_with(&db_config)
280+
.await
281+
.expect("Should be able to connect to the database!");
280282

281283
connection
282284
}
283285

284286
async fn get_root_db_client(config: &DatabaseConfig) -> PgConnection {
285287
let db_config = get_db_config(config);
286288
let root_db_config = db_config.clone().database("postgres");
287-
let connection: PgConnection = Connection::connect_with(&root_db_config).await.unwrap();
289+
let connection: PgConnection = Connection::connect_with(&root_db_config)
290+
.await
291+
.expect("Should be able to connect to the database!");
288292

289293
connection
290294
}

blueprint/cli/src/bin/generate.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ fn get_liquid_template(path: &str) -> Result<Template, anyhow::Error> {
405405
.context(format!("Failed to read blueprint {path}!"))?;
406406
let template = liquid::ParserBuilder::with_stdlib()
407407
.build()
408-
.unwrap()
408+
.context("Failed to build Liquid parser")?
409409
.parse(template_source)
410410
.context("Failed to parse blueprint as Liquid template")?;
411411

@@ -485,10 +485,15 @@ fn validate_fields(fields: &[String]) -> Result<Vec<HashMap<String, String>>, an
485485
return Err(anyhow!("Invalid field definition: {field}!"));
486486
}
487487

488-
let field_name = String::from(captures.get(1).unwrap().as_str());
488+
let field_name = String::from(
489+
captures
490+
.get(1)
491+
.expect("Should be able to get the field name")
492+
.as_str(),
493+
);
489494
let field_type = captures
490495
.get(2)
491-
.unwrap()
496+
.expect("Should be able to get the field type")
492497
.as_str()
493498
.replace("Bool", "bool")
494499
.replace("string", "String");

blueprint/db/src/test_helpers/mod.rs.liquid

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,41 @@ pub async fn teardown_db(db_pool: DbPool) {
3535
drop(db_pool);
3636

3737
let root_db_config = db_config.clone().database("postgres");
38-
let mut connection: PgConnection = Connection::connect_with(&root_db_config).await.unwrap();
38+
let mut connection: PgConnection = Connection::connect_with(&root_db_config)
39+
.await
40+
.expect("Should be able to connect to Postgres database!");
3941

40-
let test_db_name = db_config.get_database().unwrap();
42+
let test_db_name = db_config
43+
.get_database()
44+
.expect("Should be able to get the database name!");
4145

4246
let query = format!("DROP DATABASE IF EXISTS {test_db_name}");
43-
connection.execute(query.as_str()).await.unwrap();
47+
connection
48+
.execute(query.as_str())
49+
.await
50+
.unwrap_or_else(|_| panic!("Could not execute DROP DATABASE {test_db_name} query!"));
4451
}
4552

4653
async fn prepare_db(config: &DatabaseConfig) -> DatabaseConfig {
4754
let db_config = parse_db_config(&config.url);
48-
let db_name = db_config.get_database().unwrap();
55+
let db_name = db_config
56+
.get_database()
57+
.expect("Should be able to get the database name!");
4958

5059
let root_db_config = db_config.clone().database("postgres");
51-
let mut connection: PgConnection = Connection::connect_with(&root_db_config).await.unwrap();
60+
let mut connection: PgConnection = Connection::connect_with(&root_db_config)
61+
.await
62+
.expect("Should be able to connect to the Postgres database!");
5263

5364
let test_db_name = build_test_db_name(db_name);
5465

5566
let query = format!("CREATE DATABASE {test_db_name} TEMPLATE {db_name}");
56-
connection.execute(query.as_str()).await.unwrap();
67+
connection
68+
.execute(query.as_str())
69+
.await
70+
.unwrap_or_else(|_| panic!("Could not execute CREATE DATABASE {test_db_name} query!"));
5771

58-
let regex = Regex::new(r"(.+)\/(.+$)").unwrap();
72+
let regex = Regex::new(r"(.+)/(.+$)").expect("Should be able to compile regex");
5973
let test_db_url = regex.replace(&config.url, |caps: &Captures| {
6074
format!("{}/{}", &caps[1], test_db_name)
6175
});

blueprint/web/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub async fn run() -> anyhow::Result<()> {
5454
pub fn init_tracing() {
5555
let filter = EnvFilter::try_from_default_env()
5656
.or_else(|_| EnvFilter::try_new("info"))
57-
.unwrap();
57+
.expect("Should be able to parse RUST_LOG or construct default filter from info level");
5858
tracing_subscriber::registry()
5959
.with(fmt::layer())
6060
.with(filter)

blueprint/web/src/test_helpers/mod.rs.liquid

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ impl TestRequest {
7878
#[allow(unused)]
7979
#[must_use]
8080
pub fn header(mut self, name: HeaderName, value: &str) -> Self {
81-
self.headers.insert(name, value.parse().unwrap());
81+
self.headers.insert(
82+
name,
83+
value.parse().expect("Should be abe to parse header value"),
84+
);
8285
self
8386
}
8487

@@ -112,9 +115,11 @@ impl TestRequest {
112115

113116
request_builder = request_builder.method(&self.method);
114117

115-
let request = request_builder.body(self.body);
118+
let request = request_builder
119+
.body(self.body)
120+
.expect("Should be able to build request");
116121

117-
self.router.oneshot(request.unwrap()).await.unwrap()
122+
self.router.oneshot(request).await.unwrap()
118123
}
119124
}
120125

@@ -222,7 +227,9 @@ pub struct DbTestContext {
222227
#[allow(unused)]
223228
pub async fn setup() -> DbTestContext {
224229
let init_config: OnceCell<Config> = OnceCell::new();
225-
let config = init_config.get_or_init(|| load_config(&Environment::Test).unwrap());
230+
let config = init_config.get_or_init(|| {
231+
load_config(&Environment::Test).expect("Should be able to load Test environment config")
232+
});
226233

227234
let test_db_pool = setup_db(&config.database).await;
228235

src/ui/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,10 @@ mod tests {
239239
}
240240

241241
fn read_buffer(buffer: std::io::BufWriter<Vec<u8>>) -> String {
242-
let bytes = buffer.into_inner().unwrap();
242+
let bytes = buffer
243+
.into_inner()
244+
.expect("Should be able to flush the buffer!");
243245

244-
String::from_utf8(bytes).unwrap()
246+
String::from_utf8(bytes).expect("Should be able to convert the buffer to a string!")
245247
}
246248
}

0 commit comments

Comments
 (0)