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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//!
//! # Fluvio Error Codes
//!
//! Error code definitions described here.
//!

use flv_util::string_helper::upper_cammel_case_to_sentence;
use crate::derive::Encode;
use crate::derive::Decode;

// -----------------------------------
// Error Definition & Implementation
// -----------------------------------

#[fluvio(encode_discriminant)]
#[repr(i16)]
#[derive(Encode, Decode, PartialEq, Debug, Clone, Copy)]
pub enum ErrorCode {
    UnknownServerError = -1,

    // Not an error
    None = 0,

    OffsetOutOfRange = 1,
    NotLeaderForPartition = 6,
    StorageError = 56,

    // Spu errors
    SpuError = 1000,
    SpuRegisterationFailed = 1001,
    SpuOffline = 1002,
    SpuNotFound = 1003,
    SpuAlreadyExists = 1004,

    // Topic errors
    TopicError = 2000,
    TopicNotFound = 2001,
    TopicAlreadyExists = 2002,
    TopicPendingInitialization = 2003,
    TopicInvalidConfiguration = 2004,

    // Partition errors
    PartitionPendingInitialization = 3000,
    PartitionNotLeader = 3001,
}

impl Default for ErrorCode {
    fn default() -> ErrorCode {
        ErrorCode::None
    }
}

impl ErrorCode {
    pub fn is_ok(&self) -> bool {
        match self {
            Self::None => true,
            _ => false,
        }
    }

    pub fn to_sentence(&self) -> String {
        match self {
            ErrorCode::None => "".to_owned(),
            _ => upper_cammel_case_to_sentence(format!("{:?}", self), true),
        }
    }

    pub fn is_error(&self) -> bool {
        !self.is_ok()
    }
}

// -----------------------------------
// Unit Tests
// -----------------------------------

#[cfg(test)]
mod test {

    use std::convert::TryInto;

    use super::ErrorCode;

    #[test]
    fn test_error_code_from_conversion() {
        let erro_code: ErrorCode = (1001 as i16).try_into().expect("convert");
        assert_eq!(erro_code, ErrorCode::SpuRegisterationFailed);
    }
}