#![allow(clippy::doc_markdown)]
#[derive(Debug, thiserror::Error)]
pub enum MdmError {
#[error("invalid MaLo-ID '{id}': {reason}")]
InvalidMaloId { id: String, reason: String },
#[error("invalid MeLo-ID '{id}': {reason}")]
InvalidMeloId { id: String, reason: String },
#[error("invalid GLN '{mp_id}': {reason}")]
InvalidMpId { mp_id: String, reason: String },
#[error("not found: {resource_type} '{id}'")]
NotFound {
resource_type: &'static str,
id: String,
},
#[error("version conflict: expected etag '{expected}', found '{actual}'")]
VersionConflict { expected: String, actual: String },
#[error("forbidden: {reason}")]
Forbidden { reason: &'static str },
#[error("unprocessable: {reason}")]
Unprocessable { reason: String },
#[error("makod sync failed: {0}")]
MakodSync(String),
#[error("makod rejected the command ({kind}): {detail}")]
MakodConflict { kind: String, detail: String },
#[error("webhook delivery failed (subscriber={subscriber_id}): {reason}")]
WebhookDelivery {
subscriber_id: String,
reason: String,
},
#[error("internal: {0}")]
Internal(String),
}
impl MdmError {
#[must_use]
pub fn status_u16(&self) -> u16 {
match self {
Self::InvalidMaloId { .. }
| Self::InvalidMeloId { .. }
| Self::InvalidMpId { .. }
| Self::Unprocessable { .. } => 422,
Self::NotFound { .. } => 404,
Self::VersionConflict { .. } => 412,
Self::MakodConflict { .. } => 409,
Self::Forbidden { .. } => 403,
Self::MakodSync(_) | Self::WebhookDelivery { .. } | Self::Internal(_) => 500,
}
}
#[must_use]
pub fn error_code(&self) -> &'static str {
match self {
Self::InvalidMaloId { .. } => "invalid_malo_id",
Self::InvalidMeloId { .. } => "invalid_melo_id",
Self::InvalidMpId { .. } => "invalid_gln",
Self::NotFound { .. } => "not_found",
Self::VersionConflict { .. } => "version_conflict",
Self::Forbidden { .. } => "forbidden",
Self::Unprocessable { .. } => "unprocessable",
Self::MakodConflict { .. } => "makod_conflict",
Self::MakodSync(_) => "makod_sync_failed",
Self::WebhookDelivery { .. } => "webhook_delivery_failed",
Self::Internal(_) => "internal_error",
}
}
#[must_use]
pub fn error_title(&self) -> &'static str {
match self {
Self::InvalidMaloId { .. } => "Invalid MaLo-ID",
Self::InvalidMeloId { .. } => "Invalid MeLo-ID",
Self::InvalidMpId { .. } => "Invalid GLN",
Self::NotFound { .. } => "Not Found",
Self::VersionConflict { .. } => "Version Conflict",
Self::Forbidden { .. } => "Forbidden",
Self::Unprocessable { .. } => "Unprocessable Content",
Self::MakodConflict { .. } => "Command Conflict",
Self::MakodSync(_) | Self::WebhookDelivery { .. } | Self::Internal(_) => {
"Internal Server Error"
}
}
}
}