Skip to main content

bnto_core/
errors.rs

1// =============================================================================
2// Error Types
3// =============================================================================
4//
5// All error types for the Bnto WASM engine. When something goes wrong during
6// node execution (bad input, unsupported format, etc.), we return one of
7// these errors instead of crashing.
8
9use thiserror::Error;
10
11/// All possible errors from Bnto WASM node operations.
12///
13/// Each variant represents a different category of failure.
14/// The `#[error("...")]` attribute defines the display message.
15#[derive(Debug, Error)]
16pub enum BntoError {
17    // --- Input Validation Errors ---
18    /// The input data is invalid (wrong type, missing required field, etc.).
19    /// The string contains a human-readable explanation of what's wrong.
20    #[error("Invalid input: {0}")]
21    InvalidInput(String),
22
23    /// The file format isn't supported by this node.
24    /// For example, trying to compress a .bmp when we only support JPEG/PNG/WebP.
25    #[error("Unsupported format: {0}")]
26    UnsupportedFormat(String),
27
28    // --- Processing Errors ---
29    /// Something went wrong while processing the file.
30    /// This is a catch-all for errors during the actual work (compression,
31    /// CSV parsing, etc.). The string explains what happened.
32    #[error("Processing failed: {0}")]
33    ProcessingFailed(String),
34
35    /// The user cancelled the operation (or the Web Worker was terminated).
36    /// Not a "real" error — the user chose to stop.
37    #[error("Operation cancelled")]
38    Cancelled,
39
40    // --- Resource Errors ---
41    /// A file is too large for browser processing.
42    /// Browser memory is limited (~2GB), so we check file sizes before
43    /// trying to process them.
44    #[error("File too large: {0} bytes")]
45    FileTooLarge(u64),
46
47    /// The browser ran out of memory while processing.
48    /// WASM has a limited memory heap. If we try to load a huge image,
49    /// we might exhaust it.
50    #[error("Out of memory: {0}")]
51    OutOfMemory(String),
52}
53
54// =============================================================================
55// Converting BntoError to JavaScript (MOVED TO bnto-wasm)
56// =============================================================================
57//
58// NOTE: The `impl From<BntoError> for JsValue` conversion used to live here,
59// but it coupled bnto-core to WASM dependencies (wasm-bindgen, js-sys).
60// That forced every node crate to carry WASM deps even for pure Rust logic.
61//
62// The conversion now lives in `bnto-wasm/src/lib.rs` — the only crate that
63// should know about JavaScript types. This keeps bnto-core target-agnostic:
64// it works in WASM, native CLI, desktop (Tauri), or anywhere else.
65
66// =============================================================================
67// Tests
68// =============================================================================
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn test_invalid_input_error_message() {
76        // Create an error and check its message.
77        let error = BntoError::InvalidInput("missing file data".to_string());
78
79        // `.to_string()` triggers the `#[error("...")]` formatting.
80        let msg = error.to_string();
81
82        assert_eq!(msg, "Invalid input: missing file data");
83    }
84
85    #[test]
86    fn test_unsupported_format_error_message() {
87        let error = BntoError::UnsupportedFormat("BMP".to_string());
88        assert_eq!(error.to_string(), "Unsupported format: BMP");
89    }
90
91    #[test]
92    fn test_processing_failed_error_message() {
93        let error = BntoError::ProcessingFailed("decoder returned invalid data".to_string());
94        assert_eq!(
95            error.to_string(),
96            "Processing failed: decoder returned invalid data"
97        );
98    }
99
100    #[test]
101    fn test_cancelled_error_message() {
102        let error = BntoError::Cancelled;
103        assert_eq!(error.to_string(), "Operation cancelled");
104    }
105
106    #[test]
107    fn test_file_too_large_error_message() {
108        // 5 GB in bytes
109        let error = BntoError::FileTooLarge(5_000_000_000);
110        assert_eq!(error.to_string(), "File too large: 5000000000 bytes");
111    }
112
113    #[test]
114    fn test_out_of_memory_error_message() {
115        let error = BntoError::OutOfMemory("failed to allocate 2GB buffer".to_string());
116        assert_eq!(
117            error.to_string(),
118            "Out of memory: failed to allocate 2GB buffer"
119        );
120    }
121}