Skip to main content

ErrorCode

Enum ErrorCode 

Source
pub enum ErrorCode {
Show 30 variants BadRequest, ValidationFailed, Unauthorized, InvalidCredentials, TokenExpired, TokenInvalid, Forbidden, InsufficientPermissions, OrgOutsideSubtree, AncestorRequired, CrossSubtreeAccess, ResourceNotFound, MethodNotAllowed, NotAcceptable, RequestTimeout, Conflict, ResourceAlreadyExists, Gone, PreconditionFailed, PayloadTooLarge, UnsupportedMediaType, UnprocessableEntity, PreconditionRequired, RateLimited, RequestHeaderFieldsTooLarge, InternalServerError, NotImplemented, BadGateway, ServiceUnavailable, GatewayTimeout,
}
Expand description

Machine-readable error code included in every API error response.

Serializes as a URN per RFC 9457 §3.1.1, which requires the type member to be a URI reference. Format: urn:api-bones:error:<slug> (e.g. urn:api-bones:error:resource-not-found).

§Examples

use api_bones::error::ErrorCode;

let code = ErrorCode::ResourceNotFound;
assert_eq!(code.status_code(), 404);
assert_eq!(code.title(), "Resource Not Found");
assert_eq!(code.urn_slug(), "resource-not-found");

Variants§

§

BadRequest

§

ValidationFailed

§

Unauthorized

§

InvalidCredentials

§

TokenExpired

§

TokenInvalid

§

Forbidden

§

InsufficientPermissions

§

OrgOutsideSubtree

§

AncestorRequired

§

CrossSubtreeAccess

§

ResourceNotFound

§

MethodNotAllowed

§

NotAcceptable

§

RequestTimeout

§

Conflict

§

ResourceAlreadyExists

§

Gone

§

PreconditionFailed

§

PayloadTooLarge

§

UnsupportedMediaType

§

UnprocessableEntity

§

PreconditionRequired

§

RateLimited

§

RequestHeaderFieldsTooLarge

§

InternalServerError

§

NotImplemented

§

BadGateway

§

ServiceUnavailable

§

GatewayTimeout

Implementations§

Source§

impl ErrorCode

Source

pub fn status_code(&self) -> u16

HTTP status code for this error code.

§Examples
use api_bones::error::ErrorCode;

assert_eq!(ErrorCode::BadRequest.status_code(), 400);
assert_eq!(ErrorCode::Unauthorized.status_code(), 401);
assert_eq!(ErrorCode::InternalServerError.status_code(), 500);
Source

pub fn title(&self) -> &'static str

Human-friendly title for this error code (RFC 9457 title field).

§Examples
use api_bones::error::ErrorCode;

assert_eq!(ErrorCode::ResourceNotFound.title(), "Resource Not Found");
assert_eq!(ErrorCode::BadRequest.title(), "Bad Request");
Source

pub fn urn_slug(&self) -> &'static str

The URN slug for this error code (the part after urn:api-bones:error:).

§Examples
use api_bones::error::ErrorCode;

assert_eq!(ErrorCode::ResourceNotFound.urn_slug(), "resource-not-found");
assert_eq!(ErrorCode::ValidationFailed.urn_slug(), "validation-failed");
Source

pub fn urn(&self) -> String

Full type URI for this error code per RFC 9457 §3.1.1.

The format depends on the active ErrorTypeMode (see error_type_mode):

  • URL mode: https://docs.myapp.com/errors/resource-not-found
  • URN mode: urn:myapp:error:resource-not-found

Requires the std feature (dynamic namespace resolution via error_type_mode).

§Examples
use api_bones::error::{ErrorCode, set_error_type_mode, ErrorTypeMode};

set_error_type_mode(ErrorTypeMode::Urn { namespace: "test".into() });
assert_eq!(ErrorCode::ResourceNotFound.urn(), "urn:test:error:resource-not-found");
Source

pub fn from_type_uri(s: &str) -> Option<Self>

Parse an ErrorCode from a type URI string (URL or URN format).

Requires the std feature (dynamic namespace resolution via error_type_mode).

§Examples
use api_bones::error::{ErrorCode, set_error_type_mode, ErrorTypeMode};

set_error_type_mode(ErrorTypeMode::Urn { namespace: "test".into() });
let code = ErrorCode::ResourceNotFound;
let uri = code.urn();
assert_eq!(ErrorCode::from_type_uri(&uri), Some(ErrorCode::ResourceNotFound));

Trait Implementations§

Source§

impl Clone for ErrorCode

Source§

fn clone(&self) -> ErrorCode

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 ErrorCode

Source§

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

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

impl<'de> Deserialize<'de> for ErrorCode

Available on crate features serde and std only.
Source§

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

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

impl Display for ErrorCode

Available on crate feature std only.

In std mode the display resolves through the dynamic error_type_mode.

Source§

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

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

impl From<ErrorCode> for StatusCode

Source§

fn from(code: ErrorCode) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for ErrorCode

Source§

fn eq(&self, other: &ErrorCode) -> 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 ErrorCode

Available on crate features serde and std only.
Source§

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

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

impl TryFrom<u16> for ErrorCode

Attempt to convert an HTTP status code (as u16) to its canonical ErrorCode variant.

Only 4xx and 5xx codes that have a direct mapping return Ok; all other codes (1xx, 2xx, 3xx, or unmapped 4xx/5xx) return Err(()).

§Examples

use api_bones::error::ErrorCode;

assert_eq!(ErrorCode::try_from(404_u16), Ok(ErrorCode::ResourceNotFound));
assert_eq!(ErrorCode::try_from(500_u16), Ok(ErrorCode::InternalServerError));
assert!(ErrorCode::try_from(200_u16).is_err());
assert!(ErrorCode::try_from(301_u16).is_err());
Source§

type Error = ()

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

fn try_from(status: u16) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Eq for ErrorCode

Source§

impl StructuralPartialEq for ErrorCode

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> 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> ValidateIp for T
where T: ToString,

Source§

fn validate_ipv4(&self) -> bool

Validates whether the given string is an IP V4
Source§

fn validate_ipv6(&self) -> bool

Validates whether the given string is an IP V6
Source§

fn validate_ip(&self) -> bool

Validates whether the given string is an IP
Source§

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