Skip to main content

pitchfork_cli/
error.rs

1//! Custom diagnostic error types for rich error reporting via miette.
2//!
3//! This module provides structured error types that leverage miette's diagnostic
4//! features including error codes, help text, source code highlighting, and suggestions.
5
6// False positive: fields are used in #[error] format strings and miette derive macros
7#![allow(unused_assignments)]
8
9use miette::{Diagnostic, NamedSource, SourceSpan};
10use std::io;
11use std::path::PathBuf;
12use thiserror::Error;
13
14/// Errors related to daemon ID validation.
15#[derive(Debug, Error, Diagnostic)]
16pub enum DaemonIdError {
17    #[error("daemon ID cannot be empty")]
18    #[diagnostic(
19        code(pitchfork::daemon::empty_id),
20        url("https://pitchfork.jdx.dev/configuration"),
21        help("provide a non-empty identifier for the daemon")
22    )]
23    Empty,
24
25    #[error("daemon ID {component} cannot be empty")]
26    #[diagnostic(
27        code(pitchfork::daemon::empty_component),
28        url("https://pitchfork.jdx.dev/configuration"),
29        help("both namespace and name must be non-empty")
30    )]
31    EmptyComponent { component: String },
32
33    #[error("daemon ID '{id}' contains path separator '{sep}'")]
34    #[diagnostic(
35        code(pitchfork::daemon::path_separator),
36        url("https://pitchfork.jdx.dev/configuration"),
37        help("daemon IDs cannot contain '/' or '\\' to prevent path traversal")
38    )]
39    PathSeparator { id: String, sep: char },
40
41    #[error("daemon ID '{id}' contains parent directory reference '..'")]
42    #[diagnostic(
43        code(pitchfork::daemon::parent_dir_ref),
44        url("https://pitchfork.jdx.dev/configuration"),
45        help("daemon IDs cannot contain '..' to prevent path traversal")
46    )]
47    ParentDirRef { id: String },
48
49    #[error("daemon ID '{id}' contains reserved sequence '--'")]
50    #[diagnostic(
51        code(pitchfork::daemon::reserved_sequence),
52        url("https://pitchfork.jdx.dev/configuration"),
53        help("'--' is reserved for internal path encoding; use single dashes instead")
54    )]
55    ReservedSequence { id: String },
56
57    #[error("daemon ID component '{id}' starts or ends with a dash '-'")]
58    #[diagnostic(
59        code(pitchfork::daemon::leading_trailing_dash),
60        url("https://pitchfork.jdx.dev/configuration"),
61        help(
62            "remove the leading or trailing dash (e.g. 'my-daemon' not '-my-daemon' or 'my-daemon-')"
63        )
64    )]
65    LeadingTrailingDash { id: String },
66
67    #[error("daemon ID '{id}' contains spaces")]
68    #[diagnostic(
69        code(pitchfork::daemon::contains_space),
70        url("https://pitchfork.jdx.dev/configuration"),
71        help("use hyphens or underscores instead of spaces (e.g., 'my-daemon' or 'my_daemon')")
72    )]
73    ContainsSpace { id: String },
74
75    #[error("daemon ID cannot be '.'")]
76    #[diagnostic(
77        code(pitchfork::daemon::current_dir),
78        url("https://pitchfork.jdx.dev/configuration"),
79        help("'.' refers to the current directory; use a descriptive name instead")
80    )]
81    CurrentDir,
82
83    #[error("daemon ID '{id}' contains non-printable or non-ASCII character")]
84    #[diagnostic(
85        code(pitchfork::daemon::invalid_chars),
86        url("https://pitchfork.jdx.dev/configuration"),
87        help(
88            "daemon IDs must contain only printable ASCII characters (letters, numbers, hyphens, underscores, dots)"
89        )
90    )]
91    InvalidChars { id: String },
92
93    #[error("daemon ID '{id}' is missing namespace (expected format: namespace/name)")]
94    #[diagnostic(
95        code(pitchfork::daemon::missing_namespace),
96        url("https://pitchfork.jdx.dev/configuration"),
97        help("use qualified format like 'global/myapp' or 'project-name/daemon'")
98    )]
99    MissingNamespace { id: String },
100
101    #[error("invalid safe path format '{path}' (expected namespace--name)")]
102    #[diagnostic(
103        code(pitchfork::daemon::invalid_safe_path),
104        help("safe paths use '--' to separate namespace and name")
105    )]
106    InvalidSafePath { path: String },
107}
108
109/// Errors related to daemon operations.
110#[derive(Debug, Error, Diagnostic)]
111pub enum DaemonError {
112    #[error("failed to stop daemon '{id}': {error}")]
113    #[diagnostic(
114        code(pitchfork::daemon::stop_failed),
115        help("the process may be stuck or require manual intervention. Try: kill -9 <pid>")
116    )]
117    StopFailed { id: String, error: String },
118}
119
120/// Errors related to dependency resolution.
121#[derive(Debug, Error, Diagnostic)]
122pub enum DependencyError {
123    #[error("daemon '{name}' not found in configuration")]
124    #[diagnostic(
125        code(pitchfork::deps::not_found),
126        url("https://pitchfork.jdx.dev/configuration#depends")
127    )]
128    DaemonNotFound {
129        name: String,
130        #[help]
131        suggestion: Option<String>,
132    },
133
134    #[error("daemon '{daemon}' depends on '{dependency}' which is not defined")]
135    #[diagnostic(
136        code(pitchfork::deps::missing_dependency),
137        url("https://pitchfork.jdx.dev/configuration#depends"),
138        help("add the missing daemon to your pitchfork.toml or remove it from the depends list")
139    )]
140    MissingDependency { daemon: String, dependency: String },
141
142    #[error("circular dependency detected involving: {}", involved.join(", "))]
143    #[diagnostic(
144        code(pitchfork::deps::circular),
145        url("https://pitchfork.jdx.dev/configuration#depends"),
146        help("break the cycle by removing one of the dependencies")
147    )]
148    CircularDependency {
149        /// The daemons involved in the cycle
150        involved: Vec<String>,
151    },
152}
153
154/// Errors related to port binding and availability.
155#[derive(Debug, Error, Diagnostic)]
156pub enum PortError {
157    #[error("port {port} is already in use by process '{process}' (PID: {pid})")]
158    #[diagnostic(
159        code(pitchfork::port::in_use),
160        url("https://pitchfork.jdx.dev/configuration#port"),
161        help(
162            "choose a different port, stop the existing process, or enable auto_bump_port to automatically find an available port"
163        )
164    )]
165    InUse {
166        port: u16,
167        process: String,
168        pid: u32,
169    },
170
171    #[error(
172        "could not find an available port after {attempts} attempts starting from {start_port}"
173    )]
174    #[diagnostic(
175        code(pitchfork::port::no_available_port),
176        url("https://pitchfork.jdx.dev/configuration#port"),
177        help("manually specify an available port or reduce the number of concurrent services")
178    )]
179    NoAvailablePort { start_port: u16, attempts: u32 },
180}
181
182/// Error for TOML configuration parse failures with source code highlighting.
183#[derive(Debug, Error, Diagnostic)]
184pub enum ConfigParseError {
185    #[error("failed to parse configuration")]
186    #[diagnostic(code(pitchfork::config::parse_error))]
187    TomlError {
188        /// The source file contents for display
189        #[source_code]
190        src: NamedSource<String>,
191
192        /// The location of the error in the source
193        #[label("{message}")]
194        span: SourceSpan,
195
196        /// The error message from the TOML parser
197        message: String,
198
199        /// Additional help text
200        #[help]
201        help: Option<String>,
202    },
203
204    #[error("invalid daemon name '{name}' in {}", path.display())]
205    #[diagnostic(
206        code(pitchfork::config::invalid_daemon_name),
207        url("https://pitchfork.jdx.dev/configuration"),
208        help("daemon names must be valid identifiers without spaces, '--', or special characters")
209    )]
210    InvalidDaemonName {
211        name: String,
212        path: PathBuf,
213        reason: String,
214    },
215
216    #[error(
217        "invalid dependency '{dependency}' in daemon '{daemon}' ({}): {reason}",
218        path.display()
219    )]
220    #[diagnostic(
221        code(pitchfork::config::invalid_dependency),
222        url("https://pitchfork.jdx.dev/configuration#depends"),
223        help(
224            "dependency IDs must be valid daemon IDs; use 'name' for same namespace or 'namespace/name' for cross-namespace"
225        )
226    )]
227    InvalidDependency {
228        daemon: String,
229        dependency: String,
230        path: PathBuf,
231        reason: String,
232    },
233
234    #[error(
235        "namespace collision: '{}' and '{}' both resolve to namespace '{ns}'",
236        path_a.display(),
237        path_b.display()
238    )]
239    #[diagnostic(
240        code(pitchfork::config::namespace_collision),
241        url("https://pitchfork.jdx.dev/concepts/namespaces"),
242        help(
243            "rename one of the directories so that no two project configs share the same namespace"
244        )
245    )]
246    NamespaceCollision {
247        path_a: PathBuf,
248        path_b: PathBuf,
249        ns: String,
250    },
251
252    #[error(
253        "invalid namespace '{namespace}' in {}: {reason}",
254        path.display()
255    )]
256    #[diagnostic(
257        code(pitchfork::config::invalid_namespace),
258        url("https://pitchfork.jdx.dev/concepts/namespaces"),
259        help(
260            "set a valid top-level namespace in your pitchfork.toml, e.g. namespace = \"my-project\""
261        )
262    )]
263    InvalidNamespace {
264        path: PathBuf,
265        namespace: String,
266        reason: String,
267    },
268}
269
270impl ConfigParseError {
271    /// Create a new ConfigParseError from a toml parse error
272    pub fn from_toml_error(path: &std::path::Path, contents: String, err: toml::de::Error) -> Self {
273        let message = err.message().to_string();
274
275        // Try to get span information from the TOML error
276        let span = err
277            .span()
278            .map(|r| SourceSpan::from(r.start..r.end))
279            .unwrap_or_else(|| SourceSpan::from(0..0));
280
281        Self::TomlError {
282            src: NamedSource::new(path.display().to_string(), contents),
283            span,
284            message,
285            help: Some("check TOML syntax at https://toml.io".to_string()),
286        }
287    }
288}
289
290/// Errors related to file operations (config and state files).
291#[derive(Debug, Error, Diagnostic)]
292pub enum FileError {
293    #[error("failed to read file: {}", path.display())]
294    #[diagnostic(code(pitchfork::file::read_error))]
295    ReadError {
296        path: PathBuf,
297        #[source]
298        source: io::Error,
299    },
300
301    #[error("failed to write file: {}", path.display())]
302    #[diagnostic(code(pitchfork::file::write_error))]
303    WriteError {
304        path: PathBuf,
305        #[help]
306        details: Option<String>,
307    },
308
309    #[error("failed to serialize data for file: {}", path.display())]
310    #[diagnostic(
311        code(pitchfork::file::serialize_error),
312        help("this is likely an internal error; please report it")
313    )]
314    SerializeError {
315        path: PathBuf,
316        #[source]
317        source: toml::ser::Error,
318    },
319
320    #[error("no file path specified")]
321    #[diagnostic(
322        code(pitchfork::file::no_path),
323        help("ensure a pitchfork.toml file exists in your project or specify a path")
324    )]
325    NoPath,
326}
327
328/// Errors related to IPC communication with the supervisor.
329#[derive(Debug, Error, Diagnostic)]
330pub enum IpcError {
331    #[error("failed to connect to supervisor after {attempts} attempts")]
332    #[diagnostic(
333        code(pitchfork::ipc::connection_failed),
334        url("https://pitchfork.jdx.dev/supervisor")
335    )]
336    ConnectionFailed {
337        attempts: u32,
338        #[source]
339        source: Option<io::Error>,
340        #[help]
341        help: String,
342    },
343
344    #[error("IPC request timed out after {seconds}s")]
345    #[diagnostic(
346        code(pitchfork::ipc::timeout),
347        url("https://pitchfork.jdx.dev/supervisor"),
348        help(
349            "the supervisor may be unresponsive or overloaded.\nCheck supervisor status: pitchfork supervisor status\nView logs: pitchfork logs"
350        )
351    )]
352    Timeout { seconds: u64 },
353
354    #[error("IPC connection closed unexpectedly")]
355    #[diagnostic(
356        code(pitchfork::ipc::connection_closed),
357        url("https://pitchfork.jdx.dev/supervisor"),
358        help(
359            "the supervisor may have crashed or been stopped.\nRestart with: pitchfork supervisor start"
360        )
361    )]
362    ConnectionClosed,
363
364    #[error("failed to read IPC response")]
365    #[diagnostic(code(pitchfork::ipc::read_failed))]
366    ReadFailed {
367        #[source]
368        source: io::Error,
369    },
370
371    #[error("failed to send IPC request")]
372    #[diagnostic(code(pitchfork::ipc::send_failed))]
373    SendFailed {
374        #[source]
375        source: io::Error,
376    },
377
378    #[error("unexpected response from supervisor: expected {expected}, got {actual}")]
379    #[diagnostic(
380        code(pitchfork::ipc::unexpected_response),
381        help("this may indicate a version mismatch between the CLI and supervisor")
382    )]
383    UnexpectedResponse { expected: String, actual: String },
384
385    #[error("IPC message is invalid: {reason}")]
386    #[diagnostic(code(pitchfork::ipc::invalid_message))]
387    InvalidMessage { reason: String },
388}
389
390/// A collection of multiple errors that occurred during validation or processing.
391///
392/// This is useful when you want to collect and report all validation errors at once
393/// instead of failing on the first error.
394#[derive(Debug, Error, Diagnostic)]
395#[error("multiple errors occurred ({} total)", errors.len())]
396#[diagnostic(code(pitchfork::multiple_errors))]
397#[allow(dead_code)]
398pub struct MultipleErrors {
399    #[related]
400    pub errors: Vec<Box<dyn Diagnostic + Send + Sync + 'static>>,
401}
402
403#[allow(dead_code)]
404impl MultipleErrors {
405    /// Create a new MultipleErrors from a vector of diagnostics
406    pub fn new(errors: Vec<Box<dyn Diagnostic + Send + Sync + 'static>>) -> Self {
407        Self { errors }
408    }
409
410    /// Returns true if there are no errors
411    pub fn is_empty(&self) -> bool {
412        self.errors.is_empty()
413    }
414
415    /// Returns the number of errors
416    pub fn len(&self) -> usize {
417        self.errors.len()
418    }
419}
420
421/// Find the most similar daemon name for suggestions.
422pub fn find_similar_daemon<'a>(
423    name: &str,
424    available: impl Iterator<Item = &'a str>,
425) -> Option<String> {
426    use fuzzy_matcher::FuzzyMatcher;
427    use fuzzy_matcher::skim::SkimMatcherV2;
428
429    let matcher = SkimMatcherV2::default();
430    available
431        .filter_map(|candidate| {
432            matcher
433                .fuzzy_match(candidate, name)
434                .map(|score| (candidate, score))
435        })
436        .max_by_key(|(_, score)| *score)
437        .filter(|(_, score)| *score > 0)
438        .map(|(candidate, _)| format!("did you mean '{candidate}'?"))
439}
440
441#[cfg(test)]
442mod tests {
443    use super::*;
444
445    #[test]
446    fn test_daemon_id_error_display() {
447        let err = DaemonIdError::Empty;
448        assert_eq!(err.to_string(), "daemon ID cannot be empty");
449
450        let err = DaemonIdError::PathSeparator {
451            id: "foo/bar".to_string(),
452            sep: '/',
453        };
454        assert_eq!(
455            err.to_string(),
456            "daemon ID 'foo/bar' contains path separator '/'"
457        );
458
459        let err = DaemonIdError::ContainsSpace {
460            id: "my app".to_string(),
461        };
462        assert_eq!(err.to_string(), "daemon ID 'my app' contains spaces");
463    }
464
465    #[test]
466    fn test_dependency_error_display() {
467        let err = DependencyError::DaemonNotFound {
468            name: "postgres".to_string(),
469            suggestion: None,
470        };
471        assert_eq!(
472            err.to_string(),
473            "daemon 'postgres' not found in configuration"
474        );
475
476        let err = DependencyError::MissingDependency {
477            daemon: "api".to_string(),
478            dependency: "db".to_string(),
479        };
480        assert_eq!(
481            err.to_string(),
482            "daemon 'api' depends on 'db' which is not defined"
483        );
484
485        let err = DependencyError::CircularDependency {
486            involved: vec!["a".to_string(), "b".to_string(), "c".to_string()],
487        };
488        assert!(err.to_string().contains("circular dependency"));
489        assert!(err.to_string().contains("a, b, c"));
490    }
491
492    #[test]
493    fn test_find_similar_daemon() {
494        let daemons = ["postgres", "redis", "api", "worker"];
495
496        // Close match
497        let suggestion = find_similar_daemon("postgre", daemons.iter().copied());
498        assert_eq!(suggestion, Some("did you mean 'postgres'?".to_string()));
499
500        // No reasonable match
501        let suggestion = find_similar_daemon("xyz123", daemons.iter().copied());
502        assert!(suggestion.is_none());
503    }
504
505    #[test]
506    fn test_file_error_display() {
507        let err = FileError::ReadError {
508            path: PathBuf::from("/path/to/config.toml"),
509            source: io::Error::new(io::ErrorKind::NotFound, "file not found"),
510        };
511        assert!(err.to_string().contains("failed to read file"));
512        assert!(err.to_string().contains("config.toml"));
513
514        let err = FileError::NoPath;
515        assert!(err.to_string().contains("no file path"));
516    }
517
518    #[test]
519    fn test_ipc_error_display() {
520        let err = IpcError::ConnectionFailed {
521            attempts: 5,
522            source: None,
523            help: "ensure the supervisor is running".to_string(),
524        };
525        assert!(err.to_string().contains("failed to connect"));
526        assert!(err.to_string().contains("5 attempts"));
527
528        let err = IpcError::Timeout { seconds: 30 };
529        assert!(err.to_string().contains("timed out"));
530        assert!(err.to_string().contains("30s"));
531
532        let err = IpcError::UnexpectedResponse {
533            expected: "Ok".to_string(),
534            actual: "Error".to_string(),
535        };
536        assert!(err.to_string().contains("unexpected response"));
537        assert!(err.to_string().contains("Ok"));
538        assert!(err.to_string().contains("Error"));
539    }
540
541    #[test]
542    fn test_config_parse_error() {
543        let contents = "[daemons.test]\nrun = ".to_string();
544        let err = toml::from_str::<toml::Value>(&contents).unwrap_err();
545        let parse_err =
546            ConfigParseError::from_toml_error(std::path::Path::new("test.toml"), contents, err);
547
548        assert!(parse_err.to_string().contains("failed to parse"));
549    }
550
551    #[test]
552    fn test_multiple_errors() {
553        let errors: Vec<Box<dyn Diagnostic + Send + Sync>> = vec![
554            Box::new(DaemonIdError::Empty),
555            Box::new(DaemonIdError::CurrentDir),
556        ];
557        let multi = MultipleErrors::new(errors);
558
559        assert_eq!(multi.len(), 2);
560        assert!(!multi.is_empty());
561        assert!(multi.to_string().contains("2 total"));
562    }
563}