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
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
GatewayTimeout
Implementations§
Source§impl ErrorCode
impl ErrorCode
Sourcepub fn status_code(&self) -> u16
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);Sourcepub fn title(&self) -> &'static str
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");Sourcepub fn urn_slug(&self) -> &'static str
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");Sourcepub fn urn(&self) -> String
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");Sourcepub fn from_type_uri(s: &str) -> Option<Self>
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<'de> Deserialize<'de> for ErrorCode
Available on crate features serde and std only.
impl<'de> Deserialize<'de> for ErrorCode
serde and std only.Source§fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
Source§impl Display for ErrorCode
Available on crate feature std only.In std mode the display resolves through the dynamic error_type_mode.
impl Display for ErrorCode
std only.In std mode the display resolves through the dynamic error_type_mode.
Source§impl From<ErrorCode> for StatusCode
impl From<ErrorCode> for StatusCode
Source§impl TryFrom<u16> for ErrorCode
Attempt to convert an HTTP status code (as u16) to its canonical
ErrorCode variant.
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());