pub struct OpError {
pub kind: ErrorKind,
pub code: ErrorCode,
pub message: String,
pub details: Option<Value>,
pub remediation: Option<Remediation>,
}Expand description
Operation error inside a response envelope.
code is ErrorCode. remediation, when set, is Remediation.
The wire object always has the keys kind, code, message, details,
and suggested_action. details and suggested_action are JSON null
when absent. suggested_action is a JSON string when a recoverable
remediation has a non-empty first step, never the tagged Remediation
object.
A multi-step Remediation::Recoverable keeps that first step on the
wire. An empty first step writes JSON null. Remediation::NotRecoverable
writes JSON null and copies its justification into details under
justification, so an agent does not execute it as a recovery step.
Those two values do not round-trip as the same Rust variant. A single-step
recoverable remediation with a non-empty step does.
[Display] prints {code}: {message}. The type also implements
std::error::Error.
Fields§
§kind: ErrorKindCoarse class. Selects the HTTP status.
code: ErrorCodeStable machine-readable code.
message: StringHuman-readable summary.
details: Option<Value>Structured context. None is JSON null on the wire.
remediation: Option<Remediation>Structured remediation. None is JSON null under suggested_action.
Implementations§
Source§impl OpError
impl OpError
Sourcepub fn new(kind: ErrorKind, code: ErrorCode, message: impl Into<String>) -> Self
pub fn new(kind: ErrorKind, code: ErrorCode, message: impl Into<String>) -> Self
Builds an error with no details and no remediation.
Sourcepub fn from_code(
kind: ErrorKind,
code: &'static str,
message: impl Into<String>,
) -> Self
pub fn from_code( kind: ErrorKind, code: &'static str, message: impl Into<String>, ) -> Self
Builds an error from a stable code string, with no details and no remediation.
code is stored as ErrorCode. A caller that must not depend on
sc-observability-types uses this constructor instead of Self::new.
§Examples
use sc_command::{ErrorKind, OpError};
let error = OpError::from_code(
ErrorKind::Unavailable,
"RUNTIME.REQUEST_TIMEOUT",
"timed out",
);
assert_eq!(error.code.as_str(), "RUNTIME.REQUEST_TIMEOUT");
assert_eq!(error.kind, ErrorKind::Unavailable);Sourcepub fn with_details(self, details: Value) -> Self
pub fn with_details(self, details: Value) -> Self
Attaches structured details.
Sourcepub fn with_suggestion(self, suggestion: impl Into<String>) -> Self
pub fn with_suggestion(self, suggestion: impl Into<String>) -> Self
Attaches one recoverable suggestion.
The text is stored as Remediation::recoverable and written on the
wire as a JSON string when it is non-empty.
Sourcepub fn with_remediation(self, remediation: Remediation) -> Self
pub fn with_remediation(self, remediation: Remediation) -> Self
Attaches remediation from sc-observability-types.
Sourcepub fn suggested_action(&self) -> Option<&str>
pub fn suggested_action(&self) -> Option<&str>
Wire text of suggested_action when remediation projects to a
non-empty recovery step.
Sourcepub fn from_transport_strings(
code: &str,
endpoint: &str,
cause: &str,
suggested_action: Option<&str>,
) -> Self
pub fn from_transport_strings( code: &str, endpoint: &str, cause: &str, suggested_action: Option<&str>, ) -> Self
Maps the plain strings a transport failure carries into this error.
code, endpoint, and cause are the strings the transport error
exposes. suggested_action is the caller’s already-rendered recovery
text. This crate does not depend on the transport crate, and it does
not invent an application name or a daemon verb.
The code DAEMON.NOT_RUNNING selects ErrorKind::Unavailable and
the message the daemon is not running. Any other code selects
ErrorKind::Internal and the message
transport failure {code} at {endpoint}: {cause}. endpoint and
cause are stored in details for every code. None, or text that
is empty after trimming, leaves remediation unset.
§Examples
use sc_command::{ErrorKind, OpError};
let error = OpError::from_transport_strings(
"DAEMON.NOT_RUNNING",
"uds:app.sock",
"connection refused",
Some("run my-app daemon start"),
);
assert_eq!(error.kind, ErrorKind::Unavailable);
assert_eq!(error.code.as_str(), "DAEMON.NOT_RUNNING");
assert_eq!(error.suggested_action(), Some("run my-app daemon start"));Trait Implementations§
Source§impl<'de> Deserialize<'de> for OpError
impl<'de> Deserialize<'de> for OpError
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 OpError
impl Error for OpError
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()