diff --git a/examples/session_auth_axum/src/auth.rs b/examples/session_auth_axum/src/auth.rs index b96d360eb..3334f3238 100644 --- a/examples/session_auth_axum/src/auth.rs +++ b/examples/session_auth_axum/src/auth.rs @@ -163,12 +163,11 @@ pub async fn login( let user: User = User::get_from_username(username, &pool) .await - .ok_or("User does not exist.") - .map_err(|e| ServerFnError::ServerError(e.to_string()))?; + .ok_or_else(|| { + ServerFnError::ServerError("User does not exist.".into()) + })?; - match verify(password, &user.password) - .map_err(|e| ServerFnError::ServerError(e.to_string()))? - { + match verify(password, &user.password)? { true => { auth.login_user(user.id); auth.remember_user(remember.is_some()); @@ -204,13 +203,16 @@ pub async fn signup( .bind(username.clone()) .bind(password_hashed) .execute(&pool) - .await - .map_err(|e| ServerFnError::ServerError(e.to_string()))?; + .await?; - let user = User::get_from_username(username, &pool) - .await - .ok_or("Signup failed: User does not exist.") - .map_err(|e| ServerFnError::ServerError(e.to_string()))?; + let user = + User::get_from_username(username, &pool) + .await + .ok_or_else(|| { + ServerFnError::ServerError( + "Signup failed: User does not exist.".into(), + ) + })?; auth.login_user(user.id); auth.remember_user(remember.is_some()); diff --git a/examples/session_auth_axum/src/todo.rs b/examples/session_auth_axum/src/todo.rs index 9d195472c..afd54a5ed 100644 --- a/examples/session_auth_axum/src/todo.rs +++ b/examples/session_auth_axum/src/todo.rs @@ -21,14 +21,12 @@ if #[cfg(feature = "ssr")] { pub fn pool(cx: Scope) -> Result { use_context::(cx) - .ok_or("Pool missing.") - .map_err(|e| ServerFnError::ServerError(e.to_string())) + .ok_or_else(|| ServerFnError::ServerError("Pool missing.".into())) } pub fn auth(cx: Scope) -> Result { use_context::(cx) - .ok_or("Auth session missing.") - .map_err(|e| ServerFnError::ServerError(e.to_string())) + .ok_or_else(|| ServerFnError::ServerError("Auth session missing.".into())) } #[derive(sqlx::FromRow, Clone)] @@ -64,11 +62,7 @@ pub async fn get_todos(cx: Scope) -> Result, ServerFnError> { let mut rows = sqlx::query_as::<_, SqlTodo>("SELECT * FROM todos").fetch(&pool); - while let Some(row) = rows - .try_next() - .await - .map_err(|e| ServerFnError::ServerError(e.to_string()))? - { + while let Some(row) = rows.try_next().await? { todos.push(row); } @@ -117,12 +111,11 @@ pub async fn add_todo(cx: Scope, title: String) -> Result<(), ServerFnError> { pub async fn delete_todo(cx: Scope, id: u16) -> Result<(), ServerFnError> { let pool = pool(cx)?; - sqlx::query("DELETE FROM todos WHERE id = $1") + Ok(sqlx::query("DELETE FROM todos WHERE id = $1") .bind(id) .execute(&pool) .await - .map(|_| ()) - .map_err(|e| ServerFnError::ServerError(e.to_string())) + .map(|_| ())?) } #[component] diff --git a/examples/todo_app_sqlite_axum/src/todo.rs b/examples/todo_app_sqlite_axum/src/todo.rs index d603f2dad..e5492aa74 100644 --- a/examples/todo_app_sqlite_axum/src/todo.rs +++ b/examples/todo_app_sqlite_axum/src/todo.rs @@ -11,7 +11,7 @@ cfg_if! { // use http::{header::SET_COOKIE, HeaderMap, HeaderValue, StatusCode}; pub async fn db() -> Result { - SqliteConnection::connect("sqlite:Todos.db").await.map_err(|e| ServerFnError::ServerError(e.to_string())) + Ok(SqliteConnection::connect("sqlite:Todos.db").await?) } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)] @@ -47,11 +47,7 @@ pub async fn get_todos(cx: Scope) -> Result, ServerFnError> { let mut todos = Vec::new(); let mut rows = sqlx::query_as::<_, Todo>("SELECT * FROM todos").fetch(&mut conn); - while let Some(row) = rows - .try_next() - .await - .map_err(|e| ServerFnError::ServerError(e.to_string()))? - { + while let Some(row) = rows.try_next().await? { todos.push(row); } @@ -93,12 +89,11 @@ pub async fn add_todo(title: String) -> Result<(), ServerFnError> { pub async fn delete_todo(id: u16) -> Result<(), ServerFnError> { let mut conn = db().await?; - sqlx::query("DELETE FROM todos WHERE id = $1") + Ok(sqlx::query("DELETE FROM todos WHERE id = $1") .bind(id) .execute(&mut conn) .await - .map(|_| ()) - .map_err(|e| ServerFnError::ServerError(e.to_string())) + .map(|_| ())?) } #[component] diff --git a/examples/todo_app_sqlite_viz/src/todo.rs b/examples/todo_app_sqlite_viz/src/todo.rs index fda62c494..f9a987c6f 100644 --- a/examples/todo_app_sqlite_viz/src/todo.rs +++ b/examples/todo_app_sqlite_viz/src/todo.rs @@ -11,7 +11,7 @@ cfg_if! { // use http::{header::SET_COOKIE, HeaderMap, HeaderValue, StatusCode}; pub async fn db() -> Result { - SqliteConnection::connect("sqlite:Todos.db").await.map_err(|e| ServerFnError::ServerError(e.to_string())) + Ok(SqliteConnection::connect("sqlite:Todos.db").await?) } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)] @@ -47,11 +47,7 @@ pub async fn get_todos(cx: Scope) -> Result, ServerFnError> { let mut todos = Vec::new(); let mut rows = sqlx::query_as::<_, Todo>("SELECT * FROM todos").fetch(&mut conn); - while let Some(row) = rows - .try_next() - .await - .map_err(|e| ServerFnError::ServerError(e.to_string()))? - { + while let Some(row) = rows.try_next().await? { todos.push(row); } @@ -93,12 +89,11 @@ pub async fn add_todo(title: String) -> Result<(), ServerFnError> { pub async fn delete_todo(id: u16) -> Result<(), ServerFnError> { let mut conn = db().await?; - sqlx::query("DELETE FROM todos WHERE id = $1") + Ok(sqlx::query("DELETE FROM todos WHERE id = $1") .bind(id) .execute(&mut conn) .await - .map(|_| ()) - .map_err(|e| ServerFnError::ServerError(e.to_string())) + .map(|_| ())?) } #[component]