stratum-node-core 0.1.0-alpha.4

Zero-I/O core types for Stratum pod nodes: did:nostr principals, typed identifiers, a strict NIP-98 signed-request verifier and bootstrap claim bodies
Documentation
//! The error codes Steps 3a and 3b add to the JSON error shape, with their
//! HTTP statuses.

use std::fmt;

/// An error code and the HTTP status it is sent with.
///
/// Every Stratum error response is `{"error":{"code","message"}}`. The codes
/// defined before Step 3a are written by the crate that sends them. This
/// type gives the codes Steps 3a and 3b add one definition, shared by the
/// controller, the gate, the proxy, the signer and the runner that send
/// them and the client and the job library that read them. Signer-internal
/// codes (`stale_revision`, `no_recipients`) and outcomes that are never an
/// HTTP answer (`limits_not_applied`, `keys_inconsistent`) are not here.
///
/// # Examples
///
/// ```
/// use stratum_node_core::ErrorCode;
///
/// assert_eq!(ErrorCode::SUSPENDED.as_str(), "suspended");
/// assert_eq!(ErrorCode::SUSPENDED.status(), 403);
/// assert_eq!(ErrorCode::from_code("archive_required"), Some(ErrorCode::ARCHIVE_REQUIRED));
/// assert_eq!(ErrorCode::from_code("conflict"), None);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ErrorCode {
    code: &'static str,
    status: u16,
}

impl ErrorCode {
    /// `suspended`, 403: the requester, or the owner of the pod in the path,
    /// is suspended. Also the answer to `redeem` and `confirm` by a
    /// suspended key, decided before any stored response is looked up.
    ///
    /// # Examples
    ///
    /// ```
    /// use stratum_node_core::ErrorCode;
    ///
    /// assert_eq!(ErrorCode::SUSPENDED.to_string(), "suspended");
    /// ```
    pub const SUSPENDED: Self = Self {
        code: "suspended",
        status: 403,
    };

    /// `archive_required`, 409: `remove` requires an archive, and none was
    /// taken for the member's current generation and suspension number.
    ///
    /// # Examples
    ///
    /// ```
    /// use stratum_node_core::ErrorCode;
    ///
    /// assert_eq!(ErrorCode::ARCHIVE_REQUIRED.status(), 409);
    /// ```
    pub const ARCHIVE_REQUIRED: Self = Self {
        code: "archive_required",
        status: 409,
    };

    /// `pod_data_present`, 409: an invitation or redemption for a `removed`
    /// or `cancelled` key whose old pod data has not been purged.
    ///
    /// # Examples
    ///
    /// ```
    /// use stratum_node_core::ErrorCode;
    ///
    /// assert_eq!(ErrorCode::POD_DATA_PRESENT.as_str(), "pod_data_present");
    /// ```
    pub const POD_DATA_PRESENT: Self = Self {
        code: "pod_data_present",
        status: 409,
    };

    /// `app_forbidden`, 403: an application key used on a route, listener or
    /// pod it may not reach, or a member key sent to the gate's proxy
    /// listener.
    pub const APP_FORBIDDEN: Self = Self {
        code: "app_forbidden",
        status: 403,
    };

    /// `rate_limited`, 429: a principal, all application keys together, or
    /// the whole node exceeded its signed-pod-request rate limit.
    pub const RATE_LIMITED: Self = Self {
        code: "rate_limited",
        status: 429,
    };

    /// `lease_revoked`, 403: the lease was revoked by withdrawal, suspension, application disable or removal. A job exits at once.
    pub const LEASE_REVOKED: Self = Self {
        code: "lease_revoked",
        status: 403,
    };

    /// `outside_grant`, 403: the request's method or path is outside the installation's grant, or the path is an ACL.
    pub const OUTSIDE_GRANT: Self = Self {
        code: "outside_grant",
        status: 403,
    };

    /// `lease_starting`, 503: the lease is claimed but not yet started: the runner has not verified the container's limits or the signing window is not open. A job retries.
    pub const LEASE_STARTING: Self = Self {
        code: "lease_starting",
        status: 503,
    };

    /// `lease_expired`, 503: the proxy holds no live lease view: the controller has not answered its poll within the 300 s view lease. A job retries until its cut-off.
    pub const LEASE_EXPIRED: Self = Self {
        code: "lease_expired",
        status: 503,
    };

    /// `busy`, 503: the proxy (or a signer socket) is at its concurrency limit. A job retries.
    pub const BUSY: Self = Self {
        code: "busy",
        status: 503,
    };

    /// `pod_auth_refused`, 503: the pod server answered 401, which the proxy relays as retryable: its replay cache is full. A job retries.
    pub const POD_AUTH_REFUSED: Self = Self {
        code: "pod_auth_refused",
        status: 503,
    };

    /// `key_disabled`, 403: the signer refuses the key: it is disabled, destroyed, unknown or the node key.
    pub const KEY_DISABLED: Self = Self {
        code: "key_disabled",
        status: 403,
    };

    /// `no_window`, 403: the signer has no signing window open for this key and lease.
    pub const NO_WINDOW: Self = Self {
        code: "no_window",
        status: 403,
    };

    /// `outside_pod`, 403: the signer refuses a URL that is not the key's origin followed by a non-ACL path in its owner's pod.
    pub const OUTSIDE_POD: Self = Self {
        code: "outside_pod",
        status: 403,
    };

    /// `payload_rule`, 400: the signer refuses a `payload_sha256` that is malformed, present without a body or absent with one.
    pub const PAYLOAD_RULE: Self = Self {
        code: "payload_rule",
        status: 400,
    };

