1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//! A lightweight error handling library with automatic line number tracking and logging
//!
//! `DebugError` provides macros for creating errors that automatically capture
//! the file and line number of created errors, making debugging much easier.
//!
//! # Features
//! - Automatically captures error location (file + line)
//! - Optional automatic logging when errors occur
//! - Seamless integration with Rust's `?` operator
//! - Compatible with any logger that implements the `log` crate trait
//! - Supports text and variables like the 'format!' macro
//!
//! # Quick Start
//!
//! ```rust
//! use debug_error::{DebugError, debug_error, debug_error_with_log};
//! use log::info;
//!
//! fn main() -> Result<(), DebugError> {
//! // Initialize your logger (env_logger, pretty_env_logger, etc.)
//! env_logger::init();
//!
//! info!("Starting application");
//!
//! // This will return an error with location information
//! let result = might_fail()?;
//!
//! Ok(())
//! }
//!
//! fn might_fail() -> Result<(), DebugError> {
//! if some_condition() {
//! // Automatically logs the error and includes location
//! Err(debug_error_with_log!("Operation failed due to condition"))?
//! }
//!
//! Ok(())
//! }
//!
//! fn some_condition() -> bool {
//! true
//! }
//! ```
/// An error type that captures the location where it was created
///
/// `DebugError` automatically grabs the file and line number where it was created,
/// making debugging much easier by showing exactly where errors originate.
/// Creates a DebugError and automatically logs it
///
/// This macro creates a `DebugError` and immediately logs it at the error level.
/// It's perfect for rapid development when you want immediate visibility into errors.
/// During development you don't need to match on every low-level function.
/// Just use this macro and you get the location immediately, when you are about to finish you can set
/// up all your error handling and replace this macro with the 'debug_error' macro to suppress immediate logging.
///
/// # When to Use the macro
/// - During initial development and prototyping
/// - For quick debugging sessions
/// - When you want immediate error visibility
///
/// # When NOT to Use (and use 'debug_error' macro)
/// - Production code (too verbose)
/// - When you have proper error handling infrastructure set up already
///
/// # Examples
///
/// ```rust
/// use debug_error::{debug_error_with_log, DebugError};
///
/// fn connect_to_database() -> Result<(), DebugError> {
/// // Simulate a connection failure
/// Err(debug_error_with_log!("Database connection timeout after 30s"))
/// }
/// ```
/// Creates a DebugError without automatic logging
///
/// Use this macro when you want the location tracking benefits of DebugError
/// but prefer to handle logging yourself or don't want automatic logging.
///
/// # When to Use
/// - Production code where you control logging
/// - When you want to add custom context before logging
/// - When using custom error handling middleware
/// - When you have proper error handling infrastructure set up
///
/// # Examples
///
/// ```rust
/// use debug_error::{debug_error, DebugError};
/// use log::warn;
///
/// fn validate_input(input: &str) -> Result<(), DebugError> {
/// if input.is_empty() {
/// let err = debug_error!("Input cannot be empty");
/// warn!("Validation warning: {}", err);
/// return Err(err);
/// }
/// Ok(())
/// }
/// ```