Skip to main content

domain_key/
error.rs

1//! Error types and error handling for domain-key
2//!
3//! This module provides comprehensive error handling for key validation and creation.
4//! All errors are designed to provide detailed information for debugging while maintaining
5//! performance in the happy path.
6
7use core::fmt;
8use thiserror::Error;
9
10#[cfg(not(feature = "std"))]
11use alloc::format;
12#[cfg(not(feature = "std"))]
13use alloc::string::String;
14
15use core::fmt::Write;
16
17// ============================================================================
18// CORE ERROR TYPES
19// ============================================================================
20
21/// Comprehensive error type for key parsing and validation failures
22///
23/// This enum covers all possible validation failures that can occur during
24/// key creation, providing detailed information for debugging and user feedback.
25///
26/// # Error Categories
27///
28/// - **Length Errors**: Empty keys or keys exceeding maximum length
29/// - **Character Errors**: Invalid characters at specific positions
30/// - **Structure Errors**: Invalid patterns like consecutive special characters
31/// - **Domain Errors**: Domain-specific validation failures
32/// - **Custom Errors**: Application-specific validation failures
33///
34/// # Examples
35///
36/// ```rust
37/// use domain_key::{KeyParseError, ErrorCategory};
38///
39/// // Handle different error types
40/// match KeyParseError::Empty {
41///     err => {
42///         println!("Error: {}", err);
43///         println!("Code: {}", err.code());
44///         println!("Category: {:?}", err.category());
45///     }
46/// }
47/// ```
48#[derive(Debug, Error, PartialEq, Eq, Clone)]
49#[non_exhaustive]
50pub enum KeyParseError {
51    /// Key cannot be empty or contain only whitespace
52    ///
53    /// This error occurs when attempting to create a key from an empty string
54    /// or a string containing only whitespace characters.
55    #[error("Key cannot be empty or whitespace")]
56    Empty,
57
58    /// Key contains a character that is not allowed at the specified position
59    ///
60    /// Each domain defines which characters are allowed. This error provides
61    /// the specific character, its position, and optionally what was expected.
62    #[error("Invalid character '{character}' at position {position}")]
63    InvalidCharacter {
64        /// The invalid character that was found
65        character: char,
66        /// Position where the invalid character was found (0-based)
67        position: usize,
68        /// Optional description of what characters are expected
69        expected: Option<&'static str>,
70    },
71
72    /// Key exceeds the maximum allowed length for the domain
73    ///
74    /// Each domain can specify a maximum length. This error provides both
75    /// the limit and the actual length that was attempted.
76    #[error("Key is too long (max {max_length} characters, got {actual_length})")]
77    TooLong {
78        /// The maximum allowed length for this domain
79        max_length: usize,
80        /// The actual length of the key that was attempted
81        actual_length: usize,
82    },
83
84    /// Key is shorter than the minimum allowed length for the domain
85    ///
86    /// Each domain can specify a minimum length. This error provides both
87    /// the required minimum and the actual length that was attempted.
88    #[error("Key is too short (min {min_length} characters, got {actual_length})")]
89    TooShort {
90        /// The minimum allowed length for this domain
91        min_length: usize,
92        /// The actual length of the key that was attempted
93        actual_length: usize,
94    },
95
96    /// Key has invalid structure (consecutive special chars, invalid start/end)
97    ///
98    /// This covers structural issues like:
99    /// - Starting or ending with special characters
100    /// - Consecutive special characters
101    /// - Invalid character sequences
102    #[error("Key has invalid structure: {reason}")]
103    InvalidStructure {
104        /// Description of the structural issue
105        reason: &'static str,
106    },
107
108    /// Domain-specific validation error
109    ///
110    /// This error is returned when domain-specific validation rules fail.
111    /// It includes the domain name and a descriptive message.
112    #[error("Domain '{domain}' validation failed: {message}")]
113    DomainValidation {
114        /// The domain name where validation failed
115        domain: &'static str,
116        /// The error message describing what validation failed
117        message: String,
118    },
119
120    /// Custom error for specific use cases
121    ///
122    /// Applications can define custom validation errors with numeric codes
123    /// for structured error handling.
124    #[error("Custom validation error (code: {code}): {message}")]
125    Custom {
126        /// Custom error code for programmatic handling
127        code: u32,
128        /// The custom error message
129        message: String,
130    },
131}
132
133impl KeyParseError {
134    /// Create a domain validation error with domain name
135    ///
136    /// This is the preferred way to create domain validation errors.
137    ///
138    /// # Examples
139    ///
140    /// ```rust
141    /// use domain_key::KeyParseError;
142    ///
143    /// let error = KeyParseError::domain_error("my_domain", "Custom validation failed");
144    /// // Verify it's the correct error type
145    /// match error {
146    ///     KeyParseError::DomainValidation { domain, message } => {
147    ///         assert_eq!(domain, "my_domain");
148    ///         assert!(message.contains("Custom validation failed"));
149    ///     },
150    ///     _ => panic!("Expected domain validation error"),
151    /// }
152    /// ```
153    pub fn domain_error(domain: &'static str, message: impl Into<String>) -> Self {
154        Self::DomainValidation {
155            domain,
156            message: message.into(),
157        }
158    }
159
160    /// Create a domain validation error without specifying domain (for internal use)
161    pub fn domain_error_generic(message: impl Into<String>) -> Self {
162        Self::DomainValidation {
163            domain: "unknown",
164            message: message.into(),
165        }
166    }
167
168    /// Create a domain validation error with source error information
169    ///
170    /// The source error's `Display` representation is appended to `message`,
171    /// separated by `": "`.  This is a **flattened** representation — the
172    /// resulting `KeyParseError` does **not** implement `std::error::Error::source()`
173    /// chaining back to the original error.  In other words, `error.source()`
174    /// will return `None`, and tools such as `anyhow` / `tracing` that walk the
175    /// error chain will not see the wrapped cause.
176    ///
177    /// If you need a proper causal chain, wrap the original error in your own
178    /// error type (e.g. via `anyhow::Error` or a `thiserror` wrapper) before
179    /// converting it to a `KeyParseError`.
180    ///
181    /// # Limitation
182    ///
183    /// Properly storing a `Box<dyn Error + Send + Sync>` inside `KeyParseError`
184    /// variants would require either a new variant or a breaking field change.
185    /// Until that refactor is done, the full error context is preserved only in
186    /// the formatted message string.
187    #[cfg(feature = "std")]
188    pub fn domain_error_with_source(
189        domain: &'static str,
190        message: impl Into<String>,
191        source: &(dyn std::error::Error + Send + Sync),
192    ) -> Self {
193        let full_message = format!("{}: {}", message.into(), source);
194        Self::DomainValidation {
195            domain,
196            message: full_message,
197        }
198    }
199
200    /// Create a custom validation error
201    ///
202    /// Custom errors allow applications to define their own error codes
203    /// for structured error handling.
204    ///
205    /// # Examples
206    ///
207    /// ```rust
208    /// use domain_key::KeyParseError;
209    ///
210    /// let error = KeyParseError::custom(1001, "Business rule violation");
211    /// assert_eq!(error.code(), 1001);
212    /// ```
213    pub fn custom(code: u32, message: impl Into<String>) -> Self {
214        Self::Custom {
215            code,
216            message: message.into(),
217        }
218    }
219
220    /// Create a custom validation error with source error information
221    ///
222    /// The source error's `Display` representation is appended to `message`,
223    /// separated by `": "`.  This is a **flattened** representation — the
224    /// resulting `KeyParseError` does **not** implement `std::error::Error::source()`
225    /// chaining back to the original error.  In other words, `error.source()`
226    /// will return `None`, and tools such as `anyhow` / `tracing` that walk the
227    /// error chain will not see the wrapped cause.
228    ///
229    /// If you need a proper causal chain, wrap the original error in your own
230    /// error type (e.g. via `anyhow::Error` or a `thiserror` wrapper) before
231    /// converting it to a `KeyParseError`.
232    ///
233    /// # Limitation
234    ///
235    /// Properly storing a `Box<dyn Error + Send + Sync>` inside `KeyParseError`
236    /// variants would require either a new variant or a breaking field change.
237    /// Until that refactor is done, the full error context is preserved only in
238    /// the formatted message string.
239    #[cfg(feature = "std")]
240    pub fn custom_with_source(
241        code: u32,
242        message: impl Into<String>,
243        source: &(dyn std::error::Error + Send + Sync),
244    ) -> Self {
245        let full_message = format!("{}: {}", message.into(), source);
246        Self::Custom {
247            code,
248            message: full_message,
249        }
250    }
251
252    /// Get the error code for machine processing
253    ///
254    /// Returns a numeric code that can be used for programmatic error handling.
255    /// This is useful for APIs that need to return structured error responses.
256    ///
257    /// # Error Codes
258    ///
259    /// - `1001`: Empty key
260    /// - `1002`: Invalid character
261    /// - `1003`: Key too long
262    /// - `1004`: Invalid structure
263    /// - `2000`: Domain validation (base code)
264    /// - Custom codes: As specified in `Custom` errors
265    ///
266    /// # Examples
267    ///
268    /// ```rust
269    /// use domain_key::KeyParseError;
270    ///
271    /// assert_eq!(KeyParseError::Empty.code(), 1001);
272    /// assert_eq!(KeyParseError::custom(42, "test").code(), 42);
273    /// ```
274    #[must_use]
275    pub const fn code(&self) -> u32 {
276        match self {
277            Self::Empty => 1001,
278            Self::InvalidCharacter { .. } => 1002,
279            Self::TooLong { .. } => 1003,
280            Self::InvalidStructure { .. } => 1004,
281            Self::TooShort { .. } => 1005,
282            Self::DomainValidation { .. } => 2000,
283            Self::Custom { code, .. } => *code,
284        }
285    }
286
287    /// Get the error category for classification
288    ///
289    /// Returns the general category of this error for higher-level error handling.
290    /// This allows applications to handle broad categories of errors uniformly.
291    ///
292    /// # Examples
293    ///
294    /// ```rust
295    /// use domain_key::{KeyParseError, ErrorCategory};
296    ///
297    /// match KeyParseError::Empty.category() {
298    ///     ErrorCategory::Length => println!("Length-related error"),
299    ///     ErrorCategory::Character => println!("Character-related error"),
300    ///     _ => println!("Other error type"),
301    /// }
302    /// ```
303    #[must_use]
304    pub const fn category(&self) -> ErrorCategory {
305        match self {
306            Self::Empty | Self::TooLong { .. } | Self::TooShort { .. } => ErrorCategory::Length,
307            Self::InvalidCharacter { .. } => ErrorCategory::Character,
308            Self::InvalidStructure { .. } => ErrorCategory::Structure,
309            Self::DomainValidation { .. } => ErrorCategory::Domain,
310            Self::Custom { code, .. } => match code {
311                1002 => ErrorCategory::Character,
312                1003 => ErrorCategory::Length,
313                1004 => ErrorCategory::Structure,
314                _ => ErrorCategory::Custom,
315            },
316        }
317    }
318
319    /// Get a human-readable description of what went wrong
320    ///
321    /// This provides additional context beyond the basic error message,
322    /// useful for user-facing error messages or debugging.
323    #[must_use]
324    pub fn description(&self) -> &'static str {
325        match self {
326            Self::Empty => "Key cannot be empty or contain only whitespace characters",
327            Self::InvalidCharacter { .. } => {
328                "Key contains characters that are not allowed by the domain"
329            }
330            Self::TooLong { .. } => "Key exceeds the maximum length allowed by the domain",
331            Self::TooShort { .. } => {
332                "Key is shorter than the minimum length required by the domain"
333            }
334            Self::InvalidStructure { .. } => "Key has invalid structure or formatting",
335            Self::DomainValidation { .. } => "Key fails domain-specific validation rules",
336            Self::Custom { .. } => "Key fails custom validation rules",
337        }
338    }
339
340    /// Get suggested actions for fixing this error
341    ///
342    /// Returns helpful suggestions for how to fix the validation error.
343    #[must_use]
344    pub fn suggestions(&self) -> &'static [&'static str] {
345        match self {
346            Self::Empty => &[
347                "Provide a non-empty key",
348                "Remove leading/trailing whitespace",
349            ],
350            Self::InvalidCharacter { .. } => &[
351                "Use only allowed characters (check domain rules)",
352                "Remove or replace invalid characters",
353            ],
354            Self::TooLong { .. } => &[
355                "Shorten the key to fit within length limits",
356                "Consider using abbreviated forms",
357            ],
358            Self::TooShort { .. } => &["Lengthen the key to meet the minimum length requirement"],
359            Self::InvalidStructure { .. } => &[
360                "Avoid consecutive special characters",
361                "Don't start or end with special characters",
362                "Follow the expected key format",
363            ],
364            Self::DomainValidation { .. } => &[
365                "Check domain-specific validation rules",
366                "Refer to domain documentation",
367            ],
368            Self::Custom { .. } => &[
369                "Check application-specific validation rules",
370                "Contact system administrator if needed",
371            ],
372        }
373    }
374
375    /// Check if this error is recoverable through user action
376    ///
377    /// Returns `true` if the user can potentially fix this error by modifying
378    /// their input, `false` if it represents a programming error or system issue.
379    #[must_use]
380    pub const fn is_recoverable(&self) -> bool {
381        match self {
382            Self::Empty
383            | Self::InvalidCharacter { .. }
384            | Self::TooLong { .. }
385            | Self::TooShort { .. }
386            | Self::InvalidStructure { .. }
387            | Self::DomainValidation { .. } => true,
388            Self::Custom { .. } => false, // Depends on the specific custom error
389        }
390    }
391}
392
393// ============================================================================
394// ERROR CATEGORIES
395// ============================================================================
396
397/// Error category for classification of validation errors
398///
399/// These categories allow applications to handle broad types of validation
400/// errors uniformly, regardless of the specific error details.
401#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
402#[non_exhaustive]
403pub enum ErrorCategory {
404    /// Length-related errors (empty, too long)
405    Length,
406    /// Character-related errors (invalid characters)
407    Character,
408    /// Structure-related errors (invalid format, consecutive special chars)
409    Structure,
410    /// Domain-specific validation errors
411    Domain,
412    /// Custom validation errors
413    Custom,
414}
415
416impl ErrorCategory {
417    /// Get a human-readable name for this category
418    #[must_use]
419    pub const fn name(self) -> &'static str {
420        match self {
421            Self::Length => "Length",
422            Self::Character => "Character",
423            Self::Structure => "Structure",
424            Self::Domain => "Domain",
425            Self::Custom => "Custom",
426        }
427    }
428
429    /// Get a description of what this category represents
430    #[must_use]
431    pub const fn description(self) -> &'static str {
432        match self {
433            Self::Length => "Errors related to key length (empty, too long, etc.)",
434            Self::Character => "Errors related to invalid characters in the key",
435            Self::Structure => "Errors related to key structure and formatting",
436            Self::Domain => "Errors from domain-specific validation rules",
437            Self::Custom => "Custom application-specific validation errors",
438        }
439    }
440}
441
442impl fmt::Display for ErrorCategory {
443    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
444        write!(f, "{}", self.name())
445    }
446}
447
448// ============================================================================
449// ID PARSE ERROR
450// ============================================================================
451
452/// Error type for numeric ID parsing failures
453///
454/// This error is returned when a string cannot be parsed as a valid `Id<D>`.
455///
456/// # Examples
457///
458/// ```rust
459/// use domain_key::IdParseError;
460///
461/// let result = "not_a_number".parse::<u64>();
462/// assert!(result.is_err());
463/// ```
464#[derive(Debug, Error, PartialEq, Eq, Clone)]
465#[non_exhaustive]
466pub enum IdParseError {
467    /// The value is zero, which is not a valid identifier
468    #[error("ID cannot be zero")]
469    Zero,
470
471    /// The string could not be parsed as a valid non-zero u64 number
472    #[error("Invalid numeric ID: {0}")]
473    InvalidNumber(#[from] core::num::ParseIntError),
474}
475
476impl IdParseError {
477    /// Returns a user-friendly error message suitable for display.
478    #[must_use]
479    pub fn user_message(&self) -> &'static str {
480        match self {
481            Self::Zero => "Identifier cannot be zero",
482            Self::InvalidNumber(_) => "Value must be a positive integer",
483        }
484    }
485}
486
487// ============================================================================
488// UUID PARSE ERROR
489// ============================================================================
490
491/// Error type for UUID parsing failures
492///
493/// This error is returned when a string cannot be parsed as a valid `Uuid<D>`.
494/// Only available when the `uuid` feature is enabled.
495///
496/// Note: `UuidParseError` does not implement `PartialEq` because
497/// `uuid::Error` does not implement it.
498#[cfg(feature = "uuid")]
499#[derive(Debug, Error, Clone)]
500#[non_exhaustive]
501pub enum UuidParseError {
502    /// The string could not be parsed as a valid UUID
503    #[error("Invalid UUID: {0}")]
504    InvalidUuid(#[from] ::uuid::Error),
505}
506
507// ============================================================================
508// ERROR UTILITIES
509// ============================================================================
510
511/// Builder for creating detailed validation errors
512///
513/// This builder provides a fluent interface for creating complex validation
514/// errors with additional context and suggestions.
515#[derive(Debug)]
516pub struct ErrorBuilder {
517    category: ErrorCategory,
518    code: Option<u32>,
519    domain: Option<&'static str>,
520    message: String,
521    context: Option<String>,
522}
523
524impl ErrorBuilder {
525    /// Create a new error builder for the given category
526    #[must_use]
527    pub fn new(category: ErrorCategory) -> Self {
528        Self {
529            category,
530            code: None,
531            domain: None,
532            message: String::new(),
533            context: None,
534        }
535    }
536
537    /// Set the error message
538    #[must_use]
539    pub fn message(mut self, message: impl Into<String>) -> Self {
540        self.message = message.into();
541        self
542    }
543
544    /// Set a custom error code (used when category is `Custom`)
545    #[must_use]
546    pub fn code(mut self, code: u32) -> Self {
547        self.code = Some(code);
548        self
549    }
550
551    /// Set the domain name (used when category is `Domain`)
552    #[must_use]
553    pub fn domain(mut self, domain: &'static str) -> Self {
554        self.domain = Some(domain);
555        self
556    }
557
558    /// Set additional context information
559    #[must_use]
560    pub fn context(mut self, context: impl Into<String>) -> Self {
561        self.context = Some(context.into());
562        self
563    }
564
565    /// Build the final error
566    #[must_use]
567    pub fn build(self) -> KeyParseError {
568        let message = if let Some(context) = self.context {
569            format!("{} (Context: {})", self.message, context)
570        } else {
571            self.message
572        };
573
574        match self.category {
575            ErrorCategory::Custom => KeyParseError::custom(self.code.unwrap_or(0), message),
576            ErrorCategory::Domain => {
577                KeyParseError::domain_error(self.domain.unwrap_or("unknown"), message)
578            }
579            // Use the reserved structural codes so that category() round-trips
580            // correctly back to the originally-specified category.
581            // 1004 → ErrorCategory::Structure, 1003 → ErrorCategory::Length,
582            // 1002 → ErrorCategory::Character  (mirroring code() for each variant)
583            ErrorCategory::Structure => KeyParseError::custom(1004, message),
584            ErrorCategory::Length => KeyParseError::custom(1003, message),
585            ErrorCategory::Character => KeyParseError::custom(1002, message),
586        }
587    }
588}
589
590// ============================================================================
591// ERROR FORMATTING UTILITIES
592// ============================================================================
593
594/// Format an error for display to end users
595///
596/// This function provides a user-friendly representation of validation errors,
597/// including suggestions for how to fix them.
598#[must_use]
599pub fn format_user_error(error: &KeyParseError) -> String {
600    let mut output = format!("Error: {error}");
601
602    let suggestions = error.suggestions();
603    if !suggestions.is_empty() {
604        output.push_str("\n\nSuggestions:");
605        for suggestion in suggestions {
606            write!(output, "\n  - {suggestion}").unwrap();
607        }
608    }
609
610    output
611}
612
613/// Format an error for logging or debugging
614///
615/// This function provides a detailed representation suitable for logs,
616/// including error codes and categories.
617#[must_use]
618pub fn format_debug_error(error: &KeyParseError) -> String {
619    format!(
620        "[{}:{}] {} (Category: {})",
621        error.code(),
622        error.category().name(),
623        error,
624        error.description()
625    )
626}
627
628// ============================================================================
629// TESTS
630// ============================================================================
631
632#[cfg(test)]
633mod tests {
634    use super::*;
635
636    #[cfg(not(feature = "std"))]
637    use alloc::string::ToString;
638
639    #[test]
640    fn each_variant_has_unique_error_code() {
641        assert_eq!(KeyParseError::Empty.code(), 1001);
642        assert_eq!(
643            KeyParseError::InvalidCharacter {
644                character: 'x',
645                position: 0,
646                expected: None
647            }
648            .code(),
649            1002
650        );
651        assert_eq!(
652            KeyParseError::TooLong {
653                max_length: 10,
654                actual_length: 20
655            }
656            .code(),
657            1003
658        );
659        assert_eq!(
660            KeyParseError::InvalidStructure { reason: "test" }.code(),
661            1004
662        );
663        assert_eq!(
664            KeyParseError::TooShort {
665                min_length: 5,
666                actual_length: 2
667            }
668            .code(),
669            1005
670        );
671        assert_eq!(
672            KeyParseError::DomainValidation {
673                domain: "test",
674                message: "msg".to_string()
675            }
676            .code(),
677            2000
678        );
679        assert_eq!(
680            KeyParseError::Custom {
681                code: 42,
682                message: "msg".to_string()
683            }
684            .code(),
685            42
686        );
687    }
688
689    #[test]
690    fn variants_map_to_correct_category() {
691        assert_eq!(KeyParseError::Empty.category(), ErrorCategory::Length);
692        assert_eq!(
693            KeyParseError::InvalidCharacter {
694                character: 'x',
695                position: 0,
696                expected: None
697            }
698            .category(),
699            ErrorCategory::Character
700        );
701        assert_eq!(
702            KeyParseError::TooLong {
703                max_length: 10,
704                actual_length: 20
705            }
706            .category(),
707            ErrorCategory::Length
708        );
709        assert_eq!(
710            KeyParseError::InvalidStructure { reason: "test" }.category(),
711            ErrorCategory::Structure
712        );
713        assert_eq!(
714            KeyParseError::TooShort {
715                min_length: 5,
716                actual_length: 2
717            }
718            .category(),
719            ErrorCategory::Length
720        );
721        assert_eq!(
722            KeyParseError::DomainValidation {
723                domain: "test",
724                message: "msg".to_string()
725            }
726            .category(),
727            ErrorCategory::Domain
728        );
729        assert_eq!(
730            KeyParseError::Custom {
731                code: 42,
732                message: "msg".to_string()
733            }
734            .category(),
735            ErrorCategory::Custom
736        );
737    }
738
739    #[test]
740    fn empty_error_provides_recovery_suggestions() {
741        let error = KeyParseError::Empty;
742        let suggestions = error.suggestions();
743        assert!(!suggestions.is_empty());
744        assert!(suggestions.iter().any(|s| s.contains("non-empty")));
745    }
746
747    #[test]
748    fn builder_produces_custom_error_with_code_and_context() {
749        let error = ErrorBuilder::new(ErrorCategory::Custom)
750            .message("Test error")
751            .code(1234)
752            .context("In test function")
753            .build();
754
755        assert_eq!(error.code(), 1234);
756        assert_eq!(error.category(), ErrorCategory::Custom);
757    }
758
759    #[test]
760    fn variants_carry_correct_payloads() {
761        let error1 = KeyParseError::InvalidCharacter {
762            character: '!',
763            position: 5,
764            expected: Some("alphanumeric"),
765        };
766        assert!(matches!(
767            error1,
768            KeyParseError::InvalidCharacter {
769                character: '!',
770                position: 5,
771                ..
772            }
773        ));
774
775        let error2 = KeyParseError::TooLong {
776            max_length: 32,
777            actual_length: 64,
778        };
779        assert!(matches!(
780            error2,
781            KeyParseError::TooLong {
782                max_length: 32,
783                actual_length: 64
784            }
785        ));
786
787        let error3 = KeyParseError::InvalidStructure {
788            reason: "consecutive underscores",
789        };
790        assert!(matches!(
791            error3,
792            KeyParseError::InvalidStructure {
793                reason: "consecutive underscores"
794            }
795        ));
796
797        let error4 = KeyParseError::domain_error("test", "Invalid format");
798        assert!(matches!(
799            error4,
800            KeyParseError::DomainValidation { domain: "test", .. }
801        ));
802    }
803
804    #[test]
805    fn user_and_debug_formats_include_expected_sections() {
806        let error = KeyParseError::Empty;
807        let user_format = format_user_error(&error);
808        let debug_format = format_debug_error(&error);
809
810        assert!(user_format.contains("Error:"));
811        assert!(user_format.contains("Suggestions:"));
812        assert!(debug_format.contains("1001"));
813        assert!(debug_format.contains("Length"));
814    }
815
816    #[test]
817    fn recoverable_errors_distinguished_from_non_recoverable() {
818        assert!(KeyParseError::Empty.is_recoverable());
819        assert!(KeyParseError::InvalidCharacter {
820            character: 'x',
821            position: 0,
822            expected: None
823        }
824        .is_recoverable());
825        assert!(KeyParseError::TooShort {
826            min_length: 5,
827            actual_length: 2
828        }
829        .is_recoverable());
830        assert!(!KeyParseError::Custom {
831            code: 42,
832            message: "msg".to_string()
833        }
834        .is_recoverable());
835    }
836
837    #[test]
838    fn category_display_and_description_are_populated() {
839        assert_eq!(ErrorCategory::Length.to_string(), "Length");
840        assert_eq!(ErrorCategory::Character.name(), "Character");
841        assert!(ErrorCategory::Domain
842            .description()
843            .contains("domain-specific"));
844    }
845}