pub struct CqrsError(/* private fields */);Expand description
Unified error for API responses.
This is a thin wrapper around Box<CqrsErrorData> to keep Result<_, CqrsError>
small on the stack. Access fields via Deref (e.g. err.domain, err.code).
§JSON Format
{
"domain": "plan",
"code": "PLAN_NOT_FOUND",
"internalCode": 4001,
"message": "Tenant with ID 'abc' not found",
"details": { "id": "abc" },
"requestId": "req-123"
}Implementations§
Source§impl CqrsError
impl CqrsError
Sourcepub fn from_code<C: CqrsErrorCode>(code: &C, message: impl Into<String>) -> Self
pub fn from_code<C: CqrsErrorCode>(code: &C, message: impl Into<String>) -> Self
Create a CqrsError from any domain error code.
Sourcepub fn with_details(self, details: Value) -> Self
pub fn with_details(self, details: Value) -> Self
Add additional details to the error.
Sourcepub fn with_request_id(self, request_id: impl Into<String>) -> Self
pub fn with_request_id(self, request_id: impl Into<String>) -> Self
Add a request ID for tracing.
Sourcepub fn with_request_id_if_absent(self, request_id: impl Into<String>) -> Self
pub fn with_request_id_if_absent(self, request_id: impl Into<String>) -> Self
Add a request ID unless the error already carries one (empty ids are
ignored). Used by the REST layer to stamp errors with the
crate::CqrsContext request id on their way out.
Sourcepub fn with_type_uri(self, type_uri: impl Into<String>) -> Self
pub fn with_type_uri(self, type_uri: impl Into<String>) -> Self
Override the type member of the RFC 9457 problem document for this
error, bypassing both the configured base URI and the default
urn:cqrs-error:{domain}:{code}.
Sourcepub fn http_status(&self) -> StatusCode
pub fn http_status(&self) -> StatusCode
Get the HTTP status code for this error.
Sourcepub fn to_problem(&self) -> ProblemDetails
pub fn to_problem(&self) -> ProblemDetails
Render this error as an RFC 9457 problem document.
Available regardless of the problem-json feature, which only controls
what the built-in Axum routers emit.
Sourcepub fn validation(message: impl Into<String>) -> Self
pub fn validation(message: impl Into<String>) -> Self
Create a generic validation error.
Create a generic unauthorized error.
Sourcepub fn unprocessable(message: impl Into<String>) -> Self
pub fn unprocessable(message: impl Into<String>) -> Self
Create a generic unprocessable entity error (422).
Use this instead of Self::validation when the request is
syntactically valid but semantically rejected.
Sourcepub fn precondition_failed(message: impl Into<String>) -> Self
pub fn precondition_failed(message: impl Into<String>) -> Self
Create a generic precondition failed error (412).
Sourcepub fn precondition_required(message: impl Into<String>) -> Self
pub fn precondition_required(message: impl Into<String>) -> Self
Create a generic precondition required error (428).
Sourcepub fn unsupported_media_type(message: impl Into<String>) -> Self
pub fn unsupported_media_type(message: impl Into<String>) -> Self
Create a generic unsupported media type error (415).
Sourcepub fn payload_too_large(message: impl Into<String>) -> Self
pub fn payload_too_large(message: impl Into<String>) -> Self
Create a generic payload too large error (413).
Sourcepub fn too_many_requests(message: impl Into<String>) -> Self
pub fn too_many_requests(message: impl Into<String>) -> Self
Create a generic too many requests error (429).
Sourcepub fn not_implemented(message: impl Into<String>) -> Self
pub fn not_implemented(message: impl Into<String>) -> Self
Create a generic not implemented error (501).
Create a generic service unavailable error (503).
Sourcepub fn user_error(e: impl Display) -> Self
pub fn user_error(e: impl Display) -> Self
Create a user/domain error.
Sourcepub fn database_error(e: impl Display) -> Self
pub fn database_error(e: impl Display) -> Self
Create a database error.
Sourcepub fn serialization_error(e: impl Display) -> Self
pub fn serialization_error(e: impl Display) -> Self
Create a serialization error.
Sourcepub fn concurrency_error() -> Self
pub fn concurrency_error() -> Self
Create a concurrency/version conflict error.
Sourcepub fn aggregate_not_found(id: &str) -> Self
pub fn aggregate_not_found(id: &str) -> Self
Create an aggregate not found error.
Sourcepub fn aggregate_already_exists(id: &str) -> Self
pub fn aggregate_already_exists(id: &str) -> Self
Create an aggregate already exists error.
Sourcepub fn from_status(status: StatusCode, message: impl Into<String>) -> Self
pub fn from_status(status: StatusCode, message: impl Into<String>) -> Self
Create an error from an HTTP status code and message.
The status is always preserved: statuses without a dedicated
GenericErrorCode variant fall back to GenericErrorCode::Other
(code GENERIC_HTTP_<status>) rather than being degraded to 500.
Trait Implementations§
Source§impl Deref for CqrsError
impl Deref for CqrsError
Source§type Target = CqrsErrorData
type Target = CqrsErrorData
Source§fn deref(&self) -> &CqrsErrorData
fn deref(&self) -> &CqrsErrorData
Source§impl DerefMut for CqrsError
impl DerefMut for CqrsError
Source§fn deref_mut(&mut self) -> &mut CqrsErrorData
fn deref_mut(&mut self) -> &mut CqrsErrorData
Source§impl<'de> Deserialize<'de> for CqrsError
impl<'de> Deserialize<'de> for CqrsError
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Error for CqrsError
impl Error for CqrsError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()