use canvist_core::EditorError;
use canvist_model::JsonError;
use canvist_model::StepError;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum HeadlessError {
#[error("invalid configuration: {0}")]
Config(EditorError),
#[error("could not parse {format}: {reason}")]
Parse {
format: &'static str,
reason: String,
},
#[error("content does not fit the schema: {0}")]
Schema(String),
#[error("no command or query is named `{0}`")]
UnknownName(String),
#[error("invalid arguments for `{name}`: {reason}")]
InvalidArgs {
name: String,
reason: String,
},
#[error("invalid position: {0}")]
InvalidPosition(String),
#[error("invalid key `{0}`")]
InvalidKey(String),
#[error(transparent)]
Editor(EditorError),
#[error("invalid snapshot: {0}")]
Snapshot(String),
#[error("the document is not shared; call `share` first")]
NotShared,
#[error("the document is already shared")]
AlreadyShared,
#[cfg(feature = "collab")]
#[error(transparent)]
Collab(#[from] canvist_collab::CollabError),
#[cfg(feature = "ssr")]
#[error(transparent)]
Layout(#[from] canvist_layout::LayoutError),
#[cfg(feature = "scene")]
#[error(transparent)]
Canvas(#[from] canvist_canvas::CanvasError),
#[cfg(feature = "scene")]
#[error("invalid configuration: {0}")]
SceneConfig(#[from] canvist_scene::SceneSchemaError),
#[cfg(feature = "scene")]
#[error(transparent)]
Paint(#[from] canvist_paint::CpuError),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorCode {
Config,
Parse,
Schema,
UnknownName,
InvalidArgs,
InvalidPosition,
InvalidKey,
Editor,
Snapshot,
NotShared,
AlreadyShared,
Collab,
Render,
}
impl ErrorCode {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Config => "config",
Self::Parse => "parse",
Self::Schema => "schema",
Self::UnknownName => "unknownName",
Self::InvalidArgs => "invalidArgs",
Self::InvalidPosition => "invalidPosition",
Self::InvalidKey => "invalidKey",
Self::Editor => "editor",
Self::Snapshot => "snapshot",
Self::NotShared => "notShared",
Self::AlreadyShared => "alreadyShared",
Self::Collab => "collab",
Self::Render => "render",
}
}
}
impl std::fmt::Display for ErrorCode {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(self.as_str())
}
}
impl HeadlessError {
#[must_use]
pub fn code(&self) -> ErrorCode {
match self {
Self::Config(_) => ErrorCode::Config,
Self::Parse { .. } => ErrorCode::Parse,
Self::Schema(_) => ErrorCode::Schema,
Self::UnknownName(_) => ErrorCode::UnknownName,
Self::InvalidArgs { .. } => ErrorCode::InvalidArgs,
Self::InvalidPosition(_) => ErrorCode::InvalidPosition,
Self::InvalidKey(_) => ErrorCode::InvalidKey,
Self::Editor(_) => ErrorCode::Editor,
Self::Snapshot(_) => ErrorCode::Snapshot,
Self::NotShared => ErrorCode::NotShared,
Self::AlreadyShared => ErrorCode::AlreadyShared,
#[cfg(feature = "collab")]
Self::Collab(_) => ErrorCode::Collab,
#[cfg(feature = "ssr")]
Self::Layout(_) => ErrorCode::Render,
#[cfg(feature = "scene")]
Self::Canvas(error) => canvas_code(error),
#[cfg(feature = "scene")]
Self::SceneConfig(_) => ErrorCode::Config,
#[cfg(feature = "scene")]
Self::Paint(_) => ErrorCode::Render,
}
}
pub(crate) fn parse(format: &'static str, reason: impl std::fmt::Display) -> Self {
Self::Parse {
format,
reason: reason.to_string(),
}
}
}
impl From<EditorError> for HeadlessError {
fn from(error: EditorError) -> Self {
match error {
EditorError::UnknownName(name) => Self::UnknownName(name),
EditorError::InvalidArgs { name, reason } => Self::InvalidArgs { name, reason },
EditorError::InvalidKey(key) => Self::InvalidKey(key),
EditorError::Json(error) => error.into(),
error => Self::Editor(error),
}
}
}
impl From<JsonError> for HeadlessError {
fn from(error: JsonError) -> Self {
match error {
JsonError::Shape { .. } => Self::parse("json", error),
JsonError::UnknownType { .. } | JsonError::Step(_) => Self::Schema(error.to_string()),
}
}
}
impl From<StepError> for HeadlessError {
fn from(error: StepError) -> Self {
Self::Schema(error.to_string())
}
}
#[cfg(feature = "scene")]
fn canvas_code(error: &canvist_canvas::CanvasError) -> ErrorCode {
use canvist_canvas::CanvasError;
use canvist_scene::SceneError;
match error {
CanvasError::UnknownCommand(_) => ErrorCode::UnknownName,
CanvasError::UnknownField(_) | CanvasError::InvalidArgs(_) => ErrorCode::InvalidArgs,
CanvasError::Scene(SceneError::Json { .. }) => ErrorCode::Parse,
CanvasError::Scene(_) => ErrorCode::Schema,
CanvasError::Layout(_) => ErrorCode::Render,
}
}