use core::fmt;
#[derive(Clone, Debug, Default)]
pub struct SchemaError {
errors: Vec<gql::GraphQLError>,
}
impl SchemaError {
#[must_use]
pub fn new(message: impl Into<String>) -> Self {
Self {
errors: vec![gql::GraphQLError::new(message.into())],
}
}
#[must_use]
pub fn from_errors(errors: Vec<gql::GraphQLError>) -> Self {
Self { errors }
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.errors.is_empty()
}
#[must_use]
pub fn len(&self) -> usize {
self.errors.len()
}
pub fn errors(&self) -> &[gql::GraphQLError] {
&self.errors
}
#[must_use]
pub fn into_errors(self) -> Vec<gql::GraphQLError> {
self.errors
}
}
impl fmt::Display for SchemaError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (index, error) in self.errors.iter().enumerate() {
if index != 0 {
f.write_str("\n")?;
}
write!(f, "{error}")?;
}
Ok(())
}
}
impl std::error::Error for SchemaError {}
impl From<gql::SchemaError> for SchemaError {
fn from(value: gql::SchemaError) -> Self {
Self {
errors: value.into_errors(),
}
}
}
impl From<gql::GraphQLError> for SchemaError {
fn from(value: gql::GraphQLError) -> Self {
Self {
errors: vec![value],
}
}
}