    /// `application_unavailable`, 409: an installation names an application that is not `approved`.
    pub const APPLICATION_UNAVAILABLE: Self = Self {
        code: "application_unavailable",
        status: 409,
    };

    /// `image_has_volumes`, 409: the image's configuration declares a volume, refused at approval and again at create.
    pub const IMAGE_HAS_VOLUMES: Self = Self {
        code: "image_has_volumes",
        status: 409,
    };

    /// `image_absent`, 409: the runner reports the image ID absent from the tenant daemon.
    pub const IMAGE_ABSENT: Self = Self {
        code: "image_absent",
        status: 409,
    };

    /// `workspace_unavailable`, 409: an installation asks for a workspace and no slot is available; always the answer to a workspace mode other than `none` in Step 3b-2.
    pub const WORKSPACE_UNAVAILABLE: Self = Self {
        code: "workspace_unavailable",
        status: 409,
    };

    /// `container_identity_conflict`, 409: the runner found a container with the lease's name whose labels or effective profile differ from the request; it neither starts, stops nor removes it.
    pub const CONTAINER_IDENTITY_CONFLICT: Self = Self {
        code: "container_identity_conflict",
        status: 409,
    };

    /// `create_expired`, 409: the runner received a create after the controller's monotonic `not_after` and did not begin it.
    pub const CREATE_EXPIRED: Self = Self {
        code: "create_expired",
        status: 409,
    };

    /// `runner_unavailable`, 503: the controller could not complete a step
    /// that needs the runner (an application's approval queries the image
    /// on the tenant daemon): no runner is configured, the runner did not
    /// answer, or it refused. Nothing was stored; the request can be sent
    /// again once the runner answers.
    ///
    /// # Examples
    ///
    /// ```
    /// use stratum_node_core::ErrorCode;
    ///
    /// assert_eq!(ErrorCode::RUNNER_UNAVAILABLE.as_str(), "runner_unavailable");
    /// assert_eq!(ErrorCode::RUNNER_UNAVAILABLE.status(), 503);
    /// ```
    pub const RUNNER_UNAVAILABLE: Self = Self {
        code: "runner_unavailable",
        status: 503,
    };

    /// `signer_unavailable`, 503: an installation's create or consent was
    /// committed but the signer did not finish its step (making the
    /// application key, or confirming it enabled). The commit stays;
    /// sending the same request again, with the same operation ID, finishes
    /// it and answers once the signer has.
    ///
    /// # Examples
    ///
    /// ```
    /// use stratum_node_core::ErrorCode;
    ///
    /// assert_eq!(ErrorCode::SIGNER_UNAVAILABLE.as_str(), "signer_unavailable");
    /// assert_eq!(ErrorCode::SIGNER_UNAVAILABLE.status(), 503);
    /// ```
    pub const SIGNER_UNAVAILABLE: Self = Self {
        code: "signer_unavailable",
        status: 503,
    };

    /// Every code this type defines.
    ///
    /// # Examples
    ///
    /// ```
    /// use stratum_node_core::ErrorCode;
    ///
    /// assert!(ErrorCode::ALL.iter().all(|c| ErrorCode::from_code(c.as_str()) == Some(*c)));
    /// ```
    pub const ALL: [Self; 23] = [
        Self::SUSPENDED,
        Self::ARCHIVE_REQUIRED,
        Self::POD_DATA_PRESENT,
        Self::APP_FORBIDDEN,
        Self::RATE_LIMITED,
        Self::LEASE_REVOKED,
        Self::OUTSIDE_GRANT,
        Self::LEASE_STARTING,
        Self::LEASE_EXPIRED,
        Self::BUSY,
        Self::POD_AUTH_REFUSED,
        Self::KEY_DISABLED,
        Self::NO_WINDOW,
        Self::OUTSIDE_POD,
        Self::PAYLOAD_RULE,
        Self::APPLICATION_UNAVAILABLE,
        Self::IMAGE_HAS_VOLUMES,
        Self::IMAGE_ABSENT,
        Self::WORKSPACE_UNAVAILABLE,
        Self::CONTAINER_IDENTITY_CONFLICT,
        Self::CREATE_EXPIRED,
        Self::RUNNER_UNAVAILABLE,
        Self::SIGNER_UNAVAILABLE,
    ];

    /// Returns the code as it appears in `error.code`.
    ///
    /// # Examples
    ///
    /// ```
    /// use stratum_node_core::ErrorCode;
    ///
    /// assert_eq!(ErrorCode::POD_DATA_PRESENT.as_str(), "pod_data_present");
    /// ```
    pub const fn as_str(self) -> &'static str {
        self.code
    }

    /// Returns the HTTP status the code is sent with.
    ///
    /// # Examples
    ///
    /// ```
    /// use stratum_node_core::ErrorCode;
    ///
    /// assert_eq!(ErrorCode::ARCHIVE_REQUIRED.status(), 409);
    /// ```
    pub const fn status(self) -> u16 {
        self.status
    }

    /// Returns the code named `code`, if this type defines it. The match is
    /// exact and case-sensitive.
    ///
    /// # Examples
    ///
    /// ```
    /// use stratum_node_core::ErrorCode;
    ///
    /// assert_eq!(ErrorCode::from_code("suspended"), Some(ErrorCode::SUSPENDED));
    /// assert_eq!(ErrorCode::from_code("Suspended"), None);
    /// ```
    pub fn from_code(code: &str) -> Option<Self> {
        Self::ALL.into_iter().find(|known| known.code == code)
    }
}

impl fmt::Display for ErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.code)
    }
}