mysql_common/proto/codec/
error.rs

1// Copyright (c) 2017 Anatoly Ikorsky
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9use std::{fmt, io};
10
11#[derive(Debug)]
12pub enum PacketCodecError {
13    Io(io::Error),
14    PacketTooLarge,
15    PacketsOutOfSync,
16    BadCompressedPacketHeader,
17}
18
19impl From<io::Error> for PacketCodecError {
20    fn from(io_err: io::Error) -> Self {
21        Self::Io(io_err)
22    }
23}
24
25impl fmt::Display for PacketCodecError {
26    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27        match self {
28            PacketCodecError::Io(io_err) => f.write_fmt(format_args!("IO error: `{}'", io_err)),
29            PacketCodecError::PacketTooLarge => {
30                f.write_str("Packet is larger than max_allowed_packet")
31            }
32            PacketCodecError::PacketsOutOfSync => f.write_str("Packets out of sync"),
33            PacketCodecError::BadCompressedPacketHeader => {
34                f.write_str("Bad compressed packet header")
35            }
36        }
37    }
38}
39
40impl std::error::Error for PacketCodecError {
41    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
42        match self {
43            PacketCodecError::Io(io_err) => Some(io_err),
44            _other => None,
45        }
46    }
47}