1use serde::Serialize;
2use thiserror::Error;
3
4#[derive(Error, Debug)]
5pub enum IntentError {
6 #[error("Database error: {0}")]
7 DatabaseError(#[from] sqlx::Error),
8
9 #[error("IO error: {0}")]
10 IoError(#[from] std::io::Error),
11
12 #[error("Task not found: {0}")]
13 TaskNotFound(i64),
14
15 #[error("Invalid input: {0}")]
16 InvalidInput(String),
17
18 #[error("Circular dependency detected")]
19 CircularDependency,
20
21 #[error("Action not allowed: {0}")]
22 ActionNotAllowed(String),
23
24 #[error("Uncompleted children exist")]
25 UncompletedChildren,
26
27 #[error("Current directory is not an Intent-Engine project")]
28 NotAProject,
29
30 #[error("JSON serialization error: {0}")]
31 JsonError(#[from] serde_json::Error),
32}
33
34#[derive(Serialize)]
35pub struct ErrorResponse {
36 pub error: String,
37 pub code: String,
38}
39
40impl IntentError {
41 pub fn to_error_code(&self) -> &'static str {
42 match self {
43 IntentError::TaskNotFound(_) => "TASK_NOT_FOUND",
44 IntentError::DatabaseError(_) => "DATABASE_ERROR",
45 IntentError::InvalidInput(_) => "INVALID_INPUT",
46 IntentError::CircularDependency => "CIRCULAR_DEPENDENCY",
47 IntentError::ActionNotAllowed(_) => "ACTION_NOT_ALLOWED",
48 IntentError::UncompletedChildren => "UNCOMPLETED_CHILDREN",
49 IntentError::NotAProject => "NOT_A_PROJECT",
50 _ => "INTERNAL_ERROR",
51 }
52 }
53
54 pub fn to_error_response(&self) -> ErrorResponse {
55 ErrorResponse {
56 error: self.to_string(),
57 code: self.to_error_code().to_string(),
58 }
59 }
60}
61
62pub type Result<T> = std::result::Result<T, IntentError>;