Skip to content

How can I custom the response error message in different situations? #1822

Answered by jebrosen
zemelLeong asked this question in Questions
Discussion options

You must be logged in to vote

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:

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 Result<String, String> to whatever you like depending on how that route needs to respond - e.g. Result<Template, Json<MadeUpErrorType>>.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by zemelLeong
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants