AppCode

Struct AppCode 

Source
#[non_exhaustive]
pub struct AppCode { /* private fields */ }
Expand description

Stable machine-readable error code exposed to clients.

Values are serialized as SCREAMING_SNAKE_CASE strings (e.g., "NOT_FOUND"). This type is part of the public wire contract and supports both built-in constants and caller-defined codes created via AppCode::new or AppCode::try_new.

Design rules:

  • Keep the set small and meaningful.
  • Prefer adding new variants over overloading existing ones.
  • Do not encode private/internal details in codes.
  • Validate custom codes using AppCode::try_new before exposing them publicly.

Implementations§

Source§

impl AppCode

Source

pub const NotFound: Self

Machine code emitted when a resource is not found.

Source

pub const Validation: Self

Machine code emitted when validation fails.

Source

pub const Conflict: Self

Machine code emitted when a conflict is detected.

Source

pub const UserAlreadyExists: Self

Machine code emitted when attempting to create an existing user.

Source

pub const Unauthorized: Self

Machine code emitted when authentication fails or is required.

Source

pub const Forbidden: Self

Machine code emitted when an operation is not permitted.

Source

pub const NotImplemented: Self

Machine code emitted when functionality is missing.

Source

pub const BadRequest: Self

Machine code emitted when a request is malformed.

Source

pub const RateLimited: Self

Machine code emitted when a caller is throttled.

Source

pub const TelegramAuth: Self

Machine code emitted when Telegram authentication fails.

Source

pub const InvalidJwt: Self

Machine code emitted when a JWT token is invalid.

Source

pub const Internal: Self

Machine code emitted for internal server failures.

Source

pub const Database: Self

Machine code emitted for database-related issues.

Source

pub const Service: Self

Machine code emitted for service-layer failures.

Source

pub const Config: Self

Machine code emitted for configuration issues.

Source

pub const Turnkey: Self

Machine code emitted for Turnkey integration failures.

Source

pub const Timeout: Self

Machine code emitted for timeout failures.

Source

pub const Network: Self

Machine code emitted for network issues.

Source

pub const DependencyUnavailable: Self

Machine code emitted when dependencies are unavailable.

Source

pub const Serialization: Self

Machine code emitted for serialization failures.

Source

pub const Deserialization: Self

Machine code emitted for deserialization failures.

Source

pub const ExternalApi: Self

Machine code emitted when an external API fails.

Source

pub const Queue: Self

Machine code emitted for queue processing errors.

Source

pub const Cache: Self

Machine code emitted for cache subsystem failures.

Source

pub const fn new(code: &'static str) -> Self

Construct an AppCode from a compile-time string literal.

§Examples
use masterror::AppCode;

let code = AppCode::new("INVALID_JSON");
assert_eq!(code.as_str(), "INVALID_JSON");
§Panics

Panics when the literal is not SCREAMING_SNAKE_CASE. Use AppCode::try_new to validate dynamic strings at runtime.

Source

pub fn try_new(code: impl Into<String>) -> Result<Self, ParseAppCodeError>

Construct an AppCode from a dynamically provided string.

The input must be SCREAMING_SNAKE_CASE. This constructor allocates to own the string, making it suitable for runtime-defined codes.

§Errors

Returns ParseAppCodeError when the string is empty or contains characters outside of A-Z, 0-9, and _.

§Examples
use masterror::AppCode;

let code = AppCode::try_new(String::from("THIRD_PARTY_FAILURE"))?;
assert_eq!(code.as_str(), "THIRD_PARTY_FAILURE");
Source

pub fn as_str(&self) -> &str

Get the canonical string form of this code (SCREAMING_SNAKE_CASE).

This matches the JSON serialization.

Trait Implementations§

Source§

impl Clone for AppCode

Source§

fn clone(&self) -> AppCode

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for AppCode

Source§

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

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

impl<'de> Deserialize<'de> for AppCode

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 AppCode

Source§

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

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

impl From<AppErrorKind> for AppCode

Source§

fn from(kind: AppErrorKind) -> Self

Map internal taxonomy (AppErrorKind) to public machine code (AppCode).

The mapping is 1:1 today and intentionally conservative.

Source§

impl FromStr for AppCode

Parse an AppCode from its canonical string representation.

§Errors

Returns ParseAppCodeError when the input is not SCREAMING_SNAKE_CASE.

§Examples

use std::str::FromStr;

use masterror::{AppCode, ParseAppCodeError};

let code = AppCode::from_str("NOT_FOUND")?;
assert_eq!(code, AppCode::NotFound);
Source§

type Err = ParseAppCodeError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for AppCode

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for AppCode

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for AppCode

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 Eq for AppCode

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> 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> Same for T

Source§

type Output = T

Should always be Self
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

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