On the Connection Between Memory Management and Data-race Freedom [Rust]
smallcultfollowing.com6 pointsby grayrest0 comments
book! = |req| {
body : { id : I64 }
body = Req.json_body!(req)?
rows = Sql.query!(req.ctx, db_path, "SELECT id, title, author, year FROM books WHERE id = ?", [Integer(body.id)])?
row = Sql.first(rows) ? |_| NotFound("book ${body.id.to_str()} not found")
book = decode_book(row)?
Ok(book)
}
The full set of errors covers malformed utf8, missing/wrong type for id, db errors, the custom NotFound with message, and missing/changed db columns and these plus all the other errors across the app get rolled up and handled in one spot by the error mapping function which rolls the input errors to 400, a 404 for the NotFound, 500s in general in a big match. I have more compact ways to express this in the platform (sqlx) but those don't show off the error handling as nicely.