pub use apollo_errors_derive::Error;
pub use miette;
pub use http;
mod catalog;
mod error;
mod ext;
mod html;
mod metadata;
mod registry;
pub use catalog::CatalogErrorEntry as ErrorMetadata;
pub use catalog::CatalogVariantEntry as ErrorVariantMetadata;
pub use catalog::error_catalog;
pub use error::Error;
pub use ext::{ErrorExt, HeapErrorExt};
pub use metadata::{
CodeCase, CodeMetadata, FieldCase, FieldMetadata as ErrorFieldMetadata, FormatConfig,
};
#[cfg(feature = "tower")]
pub mod tower_http;
#[doc(hidden)]
pub mod private {
pub use crate::error::{diagnostic_code, diagnostic_help};
pub use crate::html::HtmlEscaped;
pub use crate::metadata::*;
pub use crate::registry::*;
pub use linkme;
pub use serde_json;
pub const DEFAULT_JSONRPC_CODE: i32 = -32000;
use http::HeaderValue;
pub trait ToHeaderValue {
fn to_header_value(&self) -> Option<HeaderValue>;
}
impl ToHeaderValue for HeaderValue {
fn to_header_value(&self) -> Option<HeaderValue> {
Some(self.clone())
}
}
impl ToHeaderValue for bool {
fn to_header_value(&self) -> Option<HeaderValue> {
Some(HeaderValue::from_static(if *self {
"true"
} else {
"false"
}))
}
}
macro_rules! impl_to_header_value_for_int {
($($ty:ty),*) => {
$(
impl ToHeaderValue for $ty {
fn to_header_value(&self) -> Option<HeaderValue> {
Some(HeaderValue::from(*self))
}
}
)*
};
}
impl_to_header_value_for_int!(u16, u32, u64, i16, i32, i64);
#[inline]
pub fn http_status_from_u16(code: u16) -> http::StatusCode {
http::StatusCode::from_u16(code)
.expect("BUG: http_status value was not validated at derive macro expansion time")
}
pub trait AsDynError<'a> {
fn as_dyn_error(&self) -> &(dyn ::std::error::Error + 'a);
}
impl<'a, T: ::std::error::Error + 'a> AsDynError<'a> for T {
fn as_dyn_error(&self) -> &(dyn ::std::error::Error + 'a) {
self
}
}
impl<'a> AsDynError<'a> for dyn ::std::error::Error + 'a {
fn as_dyn_error(&self) -> &(dyn ::std::error::Error + 'a) {
self
}
}
impl<'a> AsDynError<'a> for dyn ::std::error::Error + Send + 'a {
fn as_dyn_error(&self) -> &(dyn ::std::error::Error + 'a) {
self
}
}
impl<'a> AsDynError<'a> for dyn ::std::error::Error + Send + Sync + 'a {
fn as_dyn_error(&self) -> &(dyn ::std::error::Error + 'a) {
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn http_status_from_u16_converts_correctly() {
assert_eq!(http_status_from_u16(200), http::StatusCode::OK);
assert_eq!(http_status_from_u16(201), http::StatusCode::CREATED);
assert_eq!(
http_status_from_u16(301),
http::StatusCode::MOVED_PERMANENTLY
);
assert_eq!(http_status_from_u16(400), http::StatusCode::BAD_REQUEST);
assert_eq!(http_status_from_u16(404), http::StatusCode::NOT_FOUND);
assert_eq!(
http_status_from_u16(422),
http::StatusCode::UNPROCESSABLE_ENTITY
);
assert_eq!(
http_status_from_u16(500),
http::StatusCode::INTERNAL_SERVER_ERROR
);
assert_eq!(
http_status_from_u16(503),
http::StatusCode::SERVICE_UNAVAILABLE
);
}
#[test]
#[should_panic(
expected = "BUG: http_status value was not validated at derive macro expansion time"
)]
fn http_status_from_u16_panics_on_invalid_code() {
http_status_from_u16(99);
}
}
}