Skip to main content

bashkit/
error.rs

1//! Error types for Bashkit
2//!
3//! This module provides error types for the interpreter with the following design goals:
4//! - Human-readable error messages for users
5//! - No leakage of sensitive information (paths, memory addresses, secrets)
6//! - Clear categorization for programmatic handling
7
8use crate::limits::LimitExceeded;
9use thiserror::Error;
10
11/// Result type alias using Bashkit's Error.
12pub type Result<T> = std::result::Result<T, Error>;
13
14/// Bashkit error types.
15///
16/// All error messages are designed to be safe for display to end users without
17/// exposing internal details or sensitive information.
18#[derive(Error, Debug)]
19pub enum Error {
20    /// Parse error occurred while parsing the script (without location info).
21    #[error("parse error: {0}")]
22    Parse(String),
23
24    /// Parse error with source location information.
25    #[error("parse error at line {line}, column {column}: {message}")]
26    ParseAt {
27        message: String,
28        line: usize,
29        column: usize,
30    },
31
32    /// Execution error occurred while running the script.
33    #[error("execution error: {0}")]
34    Execution(String),
35
36    /// I/O error from filesystem operations.
37    #[error("io error: {0}")]
38    Io(#[from] std::io::Error),
39
40    /// Command not found.
41    #[error("command not found: {0}")]
42    CommandNotFound(String),
43
44    /// Resource limit exceeded.
45    #[error("resource limit exceeded: {0}")]
46    ResourceLimit(#[from] LimitExceeded),
47
48    /// Network error.
49    #[error("network error: {0}")]
50    Network(String),
51
52    /// Internal error for unexpected failures.
53    ///
54    /// THREAT[TM-INT-002]: Unexpected internal failures should not crash the interpreter.
55    /// This error type provides a human-readable message without exposing:
56    /// - Stack traces
57    /// - Memory addresses
58    /// - Internal file paths
59    /// - Panic messages that may contain sensitive data
60    ///
61    /// Use this for:
62    /// - Recovered panics that need to abort execution
63    /// - Logic errors that indicate a bug
64    /// - Security-sensitive failures where details should not be exposed
65    #[error("internal error: {0}")]
66    Internal(String),
67}
68
69impl Error {
70    /// Create a parse error with source location.
71    pub fn parse_at(message: impl Into<String>, line: usize, column: usize) -> Self {
72        Self::ParseAt {
73            message: message.into(),
74            line,
75            column,
76        }
77    }
78}