Skip to content

RUST-1559 Impl Serialize for WriteFailure, WriteConcernError, WriteError, and BulkWriteFailure #796

Closed
@borngraced

Description

@borngraced

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 by creating a custom KhWriteFailure that implements Serialize and then impl From<WriteFailure> for KhWriteFailure

Metadata

Metadata

Assignees

Labels

tracked-in-jiraTicket filed in Mongo's Jira system

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions