0

I have made a rust webserver using Warp. When I send a request to the webserver, it cannot deserialize the given body. I know exactly why this error happens, and am not asking about how to fix the deserializatin error. The issue that I have is that nothing is logged when this error occurs. I would like to learn how to generate logs for an error of this kind.

The router function for the route in question is here:

pub fn put_order(repo: data::Repository) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
    warp::path("orders")
        .and(warp::path("v1")) 
        .and(warp::put())
        .and(with_order_body())
        .and(with_repo(repo))
        .and_then(orders::put)
}

The with_order_body() function in particular is the one that I believe is throwing the error with deserialization:

fn with_order_body() -> impl Filter<Extract = (model::Order,), Error = Rejection> + Clone {
    warp::body::content_length_limit(1024 * 16).and(warp::body::json())
}

The Order type is defined (and it is supposed to be a CouchDB document using the couch_rs package, in case you were wondering) :

#[derive(Debug, Serialize, Deserialize, Clone, CouchDocument)]
pub struct Order {
    #[serde(skip_serializing_if = "String::is_empty")]
    _id: DocumentId,
    #[serde(skip_serializing_if = "String::is_empty")]
    _rev: String,
    items: Vec<OrderItem>,
    user_token: String,
    payment_token: String,
}

The error that results is returned when I curl my webserver, but not logged:

Request body deserialize error: missing field `_rev` at line 24 column 1

The request payload, you may not be surprised to find, is a 24 line JSON payload; the error "points" to the closing bracket on the JSON payload.

How can I get this log to be returned at command line when I run my webserver locally, in addition to being returned as an error to the remote caller.

Caesar
  • 6,733
  • 4
  • 38
  • 44
kingledion
  • 2,263
  • 3
  • 25
  • 39
  • Is relevante? https://stackoverflow.com/questions/61275060/how-to-log-request-response-bodies-in-warp – Zeppi Nov 16 '21 at 06:23
  • `.and(warp::body::json().map(|body| { if body.is_err() { log_error }; body}))`, you can map over the body::json? – Netwave Nov 16 '21 at 13:21

0 Answers0