-
Notifications
You must be signed in to change notification settings - Fork 244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use thiserror crate to make error definition more ergonomic. #640
Open
wt
wants to merge
1
commit into
tafia:master
Choose a base branch
from
wt:use_thiserror_lib
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||||
---|---|---|---|---|---|---|
|
@@ -2,50 +2,62 @@ | |||||
|
||||||
use crate::escape::EscapeError; | ||||||
use crate::events::attributes::AttrError; | ||||||
use crate::utils::write_byte_string; | ||||||
use std::fmt; | ||||||
use std::io::Error as IoError; | ||||||
use std::str::Utf8Error; | ||||||
use std::string::FromUtf8Error; | ||||||
use std::sync::Arc; | ||||||
|
||||||
/// The error type used by this crate. | ||||||
#[derive(Clone, Debug)] | ||||||
#[derive(thiserror::Error, Clone, Debug)] | ||||||
pub enum Error { | ||||||
/// IO error. | ||||||
/// | ||||||
/// `Arc<IoError>` instead of `IoError` since `IoError` is not `Clone`. | ||||||
#[error("I/O error: {0}")] | ||||||
Io(Arc<IoError>), | ||||||
/// Input decoding error. If `encoding` feature is disabled, contains `None`, | ||||||
/// otherwise contains the UTF-8 decoding error | ||||||
#[error("Malformed input, decoding impossible")] | ||||||
NonDecodable(Option<Utf8Error>), | ||||||
/// Unexpected End of File | ||||||
#[error("Unexpected EOF during reading {0}")] | ||||||
UnexpectedEof(String), | ||||||
/// End event mismatch | ||||||
#[error("Expecting </{expected}> found </{found}>")] | ||||||
EndEventMismatch { | ||||||
/// Expected end event | ||||||
expected: String, | ||||||
/// Found end event | ||||||
found: String, | ||||||
}, | ||||||
/// Unexpected token | ||||||
#[error("Unexpected token '{0}'")] | ||||||
UnexpectedToken(String), | ||||||
/// Unexpected <!> | ||||||
#[error( | ||||||
"Only Comment (`--`), CDATA (`[CDATA[`) and DOCTYPE (`DOCTYPE`) nodes can start with a '!', but symbol `{}` found", | ||||||
*(.0) as char)] | ||||||
UnexpectedBang(u8), | ||||||
/// Text not found, expected `Event::Text` | ||||||
#[error("Cannot read text, expecting Event::Text")] | ||||||
TextNotFound, | ||||||
/// `Event::BytesDecl` must start with *version* attribute. Contains the attribute | ||||||
/// that was found or `None` if an xml declaration doesn't contain attributes. | ||||||
#[error("XmlDecl must start with 'version' attribute, found {0:?}")] | ||||||
XmlDeclWithoutVersion(Option<String>), | ||||||
/// Empty `Event::DocType`. `<!doctype foo>` is correct but `<!doctype > is not. | ||||||
/// | ||||||
/// See <https://www.w3.org/TR/xml11/#NT-doctypedecl> | ||||||
#[error("DOCTYPE declaration must not be empty")] | ||||||
EmptyDocType, | ||||||
/// Attribute parsing error | ||||||
#[error("error while parsing attribute: {0}")] | ||||||
InvalidAttr(AttrError), | ||||||
/// Escape error | ||||||
#[error("{0}")] | ||||||
EscapeError(EscapeError), | ||||||
/// Specified namespace prefix is unknown, cannot resolve namespace for it | ||||||
#[error("Uknonwn namespace prefix '{:?}'", crate::utils::Bytes(.0))] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Misprint
Suggested change
|
||||||
UnknownPrefix(Vec<u8>), | ||||||
} | ||||||
|
||||||
|
@@ -91,52 +103,6 @@ impl From<AttrError> for Error { | |||||
/// A specialized `Result` type where the error is hard-wired to [`Error`]. | ||||||
pub type Result<T> = std::result::Result<T, Error>; | ||||||
|
||||||
impl fmt::Display for Error { | ||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||||||
match self { | ||||||
Error::Io(e) => write!(f, "I/O error: {}", e), | ||||||
Error::NonDecodable(None) => write!(f, "Malformed input, decoding impossible"), | ||||||
Error::NonDecodable(Some(e)) => write!(f, "Malformed UTF-8 input: {}", e), | ||||||
Error::UnexpectedEof(e) => write!(f, "Unexpected EOF during reading {}", e), | ||||||
Error::EndEventMismatch { expected, found } => { | ||||||
write!(f, "Expecting </{}> found </{}>", expected, found) | ||||||
} | ||||||
Error::UnexpectedToken(e) => write!(f, "Unexpected token '{}'", e), | ||||||
Error::UnexpectedBang(b) => write!( | ||||||
f, | ||||||
"Only Comment (`--`), CDATA (`[CDATA[`) and DOCTYPE (`DOCTYPE`) nodes can start with a '!', but symbol `{}` found", | ||||||
*b as char | ||||||
), | ||||||
Error::TextNotFound => write!(f, "Cannot read text, expecting Event::Text"), | ||||||
Error::XmlDeclWithoutVersion(e) => write!( | ||||||
f, | ||||||
"XmlDecl must start with 'version' attribute, found {:?}", | ||||||
e | ||||||
), | ||||||
Error::EmptyDocType => write!(f, "DOCTYPE declaration must not be empty"), | ||||||
Error::InvalidAttr(e) => write!(f, "error while parsing attribute: {}", e), | ||||||
Error::EscapeError(e) => write!(f, "{}", e), | ||||||
Error::UnknownPrefix(prefix) => { | ||||||
f.write_str("Unknown namespace prefix '")?; | ||||||
write_byte_string(f, prefix)?; | ||||||
f.write_str("'") | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
impl std::error::Error for Error { | ||||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||||||
match self { | ||||||
Error::Io(e) => Some(e), | ||||||
Error::NonDecodable(Some(e)) => Some(e), | ||||||
Error::InvalidAttr(e) => Some(e), | ||||||
Error::EscapeError(e) => Some(e), | ||||||
_ => None, | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
#[cfg(feature = "serialize")] | ||||||
pub mod serialize { | ||||||
//! A module to handle serde (de)serialization errors | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably this could have
transparent
attribute