nimble_client_logic/
err.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/nimble-rust/nimble
 * Licensed under the MIT License. See LICENSE in the project root for license information.
 */
use err_rs::{ErrorLevel, ErrorLevelProvider};
use nimble_blob_stream::in_logic_front::FrontLogicError;
use nimble_blob_stream::prelude::BlobError;
use nimble_protocol::ClientRequestId;
use std::{fmt, io};
use tick_queue::QueueError;

#[derive(Debug)]
pub enum ClientLogicError {
    IoErr(io::Error),
    WrongJoinResponseRequestId {
        expected: ClientRequestId,
        encountered: ClientRequestId,
    },
    WrongConnectResponseRequestId(ClientRequestId),
    WrongDownloadRequestId,
    DownloadResponseWasUnexpected,
    UnexpectedBlobChannelCommand,
    BlobError(BlobError),
    FrontLogicErr(FrontLogicError),
    QueueError(QueueError),
    ReceivedConnectResponseWhenNotConnecting,
    CanNotPushEmptyPredictedSteps,
}

impl From<BlobError> for ClientLogicError {
    fn from(err: BlobError) -> Self {
        Self::BlobError(err)
    }
}

impl From<QueueError> for ClientLogicError {
    fn from(err: QueueError) -> Self {
        Self::QueueError(err)
    }
}

impl From<FrontLogicError> for ClientLogicError {
    fn from(err: FrontLogicError) -> Self {
        Self::FrontLogicErr(err)
    }
}

impl From<io::Error> for ClientLogicError {
    fn from(err: io::Error) -> Self {
        Self::IoErr(err)
    }
}

impl ErrorLevelProvider for ClientLogicError {
    fn error_level(&self) -> ErrorLevel {
        match self {
            Self::IoErr(_) => ErrorLevel::Critical,
            Self::WrongConnectResponseRequestId(_) => ErrorLevel::Info,
            Self::WrongDownloadRequestId => ErrorLevel::Warning,
            Self::DownloadResponseWasUnexpected => ErrorLevel::Info,
            Self::UnexpectedBlobChannelCommand => ErrorLevel::Info,
            Self::FrontLogicErr(err) => err.error_level(),
            Self::QueueError(_) => ErrorLevel::Critical,
            Self::ReceivedConnectResponseWhenNotConnecting => ErrorLevel::Info,
            Self::BlobError(_) => ErrorLevel::Warning,
            Self::WrongJoinResponseRequestId { .. } => ErrorLevel::Info,
            Self::CanNotPushEmptyPredictedSteps => ErrorLevel::Critical,
        }
    }
}

impl fmt::Display for ClientLogicError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::IoErr(io_err) => {
                write!(f, "io:err {:?}", io_err)
            }
            Self::WrongConnectResponseRequestId(nonce) => {
                write!(f, "wrong nonce in reply to connect {:?}", nonce)
            }
            Self::WrongDownloadRequestId => {
                write!(f, "WrongDownloadRequestId")
            }
            Self::DownloadResponseWasUnexpected => {
                write!(f, "DownloadResponseWasUnexpected")
            }
            Self::UnexpectedBlobChannelCommand => write!(f, "UnexpectedBlobChannelCommand"),
            Self::FrontLogicErr(err) => write!(f, "front logic err {err:?}"),
            Self::QueueError(err) => write!(f, "Steps queue err: {err:?}"),
            Self::ReceivedConnectResponseWhenNotConnecting => {
                write!(f, "ReceivedConnectResponseWhenNotConnecting")
            }
            Self::BlobError(_) => write!(f, "BlobError"),
            Self::WrongJoinResponseRequestId {
                expected,
                encountered,
            } => write!(
                f,
                "wrong join response, expected {expected:?}, encountered: {encountered:?}"
            ),
            Self::CanNotPushEmptyPredictedSteps => write!(f, "CanNotPushEmptyPredictedSteps"),
        }
    }
}

impl std::error::Error for ClientLogicError {} // it implements Debug and Display