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