deepbiop_core/
error.rs

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
use pyo3::PyErr;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum DPError {
    #[error("An error occurred: {0}")]
    Generic(String),

    #[error("Another error occurred")]
    Another,

    #[error("The sequence is shorter than the k-mer size")]
    SeqShorterThanKmer,

    #[error("The target region is invalid")]
    TargetRegionInvalid,

    #[error("The k-mer id is invalid")]
    InvalidKmerId,

    #[error("The interval is invalid: {0}")]
    InvalidInterval(String),

    #[error("The sequence and quality scores have different lengths: {0}")]
    NotSameLengthForQualityAndSequence(String),
}

impl From<DPError> for PyErr {
    fn from(error: DPError) -> PyErr {
        use DPError::*;
        match error {
            Generic(message) => pyo3::exceptions::PyException::new_err(message),
            Another => pyo3::exceptions::PyException::new_err("Another error occurred"),
            SeqShorterThanKmer => pyo3::exceptions::PyException::new_err(
                "The sequence is shorter than the k-mer size",
            ),
            TargetRegionInvalid => {
                pyo3::exceptions::PyException::new_err("The target region is invalid")
            }
            InvalidKmerId => pyo3::exceptions::PyException::new_err("The k-mer id is invalid"),
            InvalidInterval(interval) => pyo3::exceptions::PyException::new_err(format!(
                "The interval is invalid: {:?}",
                interval
            )),
            NotSameLengthForQualityAndSequence(mes) => {
                pyo3::exceptions::PyException::new_err(format!(
                    "The sequence and quality scores have different lengths: {:?}",
                    mes
                ))
            }
        }
    }
}