How can I custom the response error message in different situations? #1822
Answered
by
jebrosen
zemelLeong
asked this question in
Questions
-
Beta Was this translation helpful? Give feedback.
Answered by
jebrosen
Aug 14, 2021
Replies: 1 comment
-
This is really close. First, you'll have to change the return type of pub async fn delete_document(conn: MysqlDBConn, ids: Json<Vec<i32>>) -> Result<String, String> { ... } Then, the handler can return a response according to the result of that database operation: let res = conn.run(...).await;
match res {
Ok(count) => Ok(format!("Deleted {} records", count)),
Err(e) => Err(e.to_string()),
} And, you can change |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
zemelLeong
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is really close.
First, you'll have to change the return type of
delete_document
so you can return either a success or error response:Then, the handler can return a response according to the result of that database operation:
And, you can change
Result<String, String>
to whatever you like depending on how that route needs to respond - e.g.Result<Template, Json<MadeUpErrorType>>
.