-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: change error handling to be strongly-typed (#1834)
* Refactor error handling to be strongly-typed. * Remove unused function. * Remove comment. * Add module + type docstrings. * Make error module private. * Fix windows build.
- Loading branch information
1 parent
f8fc8a8
commit 8416f6d
Showing
6 changed files
with
130 additions
and
150 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//! Shadowsocks-specific error encoding. | ||
/// A result with a shadowsocks-specific error. | ||
pub type ShadowsocksResult<T = ()> = Result<T, ShadowsocksError>; | ||
|
||
/// A generic error class which encodes all possible ways the application can | ||
/// fail, along with debug information. | ||
#[derive(Clone, Debug)] | ||
pub enum ShadowsocksError { | ||
ServerExitUnexpectedly(String), | ||
ServerAborted(String), | ||
LoadConfigFailure(String), | ||
LoadAclFailure(String), | ||
InsufficientParams(String), | ||
} | ||
|
||
impl ShadowsocksError { | ||
/// The corresponding `sysexits::ExitCode` for this error. | ||
pub fn exit_code(&self) -> sysexits::ExitCode { | ||
match self { | ||
Self::ServerExitUnexpectedly(_) | Self::ServerAborted(_) => sysexits::ExitCode::Software, | ||
Self::LoadConfigFailure(_) | Self::LoadAclFailure(_) => sysexits::ExitCode::Config, | ||
Self::InsufficientParams(_) => sysexits::ExitCode::Usage, | ||
} | ||
} | ||
} | ||
|
||
impl std::fmt::Display for ShadowsocksError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Self::ServerExitUnexpectedly(msg) | ||
| Self::ServerAborted(msg) | ||
| Self::LoadConfigFailure(msg) | ||
| Self::LoadAclFailure(msg) | ||
| Self::InsufficientParams(msg) => write!(f, "{msg}"), | ||
} | ||
} | ||
} | ||
|
||
impl std::error::Error for ShadowsocksError {} |
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
Oops, something went wrong.