-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from rcore-os/thiserror
Use thiserror for error types.
- Loading branch information
Showing
6 changed files
with
54 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,47 @@ | ||
//! This module contain the error from the VirtIO socket driver. | ||
|
||
use core::{fmt, result}; | ||
use core::result; | ||
use thiserror::Error; | ||
|
||
/// The error type of VirtIO socket driver. | ||
#[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
#[derive(Copy, Clone, Debug, Eq, Error, PartialEq)] | ||
pub enum SocketError { | ||
/// There is an existing connection. | ||
#[error("There is an existing connection. Please close the current connection before attempting to connect again.")] | ||
ConnectionExists, | ||
/// The device is not connected to any peer. | ||
#[error("The device is not connected to any peer. Please connect it to a peer first.")] | ||
NotConnected, | ||
/// Peer socket is shutdown. | ||
#[error("The peer socket is shutdown.")] | ||
PeerSocketShutdown, | ||
/// The given buffer is shorter than expected. | ||
#[error("The given buffer is shorter than expected")] | ||
BufferTooShort, | ||
/// The given buffer for output is shorter than expected. | ||
#[error("The given output buffer is too short. '{0}' bytes is needed for the output buffer.")] | ||
OutputBufferTooShort(usize), | ||
/// The given buffer has exceeded the maximum buffer size. | ||
#[error("The given buffer length '{0}' has exceeded the maximum allowed buffer length '{1}'")] | ||
BufferTooLong(usize, usize), | ||
/// Unknown operation. | ||
#[error("The operation code '{0}' is unknown")] | ||
UnknownOperation(u16), | ||
/// Invalid operation, | ||
#[error("Invalid operation")] | ||
InvalidOperation, | ||
/// Invalid number. | ||
#[error("Invalid number")] | ||
InvalidNumber, | ||
/// Unexpected data in packet. | ||
#[error("No data is expected in the packet")] | ||
UnexpectedDataInPacket, | ||
/// Peer has insufficient buffer space, try again later. | ||
#[error("Peer has insufficient buffer space, try again later")] | ||
InsufficientBufferSpaceInPeer, | ||
/// Recycled a wrong buffer. | ||
#[error("Recycled a wrong buffer")] | ||
RecycledWrongBuffer, | ||
} | ||
|
||
impl fmt::Display for SocketError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
Self::ConnectionExists => write!( | ||
f, | ||
"There is an existing connection. Please close the current connection before attempting to connect again."), | ||
Self::NotConnected => write!(f, "The device is not connected to any peer. Please connect it to a peer first."), | ||
Self::PeerSocketShutdown => write!(f, "The peer socket is shutdown."), | ||
Self::BufferTooShort => write!(f, "The given buffer is shorter than expected"), | ||
Self::BufferTooLong(actual, max) => { | ||
write!(f, "The given buffer length '{actual}' has exceeded the maximum allowed buffer length '{max}'") | ||
} | ||
Self::OutputBufferTooShort(expected) => { | ||
write!(f, "The given output buffer is too short. '{expected}' bytes is needed for the output buffer.") | ||
} | ||
Self::UnknownOperation(op) => { | ||
write!(f, "The operation code '{op}' is unknown") | ||
} | ||
Self::InvalidOperation => write!(f, "Invalid operation"), | ||
Self::InvalidNumber => write!(f, "Invalid number"), | ||
Self::UnexpectedDataInPacket => write!(f, "No data is expected in the packet"), | ||
Self::InsufficientBufferSpaceInPeer => write!(f, "Peer has insufficient buffer space, try again later"), | ||
Self::RecycledWrongBuffer => write!(f, "Recycled a wrong buffer"), | ||
} | ||
} | ||
} | ||
|
||
pub type Result<T> = result::Result<T, SocketError>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters