Skip to main content

redact_scan/
error.rs

1// Copyright 2026 Censgate LLC.
2// Licensed under the Apache License, Version 2.0. See the LICENSE file
3// in the project root for license information.
4
5use crate::scrub::scrub_secret;
6use std::fmt;
7
8/// Scanner error. [`Display`](std::fmt::Display) and [`Debug`] never include raw credentials.
9#[derive(Debug)]
10pub struct ScanError {
11    message: String,
12}
13
14impl ScanError {
15    /// Build an error after running the credential scrubber on `message`.
16    pub fn new(message: impl std::fmt::Display) -> Self {
17        Self {
18            message: crate::scrub::scrub(&message.to_string()),
19        }
20    }
21
22    /// Build an error, also replacing every occurrence of `secret`.
23    pub fn with_secret(message: impl std::fmt::Display, secret: &str) -> Self {
24        Self {
25            message: scrub_secret(&message.to_string(), secret),
26        }
27    }
28}
29
30impl fmt::Display for ScanError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        f.write_str(&self.message)
33    }
34}
35
36impl std::error::Error for ScanError {}
37
38impl From<anyhow::Error> for ScanError {
39    fn from(err: anyhow::Error) -> Self {
40        Self::new(err)
41    }
42}
43
44impl From<sqlx::Error> for ScanError {
45    fn from(err: sqlx::Error) -> Self {
46        Self::new(err)
47    }
48}