improved error messages

This commit is contained in:
Leonard Lorenz 2020-10-22 01:38:43 +02:00
parent b848922eee
commit bc806593e3
2 changed files with 9 additions and 8 deletions

View file

@ -8,7 +8,7 @@ use models::*;
fn establish_connection() -> SqliteConnection {
let db_path = "db.sqlite3";
SqliteConnection::establish(&db_path)
.unwrap_or_else(|_| panic!("Error connection to {}", &db_path))
.unwrap_or_else(|_| panic!("Error, connection to {} failed.", &db_path))
}
pub fn get_posts() -> std::vec::Vec<Post> {
@ -16,9 +16,10 @@ pub fn get_posts() -> std::vec::Vec<Post> {
let connection = establish_connection();
posts
.filter(published.eq(true))
.order(id.desc())
.limit(5)
.load::<Post>(&connection)
.expect("Error loading posts")
.expect("Error, couldn't load posts.")
}
pub fn get_post_by_id(id: i32) -> Post {
@ -27,7 +28,7 @@ pub fn get_post_by_id(id: i32) -> Post {
posts
.find(id)
.get_result(&connection)
.expect("Couldn't find post")
.expect("Error, couldn't find post")
}
pub fn add_post(title: &str, body: &str) {
@ -46,5 +47,5 @@ pub fn add_post(title: &str, body: &str) {
diesel::insert_into(posts::table)
.values(&new_post)
.execute(&connection)
.unwrap_or_else(|_| panic!("Error couldn't insert new Post!"));
.unwrap_or_else(|_| panic!("Error, couldn't insert new Post."));
}

View file

@ -19,12 +19,12 @@ async fn blog() -> impl Responder {
// one-off render blog template with context
let result = Tera::one_off(
&(std::fs::read_to_string("templates/blog.html")
.unwrap_or_else(|_| panic!("Couldn't load blog template."))
.unwrap_or_else(|e| panic!("Error, couldn't load blog template.\n{}", e))
.as_str()),
&context,
true,
)
.unwrap_or_else(|_| panic!("Couldn't render blog template."));
.unwrap_or_else(|e| panic!("Error, couldn't render blog template.\n{}", e));
HttpResponse::Ok().body(result)
}
@ -38,11 +38,11 @@ async fn blog_permalink(web::Path(post_id): web::Path<u32>) -> impl Responder {
// one-off render blog template with context
let result = Tera::one_off(
&(std::fs::read_to_string("templates/blog.html")
.unwrap_or_else(|_| panic!("Couldn't load blog template."))
.unwrap_or_else(|e| panic!("Error, couldn't load blog template.\n{}", e))
.as_str()),
&context,
true,
)
.unwrap_or_else(|_| panic!("Couldn't render blog template."));
.unwrap_or_else(|e| panic!("Error, couldn't render blog template.\n{}", e));
HttpResponse::Ok().body(result)
}