mongodb / bson-rust

Encoding and decoding support for BSON in Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Impl Serialize for WriteFailure, WriteConcernError, WriteError, and BulkWriteFailure in lib

borngraced opened this issue · comments

This will very useful for returning detailed errors in JSON format in actix web and co.

Currently we can't do something like this with KhWriteFailure etc unless Serialize is also implemented for the structures either by using derive_macro! or manually!

#[derive(Debug, Deserialize, Serialize)]
pub enum KhoomiResponseError {
    WriteError(WriteFailure),
}
impl ResponseError for KhoomiResponseError {
    fn status_code(&self) -> StatusCode {
        match self {
              ...,
            _ => StatusCode::INTERNAL_SERVER_ERROR,
        }
    }

    fn error_response(&self) -> HttpResponse {
        HttpResponse::build(self.status_code())
            .insert_header(ContentType::html())
            .json(self)
    }
}

I was able to fix this creating a custom KhWriteFailure that implements Serialize and then impl From<WriteFailure> for KhWriteFailure

wrong repo forthis issue...I'm closing