diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 037d21defb..bdfdee20a8 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -56,6 +56,7 @@ on: branches: - master - 0.2.x + - pr/890 concurrency: group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} diff --git a/examples/actix3_example/core/src/mutation.rs b/examples/actix3_example/core/src/mutation.rs index 7f0150a638..dd6891d4ae 100644 --- a/examples/actix3_example/core/src/mutation.rs +++ b/examples/actix3_example/core/src/mutation.rs @@ -22,11 +22,11 @@ impl Mutation { id: i32, form_data: post::Model, ) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post::ActiveModel { id: post.id, @@ -38,11 +38,11 @@ impl Mutation { } pub async fn delete_post(db: &DbConn, id: i32) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post.delete(db).await } diff --git a/examples/actix_example/core/src/mutation.rs b/examples/actix_example/core/src/mutation.rs index 7f0150a638..dd6891d4ae 100644 --- a/examples/actix_example/core/src/mutation.rs +++ b/examples/actix_example/core/src/mutation.rs @@ -22,11 +22,11 @@ impl Mutation { id: i32, form_data: post::Model, ) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post::ActiveModel { id: post.id, @@ -38,11 +38,11 @@ impl Mutation { } pub async fn delete_post(db: &DbConn, id: i32) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post.delete(db).await } diff --git a/examples/axum_example/core/src/mutation.rs b/examples/axum_example/core/src/mutation.rs index 7f0150a638..dd6891d4ae 100644 --- a/examples/axum_example/core/src/mutation.rs +++ b/examples/axum_example/core/src/mutation.rs @@ -22,11 +22,11 @@ impl Mutation { id: i32, form_data: post::Model, ) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post::ActiveModel { id: post.id, @@ -38,11 +38,11 @@ impl Mutation { } pub async fn delete_post(db: &DbConn, id: i32) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post.delete(db).await } diff --git a/examples/graphql_example/core/src/mutation.rs b/examples/graphql_example/core/src/mutation.rs index 6eac6814a4..1f2447fba2 100644 --- a/examples/graphql_example/core/src/mutation.rs +++ b/examples/graphql_example/core/src/mutation.rs @@ -23,11 +23,11 @@ impl Mutation { id: i32, form_data: note::Model, ) -> Result { - let note: note::ActiveModel = if let Ok(Some(note)) = Note::find_by_id(id).one(db).await { - note.into() - } else { - return Err(DbErr::Custom("Cannot find note.".to_owned())); - }; + let note: note::ActiveModel = Note::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find note.".to_owned())) + .map(Into::into)?; note::ActiveModel { id: note.id, @@ -39,11 +39,11 @@ impl Mutation { } pub async fn delete_note(db: &DbConn, id: i32) -> Result { - let note: note::ActiveModel = if let Ok(Some(note)) = Note::find_by_id(id).one(db).await { - note.into() - } else { - return Err(DbErr::Custom("Cannot find note.".to_owned())); - }; + let note: note::ActiveModel = Note::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find note.".to_owned())) + .map(Into::into)?; note.delete(db).await } diff --git a/examples/jsonrpsee_example/api/src/lib.rs b/examples/jsonrpsee_example/api/src/lib.rs index 2e5f2c7234..f98cc18328 100644 --- a/examples/jsonrpsee_example/api/src/lib.rs +++ b/examples/jsonrpsee_example/api/src/lib.rs @@ -60,7 +60,7 @@ impl PostRpcServer for PpcImpl { async fn insert(&self, p: post::Model) -> RpcResult { let new_post = Mutation::create_post(&self.conn, p) .await - .expect("could not insert post"); + .internal_call_error()?; Ok(new_post.id.unwrap()) } diff --git a/examples/jsonrpsee_example/core/src/mutation.rs b/examples/jsonrpsee_example/core/src/mutation.rs index 7f0150a638..dd6891d4ae 100644 --- a/examples/jsonrpsee_example/core/src/mutation.rs +++ b/examples/jsonrpsee_example/core/src/mutation.rs @@ -22,11 +22,11 @@ impl Mutation { id: i32, form_data: post::Model, ) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post::ActiveModel { id: post.id, @@ -38,11 +38,11 @@ impl Mutation { } pub async fn delete_post(db: &DbConn, id: i32) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post.delete(db).await } diff --git a/examples/poem_example/core/src/mutation.rs b/examples/poem_example/core/src/mutation.rs index 7f0150a638..dd6891d4ae 100644 --- a/examples/poem_example/core/src/mutation.rs +++ b/examples/poem_example/core/src/mutation.rs @@ -22,11 +22,11 @@ impl Mutation { id: i32, form_data: post::Model, ) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post::ActiveModel { id: post.id, @@ -38,11 +38,11 @@ impl Mutation { } pub async fn delete_post(db: &DbConn, id: i32) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post.delete(db).await } diff --git a/examples/rocket_example/core/src/mutation.rs b/examples/rocket_example/core/src/mutation.rs index 7f0150a638..dd6891d4ae 100644 --- a/examples/rocket_example/core/src/mutation.rs +++ b/examples/rocket_example/core/src/mutation.rs @@ -22,11 +22,11 @@ impl Mutation { id: i32, form_data: post::Model, ) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post::ActiveModel { id: post.id, @@ -38,11 +38,11 @@ impl Mutation { } pub async fn delete_post(db: &DbConn, id: i32) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post.delete(db).await } diff --git a/examples/salvo_example/core/src/mutation.rs b/examples/salvo_example/core/src/mutation.rs index 7f0150a638..dd6891d4ae 100644 --- a/examples/salvo_example/core/src/mutation.rs +++ b/examples/salvo_example/core/src/mutation.rs @@ -22,11 +22,11 @@ impl Mutation { id: i32, form_data: post::Model, ) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post::ActiveModel { id: post.id, @@ -38,11 +38,11 @@ impl Mutation { } pub async fn delete_post(db: &DbConn, id: i32) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post.delete(db).await } diff --git a/examples/tonic_example/core/src/mutation.rs b/examples/tonic_example/core/src/mutation.rs index 7f0150a638..dd6891d4ae 100644 --- a/examples/tonic_example/core/src/mutation.rs +++ b/examples/tonic_example/core/src/mutation.rs @@ -22,11 +22,11 @@ impl Mutation { id: i32, form_data: post::Model, ) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post::ActiveModel { id: post.id, @@ -38,11 +38,11 @@ impl Mutation { } pub async fn delete_post(db: &DbConn, id: i32) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post.delete(db).await }