nimble_client_logic/
err.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/nimble-rust/nimble
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5use err_rs::{ErrorLevel, ErrorLevelProvider};
6use nimble_blob_stream::in_logic_front::FrontLogicError;
7use nimble_blob_stream::prelude::BlobError;
8use nimble_protocol::ClientRequestId;
9use std::{fmt, io};
10use tick_queue::QueueError;
11
12#[derive(Debug)]
13pub enum ClientLogicError {
14    IoErr(io::Error),
15    WrongJoinResponseRequestId {
16        expected: ClientRequestId,
17        encountered: ClientRequestId,
18    },
19    WrongConnectResponseRequestId(ClientRequestId),
20    WrongDownloadRequestId,
21    DownloadResponseWasUnexpected,
22    UnexpectedBlobChannelCommand,
23    BlobError(BlobError),
24    FrontLogicErr(FrontLogicError),
25    QueueError(QueueError),
26    ReceivedConnectResponseWhenNotConnecting,
27    CanNotPushEmptyPredictedSteps,
28    MillisFromLowerError,
29    AbsoluteTimeError,
30    TooManyAuthoritativeSteps,
31    LatencyIsTooBig,
32    TooManyStepsInRange,
33}
34
35impl From<BlobError> for ClientLogicError {
36    fn from(err: BlobError) -> Self {
37        Self::BlobError(err)
38    }
39}
40
41impl From<QueueError> for ClientLogicError {
42    fn from(err: QueueError) -> Self {
43        Self::QueueError(err)
44    }
45}
46
47impl From<FrontLogicError> for ClientLogicError {
48    fn from(err: FrontLogicError) -> Self {
49        Self::FrontLogicErr(err)
50    }
51}
52
53impl From<io::Error> for ClientLogicError {
54    fn from(err: io::Error) -> Self {
55        Self::IoErr(err)
56    }
57}
58
59impl ErrorLevelProvider for ClientLogicError {
60    fn error_level(&self) -> ErrorLevel {
61        match self {
62            Self::IoErr(_)
63            | Self::QueueError(_)
64            | Self::CanNotPushEmptyPredictedSteps
65            | Self::TooManyAuthoritativeSteps
66            | Self::TooManyStepsInRange => ErrorLevel::Critical,
67            Self::WrongConnectResponseRequestId(_)
68            | Self::DownloadResponseWasUnexpected
69            | Self::UnexpectedBlobChannelCommand
70            | Self::ReceivedConnectResponseWhenNotConnecting
71            | Self::WrongJoinResponseRequestId { .. } => ErrorLevel::Info,
72            Self::WrongDownloadRequestId
73            | Self::BlobError(_)
74            | Self::MillisFromLowerError
75            | Self::AbsoluteTimeError
76            | Self::LatencyIsTooBig => ErrorLevel::Warning,
77            Self::FrontLogicErr(err) => err.error_level(),
78        }
79    }
80}
81
82impl fmt::Display for ClientLogicError {
83    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84        match self {
85            Self::IoErr(io_err) => {
86                write!(f, "io:err {io_err:?}")
87            }
88            Self::WrongConnectResponseRequestId(nonce) => {
89                write!(f, "wrong nonce in reply to connect {nonce:?}")
90            }
91            Self::WrongDownloadRequestId => {
92                write!(f, "WrongDownloadRequestId")
93            }
94            Self::DownloadResponseWasUnexpected => {
95                write!(f, "DownloadResponseWasUnexpected")
96            }
97            Self::UnexpectedBlobChannelCommand => write!(f, "UnexpectedBlobChannelCommand"),
98            Self::FrontLogicErr(err) => write!(f, "front logic err {err:?}"),
99            Self::QueueError(err) => write!(f, "Steps queue err: {err:?}"),
100            Self::ReceivedConnectResponseWhenNotConnecting => {
101                write!(f, "ReceivedConnectResponseWhenNotConnecting")
102            }
103            Self::BlobError(_) => write!(f, "BlobError"),
104            Self::WrongJoinResponseRequestId {
105                expected,
106                encountered,
107            } => write!(
108                f,
109                "wrong join response, expected {expected:?}, encountered: {encountered:?}"
110            ),
111            Self::CanNotPushEmptyPredictedSteps => write!(f, "CanNotPushEmptyPredictedSteps"),
112            Self::MillisFromLowerError => write!(f, "millis from lower"),
113            Self::AbsoluteTimeError => write!(f, "absolute time"),
114            Self::TooManyAuthoritativeSteps => write!(f, "TooManyAuthoritativeSteps"),
115            Self::LatencyIsTooBig => write!(f, "Latency Is Too Big"),
116            Self::TooManyStepsInRange => write!(f, "Too ManySteps"),
117        }
118    }
119}
120
121impl std::error::Error for ClientLogicError {} // it implements Debug and Display