dd_sds/scanner/
error.rs

1use std::convert::From;
2use thiserror::Error;
3
4use crate::{
5    RegexValidationError, match_action::MatchActionValidationError,
6    proximity_keywords::ProximityKeywordsValidationError, scanner::SuppressionValidationError,
7    validation::RegexPatternCaptureGroupsValidationError,
8};
9
10impl From<CreateScannerError> for i64 {
11    fn from(value: CreateScannerError) -> i64 {
12        match value {
13            CreateScannerError::InvalidRegex(_) => -2,
14            CreateScannerError::InvalidKeywords(_) => -3,
15            CreateScannerError::InvalidMatchAction(_) => -4,
16            CreateScannerError::InvalidMatchValidator(_) => -5,
17            CreateScannerError::InvalidSuppressions(_) => -6,
18            CreateScannerError::InvalidPatternCaptureGroups(_) => -7,
19        }
20    }
21}
22
23#[derive(Debug, PartialEq, Eq, Error)]
24pub enum MatchValidatorCreationError {
25    #[error("Internal error while creating the match validator")]
26    InternalError,
27}
28
29#[derive(Debug, PartialEq, Eq, Error)]
30pub enum CreateScannerError {
31    //The regex is invalid (too long, too complex, etc.)
32    #[error(transparent)]
33    InvalidRegex(#[from] RegexValidationError),
34    /// The included keywords config is invalid (empty keyword, too many keywords, etc.)
35    #[error(transparent)]
36    InvalidKeywords(#[from] ProximityKeywordsValidationError),
37    /// Invalid configuration of a match action
38    #[error(transparent)]
39    InvalidMatchAction(#[from] MatchActionValidationError),
40    /// The match validator cannot be created
41    #[error(transparent)]
42    InvalidMatchValidator(#[from] MatchValidatorCreationError),
43    /// The suppressions are invalid (too many suppressions, duplicate suppressions, etc.)
44    #[error(transparent)]
45    InvalidSuppressions(#[from] SuppressionValidationError),
46    /// The pattern capture groups are invalid (too many capture groups, capture group not present, etc.)
47    #[error(transparent)]
48    InvalidPatternCaptureGroups(#[from] RegexPatternCaptureGroupsValidationError),
49}
50
51#[derive(Debug, PartialEq, Eq, Error)]
52pub enum ScannerError {
53    #[error("Transient error while scanning")]
54    Transient(String),
55}
56
57#[cfg(test)]
58mod test {
59    use crate::match_action::MatchActionValidationError;
60    use crate::proximity_keywords::ProximityKeywordsValidationError;
61    use crate::{CreateScannerError, RegexValidationError};
62
63    fn test_error(error: CreateScannerError, expected_display: &str) {
64        assert_eq!(error.to_string(), expected_display)
65    }
66
67    #[test]
68    fn test_invalid_keywords() {
69        test_error(
70            CreateScannerError::InvalidKeywords(ProximityKeywordsValidationError::EmptyKeyword),
71            "Empty keywords are not allowed",
72        );
73
74        test_error(
75            CreateScannerError::InvalidKeywords(ProximityKeywordsValidationError::TooManyKeywords),
76            "No more than 50 keywords are allowed",
77        );
78
79        test_error(
80            CreateScannerError::InvalidKeywords(ProximityKeywordsValidationError::KeywordTooLong(
81                10,
82            )),
83            "Keywords cannot be longer than the look ahead character count (10)",
84        );
85
86        test_error(
87            CreateScannerError::InvalidKeywords(
88                ProximityKeywordsValidationError::InvalidLookAheadCharacterCount,
89            ),
90            "Look ahead character count should be bigger than 0 and cannot be longer than 50",
91        )
92    }
93
94    #[test]
95    fn test_invalid_regex() {
96        test_error(
97            CreateScannerError::InvalidRegex(RegexValidationError::InvalidSyntax),
98            "Invalid regex syntax",
99        );
100        test_error(
101            CreateScannerError::InvalidRegex(RegexValidationError::ExceededDepthLimit),
102            "The regex pattern is nested too deeply",
103        );
104        test_error(
105            CreateScannerError::InvalidRegex(RegexValidationError::TooComplex),
106            "The regex has exceeded the complexity limit (try simplifying the regex)",
107        );
108        test_error(
109            CreateScannerError::InvalidRegex(RegexValidationError::MatchesEmptyString),
110            "Regex patterns are not allowed to match an empty string",
111        );
112    }
113
114    #[test]
115    fn test_match_action() {
116        test_error(
117            CreateScannerError::InvalidMatchAction(
118                MatchActionValidationError::PartialRedactionNumCharsZero,
119            ),
120            "Partial redaction chars must be non-zero",
121        );
122    }
123}