Skip to main content

OpError

Struct OpError 

Source
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: ErrorKind

Coarse class. Selects the HTTP status.

§code: ErrorCode

Stable machine-readable code.

§message: String

Human-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

Source

pub fn new(kind: ErrorKind, code: ErrorCode, message: impl Into<String>) -> Self

Builds an error with no details and no remediation.

Source

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);
Source

pub fn with_details(self, details: Value) -> Self

Attaches structured details.

Source

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.

Source

pub fn with_remediation(self, remediation: Remediation) -> Self

Attaches remediation from sc-observability-types.

Source

pub fn suggested_action(&self) -> Option<&str>

Wire text of suggested_action when remediation projects to a non-empty recovery step.

Source

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 Clone for OpError

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for OpError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for OpError

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for OpError

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for OpError

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl PartialEq for OpError

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for OpError

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for OpError

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Observable for T
where T: Send + Sync + 'static,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.