Repository: meilisearch/meilisearch
File: meilisearch/src/error.rs (Rust)

Code before:
impl From<MeilisearchHttpError> for aweb::Error {
    fn from(other: MeilisearchHttpError) -> Self {
        aweb::Error::from(ResponseError::from(other))
    }
}

impl From<aweb::error::PayloadError> for MeilisearchHttpError {
    fn from(error: aweb::error::PayloadError) -> Self {
        MeilisearchHttpError::Payload(PayloadError::Payload(error))
    }
}

#[derive(Debug, thiserror::Error)]
pub enum PayloadError {
    #[error(transparent)]
    Payload(aweb::error::PayloadError),
    #[error(transparent)]
    Json(JsonPayloadError),
    #[error(transparent)]
    Query(QueryPayloadError),
    #[error("The json payload provided is malformed. `{0}`.")]
    MalformedPayload(serde_json::error::Error),
    #[error("A json payload is missing.")]
    MissingPayload,
}

impl ErrorCode for PayloadError {
    fn error_code(&self) -> Code {
        match self {
            PayloadError::Payload(e) => match e {
                aweb::error::PayloadError::Incomplete(_) => Code::Internal,
                aweb::error::PayloadError::EncodingCorrupted => Code::Internal,
                aweb::error::PayloadError::Overflow => Code::PayloadTooLarge,
                aweb::error::PayloadError::UnknownLength => Code::Internal,
                aweb::error::PayloadError::Http2Payload(_) => Code::Internal,
                aweb::error::PayloadError::Io(_) => Code::Internal,
                _ => todo!(),
            },
            PayloadError::Json(err) => match err {
                JsonPayloadError::Overflow { .. } => Code::PayloadTooLarge,
                JsonPayloadError::ContentType => Code::UnsupportedMediaType,
                JsonPayloadError::Payload(aweb::error::PayloadError::Overflow) => {
                    Code::PayloadTooLarge
                }
                JsonPayloadError::Payload(_) => Code::BadRequest,
                JsonPayloadError::Deserialize(_) => Code::BadRequest,
                JsonPayloadError::Serialize(_) => Code::Internal,
                _ => Code::Internal,
            },
            PayloadError::Query(err) => match err {
                QueryPayloadError::Deserialize(_) => Code::BadRequest,

Review comment (suggestion):
We need to rename this variant. There already is a `PayloadErr` part in the enum name.

Code after:
impl From<MeilisearchHttpError> for aweb::Error {
    fn from(other: MeilisearchHttpError) -> Self {
        aweb::Error::from(ResponseError::from(other))
    }
}

impl From<aweb::error::PayloadError> for MeilisearchHttpError {
    fn from(error: aweb::error::PayloadError) -> Self {
        MeilisearchHttpError::Payload(PayloadError::Payload(error))
    }
}

#[derive(Debug, thiserror::Error)]
pub enum PayloadError {
    #[error(transparent)]
    Payload(aweb::error::PayloadError),
    #[error(transparent)]
    Json(JsonPayloadError),
    #[error(transparent)]
    Query(QueryPayloadError),
    #[error("The json payload provided is malformed. `{0}`.")]
    MalformedPayload(serde_json::error::Error),
    #[error("A json payload is missing.")]
    MissingPayload,
    #[error("Error while writing the playload to disk: `{0}`.")]
    ReceivePayloadErr(Box<dyn std::error::Error + Send + Sync + 'static>),
}

impl ErrorCode for PayloadError {
    fn error_code(&self) -> Code {
        match self {
            PayloadError::Payload(e) => match e {
                aweb::error::PayloadError::Incomplete(_) => Code::Internal,
                aweb::error::PayloadError::EncodingCorrupted => Code::Internal,
                aweb::error::PayloadError::Overflow => Code::PayloadTooLarge,
                aweb::error::PayloadError::UnknownLength => Code::Internal,
                aweb::error::PayloadError::Http2Payload(_) => Code::Internal,
                aweb::error::PayloadError::Io(_) => Code::Internal,
                _ => todo!(),
            },
            PayloadError::Json(err) => match err {
                JsonPayloadError::Overflow { .. } => Code::PayloadTooLarge,
                JsonPayloadError::ContentType => Code::UnsupportedMediaType,
                JsonPayloadError::Payload(aweb::error::PayloadError::Overflow) => {
                    Code::PayloadTooLarge
                }
                JsonPayloadError::Payload(_) => Code::BadRequest,
                JsonPayloadError::Deserialize(_) => Code::BadRequest,
                JsonPayloadError::Serialize(_) => Code::Internal,
                _ => Code::Internal,
            },