1
use jetstream_wireformat::{JetStreamWireFormat, WireFormat};
2

            
3
use crate::Framer;
4

            
5
pub type Error = jetstream_error::Error;
6

            
7
pub(crate) const TLERROR: u8 = 6;
8

            
9
pub const RLERROR: u8 = TLERROR + 1;
10

            
11
pub const RJETSTREAMERROR: u8 = TLERROR - 1;
12

            
13
/// r[impl jetstream.error.v2.wireformat.error-frame]
14
/// A **global** id, below `MESSAGE_ID_START`, like every other id in this
15
/// frame.
16
///
17
/// It used to be 107 — 9P2000's `Rerror`, with a `TERROR = 106` beside it
18
/// that named a request 9P never had and that nothing here ever emitted.
19
/// `#[service]` allocates per-method ids as `MESSAGE_ID_START + 2 * index`
20
/// from 102, so 106 and 107 *are* the request and response of method
21
/// index 2: every protocol with three or more methods had this frame
22
/// sitting in the middle of its own range.
23
///
24
/// Harmless only because one code path did not exist. Nothing constructs
25
/// `ErrorFrame::RError`, so no encoder has ever written 107 — which is
26
/// also why moving it costs nothing: there is no peer speaking the old
27
/// id to break compatibility with. Its siblings were already down here
28
/// (`RJETSTREAMERROR` 5, `RLERROR` 7), so this is the outlier rejoining
29
/// them rather than a new convention.
30
pub const RERROR: u8 = 11;
31

            
32
#[derive(Debug, JetStreamWireFormat)]
33
pub struct Rlerror {
34
    pub ecode: u32,
35
}
36

            
37
// Rerror
38
#[derive(Debug, JetStreamWireFormat)]
39
pub struct Rerror {
40
    pub ename: String,
41
}
42

            
43
// r[impl jetstream.error.v2.wireformat.error-frame]
44
pub enum ErrorFrame {
45
    JetStreamError(Error),
46
    RlError(Rlerror),
47
    RError(Rerror),
48
}
49

            
50
impl Framer for ErrorFrame {
51
    fn message_type(&self) -> u8 {
52
        match self {
53
            ErrorFrame::JetStreamError(_) => RJETSTREAMERROR,
54
            ErrorFrame::RlError(_) => RLERROR,
55
            ErrorFrame::RError(_) => RERROR,
56
        }
57
    }
58

            
59
    fn byte_size(&self) -> u32 {
60
        match self {
61
            ErrorFrame::JetStreamError(error) => error.byte_size(),
62
            ErrorFrame::RlError(rlerror) => rlerror.byte_size(),
63
            ErrorFrame::RError(rerror) => rerror.byte_size(),
64
        }
65
    }
66

            
67
    fn encode<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
68
        match self {
69
            ErrorFrame::JetStreamError(error) => error.encode(writer),
70
            ErrorFrame::RlError(rlerror) => rlerror.encode(writer),
71
            ErrorFrame::RError(rerror) => rerror.encode(writer),
72
        }
73
    }
74

            
75
    fn decode<R: std::io::Read>(
76
        reader: &mut R,
77
        ty: u8,
78
    ) -> std::io::Result<Self> {
79
        match ty {
80
            RERROR => {
81
                let err = Rerror::decode(reader)?;
82
                Ok(ErrorFrame::RError(err))
83
            }
84
            RLERROR => {
85
                let err = Rlerror::decode(reader)?;
86
                Ok(ErrorFrame::RlError(err))
87
            }
88
            RJETSTREAMERROR => {
89
                let err = Error::decode(reader)?;
90
                Ok(ErrorFrame::JetStreamError(err))
91
            }
92
            _ => Err(std::io::Error::new(
93
                std::io::ErrorKind::InvalidData,
94
                format!("unknown message type: {}", ty),
95
            )),
96
        }
97
    }
98
}
99

            
100
impl From<Error> for ErrorFrame {
101
    fn from(err: Error) -> Self {
102
        ErrorFrame::JetStreamError(err)
103
    }
104
}
105

            
106
pub fn error_to_rmessage(err: &Rlerror) -> Error {
107
    let kind = match err.ecode as i32 {
108
        jetstream_libc::ENOENT => std::io::ErrorKind::NotFound,
109
        jetstream_libc::EPERM => std::io::ErrorKind::PermissionDenied,
110
        jetstream_libc::ECONNREFUSED => std::io::ErrorKind::ConnectionRefused,
111
        jetstream_libc::ECONNRESET => std::io::ErrorKind::ConnectionReset,
112
        jetstream_libc::ECONNABORTED => std::io::ErrorKind::ConnectionAborted,
113
        jetstream_libc::ENOTCONN => std::io::ErrorKind::NotConnected,
114
        jetstream_libc::EADDRINUSE => std::io::ErrorKind::AddrInUse,
115
        jetstream_libc::EADDRNOTAVAIL => std::io::ErrorKind::AddrNotAvailable,
116
        jetstream_libc::EPIPE => std::io::ErrorKind::BrokenPipe,
117
        jetstream_libc::EEXIST => std::io::ErrorKind::AlreadyExists,
118
        jetstream_libc::EWOULDBLOCK => std::io::ErrorKind::WouldBlock,
119
        jetstream_libc::EINVAL => std::io::ErrorKind::InvalidInput,
120
        jetstream_libc::ETIMEDOUT => std::io::ErrorKind::TimedOut,
121
        jetstream_libc::EINTR => std::io::ErrorKind::Interrupted,
122
        _ => std::io::ErrorKind::Other,
123
    };
124
    std::io::Error::new(kind, "rlerror".to_string()).into()
125
}
126

            
127
impl From<ErrorFrame> for Error {
128
    fn from(frame: ErrorFrame) -> Self {
129
        match frame {
130
            ErrorFrame::JetStreamError(error) => error,
131
            ErrorFrame::RlError(rlerror) => error_to_rmessage(&rlerror),
132
            ErrorFrame::RError(rerror) => Error::new(rerror.ename),
133
        }
134
    }
135
}